android - 旋转动画后如何在ImageView上设置OnClickListener

标签 android animation onclick rotation imageview

我想在我使用 addView 方法动态创建它们并在我的应用程序中旋转它们之后,在 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"

关于android - 旋转动画后如何在ImageView上设置OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6743290/

相关文章:

jQuery 文本颜色变化动画

java - 在控制台中打印多行 ASCII 动画

python - 单击网站上的按钮然后抓取网页

android - java.lang.IllegalStateException : Cannot invoke observeForever on a background thread

java - Android IndexOutOfBoundException

android - sequoyah 的编译器错误,但从命令行构建良好

android - 无需拖放即可从主屏幕删除小部件

android - 在 Android Studio 中,布局 xml 文件可以分组到文件夹中吗?

javascript - 纹理加载错误(JS动画)

jquery - onclick ="return confirm(' ' )"working with $(' .class').click(function () {});