2014-10-27 74 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