2

我需要使用旋转功能进行图像视图。所以我看着Android开发者site。并使用他们的代码。但不知何故,我得到一个错误。ImageView在Android中的旋转

错误: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.getBackground返回StateListDrawable我想。

回答

0

我发现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> 
0

旋转动画,以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);` 
+0

对不起,我编辑了我的答案.. – KrishnaJ

0

我有我的ImageView的旋转动画。您可以使用此代码尝试: 首先创建您的旋转动画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类(活动或片段)补充一点:

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

现在你已经准备好了。快乐作弄:)

+0

我没有anim目录。我应该直接添加到res文件夹下或应用程序文件夹下。 – gunescelil

+0

你需要创建一个新文件夹,右键单击res文件夹,创建新文件夹命名为anim。你可以把你的动画xmls放进去。 –

1

您可以使用类似下面的代码和进口下面这个类

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

使用为好。

import android.graphics.drawable.AnimationDrawable; 



private AnimationDrawable mAnimation; 
private ImageView mAnimLogo; 

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


mAnimation.start(); 
0

这一行:

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

你想投一个StateListDrawable到引发异常的AnimationDrawable。

尼斯和简单:

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

顺时针旋转视图360度的...毫秒。

或者

旋转BY 360度视图

检查出ViewPropertyAnimator您可以在一行中编码的各种动画。

+0

我的持续时间未知。我正在扫描一些东西,所以它必须继续,直到完成搜索。 – gunescelil