2014-10-27 76 views
1

我有以下代码,我试图实现翻转卡动画。当我点击第二个图像视图时,它会突然翻转并翻转(很难解释,它只是快速翻转)。另外,当我点击第二个imageview时,第一个imageview上的动画就开始了。你能告诉我如何解决这个问题吗?我只想要卡片上的动画在点击特定卡片时启动。只有一个图像视图显示android中的动画

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

onClick1是不正确的签名,除非你把它放在与属性的XML。如果是后者的话,那么你就必须删除,一个

之间
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) { 

     } 
    } 
+0

非常感谢Blackbelt。动画适用于第二个图像视图,但我现在有另一个问题。当我点击第二张图片时,第一张图片的动画也开始了。而且,第二个图像视图的动画只能使用一次。对于第一个图像视图一切工作正常。请告诉我应该怎么做。 – 2014-10-27 15:00:02

+0

编辑您的问题并发布您正在使用的代码 – Blackbelt 2014-10-27 15:01:13