我有一個佈局,其中我使用6個圖像視圖,並且當我按下按鈕時,這6個圖像視圖全部同時翻轉或動畫。 現在我想要發生兩件事: 1)所有這些圖像視圖應在2秒後自行翻轉,而不需要按鈕的幫助。 2)所有的圖像視圖不應該同時翻轉,但在不同的時間。可以說一個圖像視圖在1秒後翻轉,一些視圖在2秒後翻轉,一些在3秒後翻轉,因此圖像視圖總是在屏幕上翻轉,或者只要屏幕沒有改變。如何在android中自己(沒有按鈕)動畫(翻轉)圖像視圖?
如何實現這一點。請指導。 下面是我編寫的用於翻轉按鈕單擊圖像視圖的代碼。
MainActivity.java:
public class MainActivity extends ActionBarActivity 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);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF0000")));
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.button).setOnClickListener(this);
}
@Override
public void onClick(View v)
{
v.setEnabled(false);
((ImageView)findViewById(R.id.imageView1)).clearAnimation();
((ImageView)findViewById(R.id.imageView1)).setAnimation(animation1);
((ImageView)findViewById(R.id.imageView1)).startAnimation(animation1);
((ImageView)findViewById(R.id.imageView2)).clearAnimation();
((ImageView)findViewById(R.id.imageView2)).setAnimation(animation1);
((ImageView)findViewById(R.id.imageView2)).startAnimation(animation1);
((ImageView)findViewById(R.id.imageView3)).clearAnimation();
((ImageView)findViewById(R.id.imageView3)).setAnimation(animation1);
((ImageView)findViewById(R.id.imageView3)).startAnimation(animation1);
((ImageView)findViewById(R.id.imageView4)).clearAnimation();
((ImageView)findViewById(R.id.imageView4)).setAnimation(animation1);
((ImageView)findViewById(R.id.imageView4)).startAnimation(animation1);
((ImageView)findViewById(R.id.imageView5)).clearAnimation();
((ImageView)findViewById(R.id.imageView5)).setAnimation(animation1);
((ImageView)findViewById(R.id.imageView5)).startAnimation(animation1);
((ImageView)findViewById(R.id.imageView6)).clearAnimation();
((ImageView)findViewById(R.id.imageView6)).setAnimation(animation1);
((ImageView)findViewById(R.id.imageView6)).startAnimation(animation1);
}
@Override
public void onAnimationEnd(Animation animation) {
if (animation==animation1) {
if (isBackOfCardShowing) {
((ImageView)findViewById(R.id.imageView1)).setImageResource(R.drawable.back);
((ImageView)findViewById(R.id.imageView2)).setImageResource(R.drawable.back);
((ImageView)findViewById(R.id.imageView3)).setImageResource(R.drawable.back);
((ImageView)findViewById(R.id.imageView4)).setImageResource(R.drawable.back);
((ImageView)findViewById(R.id.imageView5)).setImageResource(R.drawable.back);
((ImageView)findViewById(R.id.imageView6)).setImageResource(R.drawable.back);
} else {
((ImageView)findViewById(R.id.imageView1)).setImageResource(R.drawable.aboutus);
((ImageView)findViewById(R.id.imageView2)).setImageResource(R.drawable.privacy);
((ImageView)findViewById(R.id.imageView3)).setImageResource(R.drawable.conv_up);
((ImageView)findViewById(R.id.imageView4)).setImageResource(R.drawable.volunteer);
((ImageView)findViewById(R.id.imageView5)).setImageResource(R.drawable.share);
((ImageView)findViewById(R.id.imageView6)).setImageResource(R.drawable.rateus);
}
((ImageView)findViewById(R.id.imageView1)).clearAnimation();
((ImageView)findViewById(R.id.imageView1)).setAnimation(animation2);
((ImageView)findViewById(R.id.imageView1)).startAnimation(animation2);
((ImageView)findViewById(R.id.imageView2)).clearAnimation();
((ImageView)findViewById(R.id.imageView2)).setAnimation(animation2);
((ImageView)findViewById(R.id.imageView2)).startAnimation(animation2);
((ImageView)findViewById(R.id.imageView3)).clearAnimation();
((ImageView)findViewById(R.id.imageView3)).setAnimation(animation2);
((ImageView)findViewById(R.id.imageView3)).startAnimation(animation2);
((ImageView)findViewById(R.id.imageView4)).clearAnimation();
((ImageView)findViewById(R.id.imageView4)).setAnimation(animation2);
((ImageView)findViewById(R.id.imageView4)).startAnimation(animation2);
((ImageView)findViewById(R.id.imageView5)).clearAnimation();
((ImageView)findViewById(R.id.imageView5)).setAnimation(animation2);
((ImageView)findViewById(R.id.imageView5)).startAnimation(animation2);
((ImageView)findViewById(R.id.imageView6)).clearAnimation();
((ImageView)findViewById(R.id.imageView6)).setAnimation(animation2);
((ImageView)findViewById(R.id.imageView6)).startAnimation(animation2);
} else {
isBackOfCardShowing=!isBackOfCardShowing;
findViewById(R.id.button).setEnabled(true);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
感謝您的幫助..您的代碼確實幫助了我。我不得不修改它以使圖像視圖之間產生間隔。謝謝。 – user2900761