2012-05-20 43 views
0

我在屏幕上有一個ImageView,我想讓它搖動(向左旋轉,然後向右旋轉)。 我知道如何製作動畫的ImageView的,這是我的代碼:ImageView中的環狀動畫

new RotateAnimation(20f, 50f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 

anim.setInterpolator(new LinearInterpolator()); 
anim.setRepeatCount(Animation.INFINITE); 
anim.setDuration(700); 

// Start animating the image 
final ImageView splash = (ImageView) findViewById(R.id.mobileshake); 
splash.startAnimation(anim); 

的問題是,現在的ImageView的是循環一個動畫,但我想2個動畫循環(左轉再右轉)。

我該怎麼做?

對不起,我的英文不好..

回答

0

我想通了通過執行以下操作,而且運作非常順利:)

final RotateAnimation anim1 = new RotateAnimation(20f, 50f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
     anim1.setInterpolator(new LinearInterpolator()); 
     //anim1.setRepeatCount(Animation.INFINITE); 
     anim1.setDuration(300); 

     final RotateAnimation anim2 = new RotateAnimation(50f, 20f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
     anim2.setInterpolator(new LinearInterpolator()); 
     //anim2.setRepeatCount(Animation.INFINITE); 
     anim2.setDuration(300); 

     final ImageView splash = (ImageView) findViewById(R.id.mobileshake); 
     anim1.setAnimationListener(new AnimationListener(){ 

      public void onAnimationEnd(Animation animation) { 
       // TODO Auto-generated method stub 
       splash.startAnimation(anim2); 
      } 

      public void onAnimationRepeat(Animation animation) { 
       // TODO Auto-generated method stub 

      } 

      public void onAnimationStart(Animation animation) { 
       // TODO Auto-generated method stub 

      }}); 
     anim2.setAnimationListener(new AnimationListener(){ 

      public void onAnimationEnd(Animation animation) { 
       // TODO Auto-generated method stub 
       splash.startAnimation(anim1); 
      } 

      public void onAnimationRepeat(Animation animation) { 
       // TODO Auto-generated method stub 

      } 

      public void onAnimationStart(Animation animation) { 
       // TODO Auto-generated method stub 

      }}); 

     splash.startAnimation(anim1); 
0

您可以結合使用AnimationSet兩個(或更多)的動畫。

API Demos中使用xml中定義的TranslateAnimation有一個「搖動」動畫的例子。按照類似的方法,您可以獲得您正在查找的結果。

+0

謝謝!我也想出了一個不同的解決方案:P –