2015-01-14 14 views
9

Ich folgte dem von Google bereitgestellten Beispiel für die Verwendung von AnimationDrawable mit einem ImageView. Sie können es hier finden: http://developer.android.com/guide/topics/graphics/drawable-animation.htmlAndroid: AnimationDrawable Cast-Fehler

imageView.setBackgroundResource(R.drawable.animation); 
AnimationDrawable animation = (AnimationDrawable)imageView.getBackground(); 
animation.start(); 

Wenn ich es laufen bekomme ich den Fehler:

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

Google scheint dies zu denken, sollte funktionieren, aber wenn man keine BitmapDrawable werfen kann AnimationDrawable Ich bin nicht Sicher wie das funktionieren soll?

+0

Post-Quelle für animation.xml. – alanv

+0

Das ist nicht das Problem. Das Problem ist mit Google-Code. Sie können ein BitmapDrawable nicht in ein AnimationDrawable-Objekt umwandeln. – TheOneX

+0

Wenn animation.xml ein AnimationDrawable definiert, sollte dies funktionieren. View.getBackground() gibt nur das Zeichen zurück, das von der Hintergrund-XML-Ressource geladen wurde. Die Dokumentation ist korrekt. Möglicherweise gibt es ein anderes Problem. – alanv

Antwort

6

Ich fand die Lösung für dieses Problem.

imageView.setImageDrawable(getResource().getDrawable(R.drawable.animation); 
AnimationDrawable animation = (AnimationDrawable)imageView.getDrawable(); 
animation.start(); 

Ich habe keine Ahnung, warum die Google-Dokumentation sagt den Hintergrund zu verwenden, aber mit setImageDrawable und getDrawable funktioniert. Ehrlich gesagt macht es mehr Sinn, dass es auf diese Weise funktioniert als auf die andere Art und Weise.

2

Ich hatte das gleiche Problem. Ich weiß, dass dieser Thread einige Monate alt ist, aber vielleicht jemand, der etwas über meine Erfahrung zu lesen hat.

Ich weiß nicht warum, aber Google akzeptiert keine Leerzeichen wie "_" in seinen Bildnamen, während es für die Animation verwendet wird. Ich benutze Namen wie "loading_frame1", und es funktioniert nicht. Ich änderte den Namen so etwas wie "loadingframe1" und es funktioniert ....

Vorher:

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> 

    <item android:drawable="@drawable/loading_frame1" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame2" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame3" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame4" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame5" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame6" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame7" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame8" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame9" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame10" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame11" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame12" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame13" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame14" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame15" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame16" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame17" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame18" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame19" android:duration="100" /> 
    <item android:drawable="@drawable/loading_frame20" android:duration="100" /> 

</animation-list> 

Nach:

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> 

    <item android:drawable="@drawable/loadingframe1" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe2" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe3" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe4" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe5" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe6" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe7" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe8" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe9" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe10" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe11" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe12" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe13" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe14" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe15" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe16" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe17" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe18" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe19" android:duration="100" /> 
    <item android:drawable="@drawable/loadingframe20" android:duration="100" /> 

</animation-list> 

Und hier dem LoadingAnimation.class Eintrag

package com.justkidding.animation; 

import android.support.v7.app.ActionBarActivity; 
import android.graphics.drawable.AnimationDrawable; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ImageView; 

public class LoadingAnimation extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_loading_animation);  
    } 

    @Override 
    public void onWindowFocusChanged (boolean hasFocus) { 
     super.onWindowFocusChanged(hasFocus); 

     ImageView animation = (ImageView)findViewById(R.id.aniimage); 
     animation.setBackgroundResource(R.drawable.loading_animation); 
     AnimationDrawable frameAnimation = (AnimationDrawable) animation.getBackground(); 
     if(hasFocus) { 
      frameAnimation.start(); 
     } else { 
      frameAnimation.stop(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.loading_animation, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 
1

Googles Code funktioniert. Das "can not cast issue", das mich hierher führt, war, weil ich nicht aufpasste und meine animation.xml in res.anim anstelle von res.drawable ablegte.

Allerdings stimme ich mit SetImageDrawable und getDrawable funktioniert besser.