2014-06-19 24 views
0

我有一個佈局,其中我使用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 

} 

} 

回答

0

可以使用CountdownTimer類。

第一個參數是計時器應該運行多少毫秒,第二個參數是多少毫秒,一個是,刻度是onTick每次都執行1 勾選完成並且onFinish在倒計時結束時執行。

我認爲你可以創造性地使用它來實現你的結果。

CountdownTimer定時器=新CountDownTimer(2000,1000){

  public void onTick(long millisUntilFinished) { 
       //Do something every tick 
      } 

      public void onFinish() { 

       //Do something when timer finishes 
      } 
     }.start(); 
1
public class MainActivity extends ActionBarActivity implements OnClickListener, AnimationListener { 

private Animation animation1; 
private Animation animation2; 
int current = 0; 
int[] imageViewIds ={R.id.imageView1,R.id.imageView2,R.id.imageVie3,R.id.imageView4,R.id.imageView5,R.id.imageView6}; 
@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); 

    playLoopingAnimations(); 
} 

playLoopingAnimations(); 
{ 
    ((ImageView)findViewById(imageViewIds[current])).clearAnimation(); 
    ((ImageView)findViewById(imageViewIds[current])).setAnimation(animation1); 
    ((ImageView)findViewById(imageViewIds[current])).startAnimation(animation1); 
} 

@Override 
public void onAnimationEnd(Animation animation) 
{ 
    current++; 
    if(current==imageViewIds.lenghth()) 
    current=0; 
    playLoopingAnimations(); 
} 

@Override 
public void onAnimationRepeat(Animation animation) 
{ 

} 

@Override 
public void onAnimationStart(Animation animation) 
{ 

} 

} 

我覺得這種設置可以修改一點,以一針見血的頭!

+0

感謝您的幫助..您的代碼確實幫助了我。我不得不修改它以使圖像視圖之間產生間隔。謝謝。 – user2900761