在 Android 中旋转的 ImageView

ImageView with rotation in Android

我需要制作一个具有旋转功能的图像视图。所以我查看了 android 开发人员 site。并使用了他们的代码。但不知何故我得到了一个错误。

错误:java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.AnimationDrawable

我有这些代码:

ImageView refresh = (ImageView)findViewById(R.id.refresh_devices_button);
refresh.setBackgroundResource(R.drawable.spin_animation); // The IDE says that it may produce null pointer exception
AnimationDrawable frameAnimation = (AnimationDrawable) refresh.getBackground();
frameAnimation.start();

在spin_animation.xml中:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<animation-list android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/scan_1" android:duration="50" />
    <item android:drawable="@drawable/scan_2" android:duration="50" />
    <item android:drawable="@drawable/scan_3" android:duration="50" />
    <item android:drawable="@drawable/scan_4" android:duration="50" />
    <item android:drawable="@drawable/scan_5" android:duration="50" />
</animation-list>
</selector>

请帮助我。从 android 的网站我得到了代码,但他们的代码不起作用。也许问题出在我的 spin_animation.xml 文件上。

refresh.getBackgroundreturnsStateListDrawable我觉得。

旋转动画到imageview

 `RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
  anim.setInterpolator(new LinearInterpolator());
  anim.setRepeatCount(Animation.INFINITE);
  anim.setDuration(700);

  // Start animating the image
  final ImageView splash = (ImageView) findViewById(R.id.splash);
  splash.startAnimation(anim);

  // Later.. stop the animation
 splash.setAnimation(null);`

我的 ImageView 有一个旋转动画。您可以尝试使用此代码: 首先在 anim 目录中为旋转动画创建 xml : rotation_animation.xml :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="400"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="0"
        android:toDegrees="360" />
</set>

然后在你的 Java class(activity 或片段)中添加:

Animation rotationAnimation = AnimationUtils.loadAnimation(this, R.anim.rotation_animation);
yourImageView.startAnimation(rotationAnimation);

现在你准备好了。编码愉快 :)

你可以使用这样的东西

        ImageView splash = (ImageView) view.findViewById(R.id.imageView);

        RotateAnimation anim = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatCount(Animation.INFINITE);
        anim.setDuration(500);

        // Start animating the image

        splash.startAnimation(anim);

使用下面的代码并在下面导入 class。

import android.graphics.drawable.AnimationDrawable;



private AnimationDrawable mAnimation;
private ImageView mAnimLogo;

mAnimLogo = (ImageView) findViewById(R.id.loading_image);
mAnimation = (AnimationDrawable) mAnimLogo.getDrawable();


 mAnimation.start();

这一行:

AnimationDrawable frameAnimation = (AnimationDrawable) refresh.getBackground();

您正在尝试将 StateListDrawable 转换为引发异常的 AnimationDrawable。

简洁明了 :

refresh.animate().rotation(360).setDuration(...);

在 ... 毫秒内将您的视图顺时针旋转 360 度

 refresh.animate().rotationBy(360).setDuration(...);

将视图旋转 BY 360 度。

查看 ViewPropertyAnimator 可以在一行中编写不同类型的动画。

我发现 android 网站没问题。问题出在我的 xml 文件上。 我有这个代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <animation-list android:id="@+id/selected" android:oneshot="false">
        <item android:drawable="@drawable/scan_1" android:duration="50" />
        <item android:drawable="@drawable/scan_2" android:duration="50" />
        <item android:drawable="@drawable/scan_3" android:duration="50" />
        <item android:drawable="@drawable/scan_4" android:duration="50" />
        <item android:drawable="@drawable/scan_5" android:duration="50" />
    </animation-list>
</selector>

我使用下面的代码代替上面的代码。我不得不删除 <selector> 标签。

<?xml version="1.0" encoding="utf-8"?>
<animation-list 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/scan_1" android:duration="50" />
    <item android:drawable="@drawable/scan_2" android:duration="50" />
    <item android:drawable="@drawable/scan_3" android:duration="50" />
    <item android:drawable="@drawable/scan_4" android:duration="50" />
    <item android:drawable="@drawable/scan_5" android:duration="50" />
</animation-list>