旋转动画android

时间:2012-10-14 14:04:37

标签: android animation

我正在尝试制作旋转图像动画。 我需要围绕本身旋转图标,就像在进度条中一样,但我得到的是围绕圆圈旋转的图像。 这是我的动画代码

<?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 个答案:

答案 0 :(得分:66)

这是布局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>

这是我的活动:

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);

    }


}

答案 1 :(得分:32)

您可以尝试使用以下代码,而不是使用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);

答案 2 :(得分:5)

嗯,我得到了我错的地方。我给了我的图像填充,导致它每次旋转时移动一点。 无论如何我删除了填充,现在它正常工作。

答案 3 :(得分: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

相关问题