2015-01-14 3 views
9

ImageView에서 AnimationDrawable을 사용하는 방법에 대한 Google 제공 예제를 따르고있었습니다. 당신은 여기에서 찾을 수 있습니다 http://developer.android.com/guide/topics/graphics/drawable-animation.htmlAndroid : AnimationDrawable cast error

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 자원에서로드 된 드로어 블을 리턴합니다. 설명서가 정확합니다. 또 다른 문제가있을 수 있습니다. – alanv

답변

6

이 문제의 해결책을 찾았습니다.

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

나는 구글의 문서 배경을 사용하여 말한다 이유는 모르겠지만, 사용 setImageDrawable 및 getDrawable 작동합니다. 솔직히 어쨌든 다른 방법보다이 방법이 효과적 일 것입니다.

2

동일한 문제가있었습니다. 이 스레드는 몇 달 전이지만, 어쩌면 누군가 내 경험에 대해 읽어야 할 것입니다.

왜 그런지 모르지만 Google에서는 애니메이션을 사용하면서 자신의 그림 이름에서 "_"과 같은 공간 마크를 허용하지 않습니다. 나는 "loading_frame1"과 같은 이름을 사용합니다.

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); 
    } 
} 
LoadingAnimation.class 리스팅을

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

그리고 여기 :

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

후 :

전에 .... 나는 "loadingframe1"와 같은 뭔가 이름을 변경하고 그것을 작동

1

Google 코드가 작동합니다. 나를 이끌고있는 "캐스팅 할 수없는 문제"는주의를 기울이지 않고 res.xml 대신 res.anim에 animation.xml을 넣었 기 때문입니다.

그러나 setImageDrawable 및 getDrawable을 사용하면 더 잘 작동합니다.