设置OnClickListener上旋转动画后的ImageVie

设置OnClickListener上旋转动画后的ImageVie

本文介绍了如何设置OnClickListener上旋转动画后的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置OnClickListener上ImageViews后,我让他们使用addView方法动态,并在我的应用程序旋转。但是,当我美元画面p $ pssed的ImageView的onClick方法不能正常工作。如果我在的ImageView的原始位置preSS屏幕,它的工作原理。我想在旋转动画后的最终位置,以使其他先进ImageViews,但我不知道如何使具有ImageView的倾斜布局。我等待着你的帮助......

感谢您。

 进口android.app.Activity;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Matrix;
进口android.graphics.RectF;
进口android.graphics.drawable.BitmapDrawable;进口android.os.Bundle;
进口android.util.Log;
进口android.view.Gravity;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup.LayoutParams;
进口android.view.animation.Animation;
进口android.view.animation.Animation.AnimationListener;
进口android.view.animation.LinearInterpolator;
进口android.view.animation.RotateAnimation;
进口android.widget.FrameLayout;
进口android.widget.ImageButton;
进口android.widget.ImageView;
进口android.widget.ImageView.ScaleType;
进口android.widget.Toast;公共类MovingBillActivity延伸活动{
    私人字符串美元[] = {dollar1,dollar5,dollar10,dollar20,dollar50,dollar100};    私人ImageView的钱包;    的FrameLayout mFrameLayout;    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        钱包=(ImageView的)findViewById(R.id.wallet);        最后OnClickListener处理程序=新OnClickListener(){
            公共无效的onClick(视图v){
                开关(v.getId()){
                    壳体100:Log.i(KCY,100);打破;
                    情况50:Log.i(KCY,50);打破;
                }
            }
        };
        wallet.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                wallet.setClickable(假);
                //创建在其中添加ImageView的一个的FrameLayout
                mFrameLayout =(的FrameLayout)findViewById(R.id.ll01);                RotateAnimation动画;                对于(INT J = 0; J< dollars.length; J ++){
                    //实例化一个ImageView的并定义其属性
                    最终的ImageView I =新ImageView的(MovingBillActivity.this);
                    开关(J){
                        案例5:i.setImageResource(R.drawable.dollar100);打破;
                        情况4:i.setImageResource(R.drawable.dollar50);打破;
                        案例3:i.setImageResource(R.drawable.dollar20);打破;
                        案例2:i.setImageResource(R.drawable.dollar10);打破;
                        案例1:i.setImageResource(R.drawable.dollar5);打破;
                        情况下0:i.setImageResource(R.drawable.dollar1);打破;
                    }
                    i.setAdjustViewBounds(真); //设置ImageView的界限,以匹配可绘制的尺寸
                    i.setLayoutParams(新FrameLayout.LayoutParams(170,67,Gravity.CENTER));
                    i.setOnClickListener(处理);                    动画=新RotateAnimation(0F,30F - J * 20F,170,34);
                    anim.setInterpolator(新LinearInterpolator());
                    anim.setRepeatCount(Animation.ABSOLUTE);
                    anim.setDuration(500);
                    anim.setFillEnabled(真);
                    anim.setFillAfter(真);                    //将ImageView的加入布局和设置布局为内容视图
                    mFrameLayout.addView(ⅰ);
                    i.startAnimation(阿尼姆);
                }
            }
        });
    }
}


解决方案

Android将运行的onDestroy()和OnCreate()中再次进行方向改变。为了避免这种情况,尽量工具onConfigurationChanged()在您的活动,不要忘记,让机器人:配置=方向在你的Andr​​oidManifest.xml

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);
                }
            }
        });
    }
}
解决方案

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

这篇关于如何设置OnClickListener上旋转动画后的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 02:25