2012-08-10 71 views
0

我有一個ImageView圖像。我需要旋轉這個圖像90去油脂,然後從左到右移動這個圖像。我管理如何做到這一點。我用AnnimationListener和旋轉完成後我開始moveAnimation()。但在運動圖像返回到原始外觀之前(旋轉之前)。旋轉Android:旋轉動畫和移動動畫組合

XML代碼rotation.xml

<?xml version="1.0" encoding="utf-8"?> 
<rotate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="0" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:toDegrees="90" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="1000" 
    android:startOffset="0" 
/> 

rotateAnimation()

private void rotateAnimation(){ 

     Animation rotation = AnimationUtils.loadAnimation(getContext(), R.anim.rotate); 
     rotation.setRepeatCount(0); 
     rotation.setFillAfter(true); 

     rotation.setAnimationListener(new AnimationListener() { 


     public void onAnimationEnd(Animation animation) { 
      moveAnnimation(); 
     } 
    }); 

moveAnnimation()

 private void moveAnnimation(){ 

    TranslateAnimation moveLefttoRight = new TranslateAnimation(0, 2000, 0, 0); 
     moveLefttoRight.setDuration(1000); 
     moveLefttoRight.setFillAfter(true); 

     moveLefttoRight.setAnimationListener(new AnimationListener() { 

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

     } 

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

     } 

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

     } 
    }); 


    image.startAnimation(moveLefttoRight); 
} 
+0

你能舉出你用來旋轉和移動圖像的示例代碼。 – Shachillies 2012-08-10 09:45:47

+0

好的,但我給你我的意見..嘗試使用矩陣旋轉和類似的矩陣翻譯,這樣你的矩陣將集中和圖像視圖將不會重置。不要忘記在執行任何矩陣操作之前將imageview的屬性scaletype設置爲矩陣。 – Shachillies 2012-08-10 09:46:22

+0

添加代碼看看吧plz – haawa 2012-08-10 16:55:00

回答

0

您需要setFillAfter(true)的旋轉和平移兩個Animation obje克拉。

Animation.html#setFillAfter(boolean)

If fillAfter is true, the transformation that this animation performed will persist when it is finished. Defaults to false if not set. Note that this applies to individual animations and when using an AnimationSet to chain animations.

以下是我的代碼,你需要AnimationSet實現鏈接動畫效果。

public class MainActivity extends Activity { 

    ImageView image = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     image = (ImageView)findViewById(R.id.imageview); 

     animateImageView(); 
    } 

    private void animateImageView() { 
     AnimationSet set = new AnimationSet(true); 
     set.setFillAfter(true); 

     Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation); 
     TranslateAnimation moveLefttoRight = new TranslateAnimation(0, 200, 0, 0); 
     moveLefttoRight.setDuration(1000); 
     moveLefttoRight.setStartOffset(1000); 

     set.addAnimation(rotation); 
     set.addAnimation(moveLefttoRight); 

     image.startAnimation(set); 
    } 
} 
+0

確定我做到了!那就是問題所在!我不好意思,我沒說過。 – haawa 2012-08-10 09:22:57

+0

你可以顯示你的代碼的一部分,你如何做動畫? – NcJie 2012-08-10 09:24:02

+0

稍後我會添加一些代碼,但總而言之,我使用xml動畫文件進行旋轉和簡單的移動動畫而不使用xml文件。 – haawa 2012-08-10 09:25:25