2015-01-14 56 views
9

我按照Google提供的如何將AnimationDrawable與ImageView一起使用的示例。你可以在這裏找到它:http://developer.android.com/guide/topics/graphics/drawable-animation.htmlAndroid:AnimationDrawable投射錯誤

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

當我運行它,我得到的錯誤:

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

谷歌似乎認爲這應該工作,但是如果你不能施放BitmapDrawable到AnimationDrawable我不是確定這應該如何工作?

+0

animation.xml的發佈源。 – alanv

+0

這不是問題所在。問題出在Google的代碼上。您無法將BitmapDrawable轉換爲AnimationDrawable。 – TheOneX

+0

如果animation.xml定義了一個AnimationDrawable,這應該起作用。 View.getBackground()只返回從後臺XML資源加載的drawable。該文件是正確的。可能有另一個問題。 – alanv

回答

6

我想出瞭解決這個問題的辦法。

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

我不知道爲什麼谷歌的文檔說要使用背景,但使用setImageDrawable和getDrawable的作品。說實話,它更有意義,它會以這種方式工作,而不是反過來。

2

我有同樣的問題。我知道這個帖子已經有一個月了,但也許有人會看我的經歷。

我不知道爲什麼,但谷歌不接受像他的照片名稱中的「_」這樣的空間,而將其用於動畫。我使用像「loading_frame1」這樣的名稱,它不起作用。我改變了名稱類似於 「loadingframe1」 和它的作品....

前:

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

後:

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

而這裏LoadingAnimation.class上市

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

Google的代碼有效。在這裏引導我的「不能被拋出的問題」是因爲我沒有關注並將我的animation.xml放在res.anim而不是res.drawable中。

但我同意使用setImageDrawable和getDrawable更好地工作。