我想在使用 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);
}
}
});
}
}
Android 将再次运行 onDestroy() 和 onCreate() 来执行方向更改。为了避免这种情况,请尝试在您的 Activity 中实现 onConfigurationChanged() 并且不要忘记在您的 AndroidManifest.xml 中允许“android:config=”orientation”
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)