如何在旋转动画后在 ImageView 上设置 OnClickListener
我想在使用 addView 方法动态创建 ImageView 并在我的应用程序中旋转它们后,在 ImageView 上设置 OnClickListener 。但是当我在屏幕上按下 ImageView 时,onClick 方法无法正常工作。如果我在 ImageView 的原始位置按屏幕,它就会起作用。我想在旋转动画后的最终位置提前制作其他ImageView,但我不知道如何使ImageView具有倾斜的布局。我正在等待您的帮助......
谢谢。
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;
public class MovingBillActivity extends Activity {
private String dollars[] = {"dollar1", "dollar5", "dollar10", "dollar20", "dollar50", "dollar100"};
private ImageView wallet;
FrameLayout mFrameLayout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wallet = (ImageView) findViewById(R.id.wallet);
final OnClickListener handler = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case 100: Log.i("kcy", "100"); break;
case 50: Log.i("kcy", "50"); break;
}
}
};
wallet.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
wallet.setClickable(false);
// Create a FrameLayout in which to add the ImageView
mFrameLayout = (FrameLayout) findViewById(R.id.ll01);
RotateAnimation anim;
for(int j=0; j<dollars.length; j++) {
// Instantiate an ImageView and define its properties
final ImageView i = new ImageView(MovingBillActivity.this);
switch (j) {
case 5 : i.setImageResource(R.drawable.dollar100); break;
case 4 : i.setImageResource(R.drawable.dollar50); break;
case 3 : i.setImageResource(R.drawable.dollar20); break;
case 2 : i.setImageResource(R.drawable.dollar10); break;
case 1 : i.setImageResource(R.drawable.dollar5); break;
case 0 : i.setImageResource(R.drawable.dollar1); break;
}
i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
i.setLayoutParams(new FrameLayout.LayoutParams(170, 67, Gravity.CENTER));
i.setOnClickListener(handler);
anim = new RotateAnimation(0f, 30f - j * 20f, 170, 34);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.ABSOLUTE);
anim.setDuration(500);
anim.setFillEnabled(true);
anim.setFillAfter(true);
// Add the ImageView to the layout and set the layout as the content view
mFrameLayout.addView(i);
i.startAnimation(anim);
}
}
});
}
}
I want to set OnClickListener on the ImageViews after I make them using addView method dynamically and rotate them in my application. But onClick method doesn't work properly when I pressed ImageView on screen. If I press the screen at the original location of the ImageView, it works. I want to make other ImageViews in advanced at the final location after rotation animation, but I don't know how to make ImageView having skewed Layout. I'm waiting for your help.....
Thank you.
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;
public class MovingBillActivity extends Activity {
private String dollars[] = {"dollar1", "dollar5", "dollar10", "dollar20", "dollar50", "dollar100"};
private ImageView wallet;
FrameLayout mFrameLayout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wallet = (ImageView) findViewById(R.id.wallet);
final OnClickListener handler = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case 100: Log.i("kcy", "100"); break;
case 50: Log.i("kcy", "50"); break;
}
}
};
wallet.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
wallet.setClickable(false);
// Create a FrameLayout in which to add the ImageView
mFrameLayout = (FrameLayout) findViewById(R.id.ll01);
RotateAnimation anim;
for(int j=0; j<dollars.length; j++) {
// Instantiate an ImageView and define its properties
final ImageView i = new ImageView(MovingBillActivity.this);
switch (j) {
case 5 : i.setImageResource(R.drawable.dollar100); break;
case 4 : i.setImageResource(R.drawable.dollar50); break;
case 3 : i.setImageResource(R.drawable.dollar20); break;
case 2 : i.setImageResource(R.drawable.dollar10); break;
case 1 : i.setImageResource(R.drawable.dollar5); break;
case 0 : i.setImageResource(R.drawable.dollar1); break;
}
i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
i.setLayoutParams(new FrameLayout.LayoutParams(170, 67, Gravity.CENTER));
i.setOnClickListener(handler);
anim = new RotateAnimation(0f, 30f - j * 20f, 170, 34);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.ABSOLUTE);
anim.setDuration(500);
anim.setFillEnabled(true);
anim.setFillAfter(true);
// Add the ImageView to the layout and set the layout as the content view
mFrameLayout.addView(i);
i.startAnimation(anim);
}
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Android 将再次运行 onDestroy() 和 onCreate() 来执行方向更改。为了避免这种情况,请尝试在您的 Activity 中实现 onConfigurationChanged() 并且不要忘记在您的 AndroidManifest.xml 中允许“android:config=”orientation”
Android will run onDestroy() and onCreate() again to perform the orientation change. To avoid this, try to implements onConfigurationChanged() in your activity and don't forget to allow "android:config="orientation" in your AndroidManifest.xml