2016-10-17 3 views
0

Я следил за приложением Google, как использовать AnimationDrawable с ImageView. Вы можете найти его здесь: http://developer.android.com/guide/topics/graphics/drawable-animation.htmljava.lang.ClassCastException: android.graphics.drawable.ColorDrawable не может быть передан в android.graphics.drawable.AnimationDrawable

image.setBackgroundResource(R.drawable.run_animation); 

     frameAnimation = (AnimationDrawable) image.getDrawable(); 
     frameAnimation.start(); 

вытяжки файла run_animation:

<?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/iv1" 
     android:duration="500" /> 

    <item 
     android:drawable="@drawable/iv2" 
     android:duration="500" /> 
    <item 
     android:drawable="@drawable/iv3" 
     android:duration="500" /> 
    <item 
     android:drawable="@drawable/iv4" 
     android:duration="500" /> 

</animation-list> 

После выполнения я receciving этой ошибки на LogCat

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

пожалуйста, помогите мне.

+1

Вы можете увидеть подобную нить [здесь] (http://stackoverflow.com/questions/27951712/android-animationdrawable-cast -ошибка). Поскольку @TheOneX сказал в этом потоке, попробуйте getDrawable(), а не getBackground() – Raghavendra

+0

Он не работает – kashyap

+0

Неужели вы пытаетесь с getDrawable(), чем image.getBackground()? – Raghavendra

ответ

0

Я получаю эту ошибку при вызове метода setBackgroundColor(), который определен в классе View.

image.setBackgroundColor(Color.parseColor("#ffffcc")); 

Вместо setCardBackgroundColor() метод должен быть вызван, который является специфическим для ImageViews и определены в пределах класса ImageView.

Этот параметр как цвет фона и радиус угла код работает для меня:

image.setCardBackgroundColor(Color.parseColor("#ffffcc")); 
image.setRadius(50); 
Смежные вопросы