2012-08-10 38 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