安卓旋转动画

23

我正在尝试做一个旋转的图像动画。 我需要将一个图标围绕自身旋转,就像在进度条中一样,但我得到的是一个图像围绕圆形旋转的效果。 以下是我的动画代码:

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

这里我做错了什么? 谢谢

4个回答

69

这是用于布局的主要源代码 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/testButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="animationText" 
        android:onClick="AnimClick"/>

    <ImageView
        android:id="@+id/testImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:contentDescription="image_desc"
        android:scaleType="fitCenter"
        android:src="@drawable/cat2" />

</RelativeLayout>

为实现旋转动画,我们可以通过XML或Java代码定义动画。如果要在XML中编写动画,则需要在/res/anim文件夹下创建一个动画XML文件。这里,我们创建一个名为rotate_around_center_point.xml的XML文件。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <rotate
        android:duration="2500"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:toDegrees="360" />

</set>

这是我的Activity:

public class MainActivity extends Activity implements OnClickListener {

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

        Button btn = (Button) findViewById(R.id.testButton);
        btn.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        ImageView animationTarget = (ImageView) this.findViewById(R.id.testImage);

        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_around_center_point);
        animationTarget.startAnimation(animation);

    }


}

谢谢。我已经尝试将ScaleType添加到图像中,但仍然没有帮助。 - orelzion

33
您可以尝试以下代码,而不是在XML中完成它:
RotateAnimation rotate = new RotateAnimation(0, 360,
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
        0.5f);

rotate.setDuration(4000);
rotate.setRepeatCount(Animation.INFINITE);
yourView.setAnimation(rotate);

你知道如何避免循环完成后重复之前的暂停(停顿)吗? - portfoliobuilder
8
尝试添加 rotate.setInterpolator(new LinearInterpolator());。该语句的作用是设置旋转动画的插值器为线性插值器,以使动画更加顺畅自然。 - Carl Whalley

4

我知道了我的错误。我给我的图片添加了padding,这导致它每次旋转时都会稍微移动一下。 无论如何,我去掉了padding,现在它正常工作了。


0
将以下xml添加到动画文件夹中。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" 
    android:duration="4000" 
    android:fromdegrees="0" 
    android:pivotx="50%" 
    android:pivoty="50%"
    android:todegrees="360" 
    android:toyscale="0.0">
 </set>

希望这能对你有所帮助。
如需更多信息,请参考http://www.blazin.in/2013/09/simple-rotate-animation-in-android.html

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接