2016-03-26 61 views
1

我想每次旋轉一次imageView 10次,imageView會隨機加載可繪製資源中的圖像。例如:有6個圖像,如img1到img6。我不喜歡這樣的代碼,但它不工作重複動畫時更改圖片

public void clockwise(View view){ 

    for (int i=1; i<=6; i++){ 
     ImageView image = (ImageView)findViewById(R.id.imageView); 
     Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise); 
     animation.setRepeatCount(10); 

     int max=6, min=1; 
     int randNum = min + (int)(Math.random()*((max-min)+1));// 
     if (randNum==1) image.setImageDrawable(getResources().getDrawable(R.drawable.die1)); 
     else if (randNum==2) image.setImageDrawable(getResources().getDrawable(R.drawable.die2)); 
     else if (randNum==3) image.setImageDrawable(getResources().getDrawable(R.drawable.die3)); 
     else if (randNum==4) image.setImageDrawable(getResources().getDrawable(R.drawable.die4)); 
     else if (randNum==5) image.setImageDrawable(getResources().getDrawable(R.drawable.die5)); 
     else if (randNum==6) image.setImageDrawable(getResources().getDrawable(R.drawable.die6)); 

     image.startAnimation(animation); 


    } 

它只是加載,我在XML文件中設置爲10次重複

回答

1

你可以用動畫聽衆像下面

只有一個圖像
 animation.setAnimationListener(new Animation.AnimationListener() { 
@Override 
public void onAnimationStart(Animation animation) { 

} 

@Override 
public void onAnimationEnd(Animation animation) { 

} 

@Override 
public void onAnimationRepeat(Animation animation) { 

} 
});) 

因此,在onAnimationRepeat方法中,您可以更改圖像。

1

而不是循環使用動畫偵聽器。

ImageView image = (ImageView)findViewById(R.id.imageView); 
     Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise); 
     animation.setRepeatCount(10); 

image.startAnimation(animation); 

覆蓋的方法。

@Override 
    public void onAnimationStart(Animation animation) { 

    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 

    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 

int max=6, min=1; 
     int randNum = min + (int)(Math.random()*((max-min)+1));// 
     if (randNum==1) image.setImageDrawable(getResources().getDrawable(R.drawable.die1)); 
     else if (randNum==2) image.setImageDrawable(getResources().getDrawable(R.drawable.die2)); 
     else if (randNum==3) image.setImageDrawable(getResources().getDrawable(R.drawable.die3)); 
     else if (randNum==4) image.setImageDrawable(getResources().getDrawable(R.drawable.die4)); 
     else if (randNum==5) image.setImageDrawable(getResources().getDrawable(R.drawable.die5)); 
     else if (randNum==6) image.setImageDrawable(getResources().getDrawable(R.drawable.die6)); 


    } 
0

感謝KDeogharkar和Ragesh的建議。

我試圖用你的方式改變圖像onAnimationRepeat(),但它仍然無法正常工作。我想這是因爲圖像仍然被動畫使用,所以它不允許改變圖像。因此,我寫了一個等待動畫結束的遞歸函數來改變圖像,並調用相同的動畫,所以最終它起作用。這裏是我的代碼

public void rotate(int count){ 

     // 
     final int nextCount = count - 1; 
     ImageView image = (ImageView)findViewById(R.id.imageView); 
     int max = 6, min = 1; 
     int randNum = min + (int) (Math.random() * ((max - min) + 1));// 
     if (randNum == 1) 
      image.setImageDrawable(getResources().getDrawable(R.drawable.die1)); 
     else if (randNum == 2) 
      image.setImageDrawable(getResources().getDrawable(R.drawable.die2)); 
     else if (randNum == 3) 
      image.setImageDrawable(getResources().getDrawable(R.drawable.die3)); 
     else if (randNum == 4) 
      image.setImageDrawable(getResources().getDrawable(R.drawable.die4)); 
     else if (randNum == 5) 
      image.setImageDrawable(getResources().getDrawable(R.drawable.die5)); 
     else if (randNum == 6) 
      image.setImageDrawable(getResources().getDrawable(R.drawable.die6)); 


     Animation animation; 
     animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise); 

     animation.setRepeatCount(1); 
     animation.setAnimationListener(new Animation.AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 

      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 

       if (nextCount >=0) rotate(nextCount); 

      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 

      } 
     }); 

     image.startAnimation(animation); 
    } 

之後,我只需要在main()函數中調用rotate(animationTimes)。