只有一个图像视图在android中显示动画

时间:2014-10-27 14:32:09

标签: android android-animation

我有以下代码,我正在尝试实现翻转卡动画。当我点击第二张图像时,它会翻转并突然转动(很难解释,它只是快速翻转)。此外,当我单击第二个imageview时,第一个imageview上的动画开始。你能建议我怎么解决这个问题吗?我只想要在点击该特定卡时启动卡上的动画。

 package com.example.twocards;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.app.Activity;
public class MainActivity extends Activity implements OnClickListener,
AnimationListener {

       private Animation animation1;
       private Animation animation2;
       private boolean isBackOfCardShowing = true;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);
             animation1 = AnimationUtils.loadAnimation(this, R.anim.to_middle);
             animation1.setAnimationListener(this);
             animation2 = AnimationUtils.loadAnimation(this, R.anim.from_middle);
             animation2.setAnimationListener(this);
             findViewById(R.id.imageView2).setOnClickListener(this);
             findViewById(R.id.imageView1).setOnClickListener(this);
       }
       @Override
       public void onClick(View v) {
           if (v.getId() == R.id.imageView1) {
              v.setEnabled(false);
              ((ImageView)findViewById(R.id.imageView1)).clearAnimation();
              ((ImageView)findViewById(R.id.imageView1)).setAnimation(animation1);
              ((ImageView)findViewById(R.id.imageView1)).startAnimation(animation1);
        }else if (v.getId() == R.id.imageView2) {
            v.setEnabled(false);
            ((ImageView)findViewById(R.id.imageView2)).clearAnimation();
            ((ImageView)findViewById(R.id.imageView2)).setAnimation(animation1);
            ((ImageView)findViewById(R.id.imageView2)).startAnimation(animation1);
        }
           }

        @Override
        public void onAnimationEnd(Animation animation) {
              if (animation==animation1) {
                     if (isBackOfCardShowing) {
        ((ImageView)findViewById(R.id.imageView1)).setImageResource(R.drawable.strategy);
                       } else {
        ((ImageView)findViewById(R.id.imageView1)).setImageResource(R.drawable.memory);
                       }
                       ((ImageView)findViewById(R.id.imageView1)).clearAnimation();
         ((ImageView)findViewById(R.id.imageView1)).setAnimation(animation2);
         ((ImageView)findViewById(R.id.imageView1)).startAnimation(animation2);
                 } else {
                        isBackOfCardShowing=!isBackOfCardShowing;
                        findViewById(R.id.imageView1).setEnabled(true);
                 }
        }
        public void onAnimationEnd1(Animation animation) {
              if (animation==animation1) {
                     if (isBackOfCardShowing) {
        ((ImageView)findViewById(R.id.imageView2)).setImageResource(R.drawable.memory);
                       } else {
        ((ImageView)findViewById(R.id.imageView2)).setImageResource(R.drawable.strategy);
                       }
                       ((ImageView)findViewById(R.id.imageView2)).clearAnimation();
         ((ImageView)findViewById(R.id.imageView2)).setAnimation(animation2);
         ((ImageView)findViewById(R.id.imageView2)).startAnimation(animation2);
                 } else {
                        isBackOfCardShowing=!isBackOfCardShowing;
                        findViewById(R.id.imageView2).setEnabled(true);
                 }
        }




        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }
        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

}

1 个答案:

答案 0 :(得分:1)

除非您使用onClick1属性在xml上设置,否则

android:onClick不是正确的签名。如果是后者,那么你必须删除,一个在

之间
findViewById(R.id.imageView2).setOnClickListener(this);
findViewById(R.id.imageView1).setOnClickListener(this);

如果不是这种情况,则仅调用onClick(View view),您必须区分单击的视图。一种方法是将视图提供的id作为参数进行比较:

   @Override
   public void onClick(View v) {
         if (v.getId() == R.id.imageView1) {
          v.setEnabled(false);
          ((ImageView)findViewById(R.id.imageView1)).clearAnimation();
          ((ImageView)findViewById(R.id.imageView1)).setAnimation(animation1);
          ((ImageView)findViewById(R.id.imageView1)).startAnimation(animation1);
        } else if (v.getId() == R.id.imageView2) {

        }
    }
相关问题