如何使ImageView不断旋转?

9
<ImageView
      android:id="@+id/imageView1"
      android:layout_width="300dp"
      android:layout_height="300dp"
      android:src="@drawable/zaid1"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true" />

我在一个活动中有一个ImageView,我该如何让它无限地以360度旋转?
2个回答

25

尝试使用RotateAnimation

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

rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(500);
rotateAnimation.setRepeatCount(Animation.INFINITE);

findViewById(R.id.imageView1).startAnimation(rotateAnimation);

3

或者使用XML方式。 在您的res文件夹中创建一个名为“anim”的文件夹。 创建一个XML文件,随意命名。您将在此处定义动画。

在XML文件中放置以下内容:

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

现在你只需加载动画:
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.your_xml_name);

然后启动它:

findViewById(R.id.imageView1).startAnimation(rotation);

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