2

I need to make an imageview with rotation feature. So I looked at the android developers site. And used their codes. But somehow I get an error.

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

I have these codes:

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

In 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>

Please help me. From android's site I get the codes but their codes does not work. Maybe the problem is with my spin_animation.xml file.

refresh.getBackground returns StateListDrawableI think.

gunescelil
  • 313
  • 1
  • 23

6 Answers6

1

You can use something like this

        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);
Andrey Danilov
  • 6,194
  • 5
  • 32
  • 56
0

Rotation Animation to 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);`
Krishna Kachhela
  • 773
  • 1
  • 7
  • 24
0

I have a rotation animation for my ImageView. You can try with this code: First create your xml for rotation animation in anim directory : 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>

After that in your Java class (activity or fragment) add this :

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

Now you are ready. Happy codding :)

Lubomir Babev
  • 1,892
  • 1
  • 12
  • 14
0

Use below code and import below class as well.

import android.graphics.drawable.AnimationDrawable;



private AnimationDrawable mAnimation;
private ImageView mAnimLogo;

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


 mAnimation.start();
ViramP
  • 1,659
  • 11
  • 11
0

With this line :

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

you are trying to cast a StateListDrawable to an AnimationDrawable which throws the exception.

Nice and simple :

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

rotates your view to 360 Degrees clockwise in ... ms.

Or

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

rotates the view BY 360 degrees.

Check out ViewPropertyAnimator for different kinds of animations you can code in one line.

A Honey Bustard
  • 3,433
  • 2
  • 22
  • 38
0

I found that android site is okay. The problem was with my xml file. I had this code

<?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>

Instead of the above code I used the following code. I had to remove the<selector> tags.

<?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>
gunescelil
  • 313
  • 1
  • 23