2013-06-27 136 views
2

我正在制作一个示例,将图像从左向右移动,然后自行旋转。 我试过使用AnimationSet,但图像旋转不正确。它在一个循环中移动,而不是它自己。 如何解决这个问题?将图像从左向右移动,然后旋转动画

我想要什么 enter image description here MainActivity

@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    AnimationSet set = new AnimationSet(true); 
    set.setFillAfter(true); 
    img_animation = (ImageView) findViewById(R.id.imgBanner); 
    Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation); 
    rotation.setStartOffset(2000); 
    rotation.setDuration(2000); 
    TranslateAnimation moveLefttoRight = new TranslateAnimation(0, 200, 0, 0); 
    moveLefttoRight.setStartOffset(1000); 
    moveLefttoRight.setDuration(1000); 
    set.addAnimation(moveLefttoRight); 
    set.addAnimation(rotation); 
    img_animation.startAnimation(set); 
} 

Rotation.xml

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

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <ImageView 
     android:id="@+id/imgBanner" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/banner" /> 

</RelativeLayout> 

任何建议与妄作

+0

你尝试单独应用每个动画而不是创建一个动画集? – neo

+0

@neo是的,我试着在moveLefttoRight的onAnimationEnd里面旋转。但是如果没有setFillAfter(true),图像会跳转到开始位置并自行旋转。所以我切换到AnimationSet – Dolphin

回答

5

尝试使用这样的:

RotateAnimation anim = new RotateAnimation(0f, 360f, 
        Animation.RELATIVE_TO_SELF, 0.5f, 
        Animation.RELATIVE_TO_SELF, 0.5f); 

因此,将相对于自身旋转。 希望这有助于。

编辑:

image.clearAnimation(); 
RotateAnimation anim = new RotateAnimation(30, 360, image.getWidth()/2, image.getHeight()/2); 
anim.setFillAfter(true); 
anim.setRepeatCount(0); 
anim.setDuration(10000); 
image.startAnimation(anim); 

我测试了这个在演示应用程序。它的工作很棒。希望这可以帮助你:)

+0

感谢您的快速答案,但它仍然在循环旋转。它围绕上图中的红点旋转。 – Dolphin

+0

是的,它围绕起始位置循环移动 – Dolphin

+0

你试过把'moveLefttoRight.setFillAfter(true);'? @Dolphin –

相关问题