2

Ich muss eine Bildansicht mit Rotationsfunktion machen. Also schaute ich auf die Android-Entwickler site. Und benutzte ihre Codes. Aber irgendwie bekomme ich einen Fehler.ImageView mit Drehung in Android

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

Ich habe diese 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> 

Bitte mir helfen. Von Android-Website bekomme ich die Codes, aber ihre Codes funktionieren nicht. Vielleicht ist das Problem mit meiner spin_animation.xml-Datei.

refresh.getBackground liefert StateListDrawable ich denke.

Antwort

0

Ich fand, dass Android-Website in Ordnung ist. Das Problem war mit meiner XML-Datei. Ich hatte diesen 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> 

Statt den obigen Code, den ich den folgenden Code verwendet. Ich musste die <selector> Tags entfernen.

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

Rotation Animation 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

Sorry, ich habe meine Antwort bearbeitet. – KrishnaJ

0

Ich habe eine Rotation Animation für mein Image. Sie können mit diesem Code versuchen: zuerst Ihre xml für die Rotation Animation in anim Verzeichnis erstellen: 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> 

Danach in Ihrer Java-Klasse (Aktivität oder Fragment) hinzufügen, um diesen:

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

Jetzt sind Sie bereit. Glückliche codding :)

+0

Ich habe kein Anim-Verzeichnis. Sollte ich direkt unter res-Ordner oder unter dem App-Ordner hinzufügen. – gunescelil

+0

Sie müssen einen neuen Ordner erstellen, klicken Sie mit der rechten Maustaste auf den Ordner res, erstellen Sie den neuen Ordner und nennen Sie ihn anim. Und Sie können Ihre Animation xmls darin einfügen. –

1

Sie können auch so etwas wie dieses

 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

Verwenden Sie den folgenden Code und Import unter Klasse.

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

Mit dieser Zeile:

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

Sie versuchen, eine StateListDrawable zu einem AnimationDrawable zu werfen, die die Ausnahme auslöst.

Schön und einfach:

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

dreht sich die Ansicht auf 360 Grad im Uhrzeigersinn in ... ms.

Oder

dreht die Ansicht BY 360 Grad.

Auschecken ViewPropertyAnimator für verschiedene Arten von Animationen können Sie in einer Zeile codieren.

+0

Meine Dauer ist nicht bekannt. Ich scanne etwas, also muss es weitergehen, bis die Suche beendet ist. – gunescelil