2015-01-14 13 views
9

Tôi đã làm theo ví dụ do Google cung cấp về cách sử dụng AnimationDrawable với ImageView. Bạn có thể tìm thấy nó ở đây: 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(); 

Khi tôi chạy nó tôi nhận được lỗi:

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

Google dường như nghĩ rằng điều này sẽ làm việc, nhưng nếu bạn không thể bỏ BitmapDrawable để AnimationDrawable Tôi không chắc chắn như thế nào đây là vụ phải làm việc?

+0

Nguồn bài cho animation.xml. – alanv

+0

Đó không phải là vấn đề. Vấn đề là với mã của Google. Bạn không thể đúc một BitmapDrawable thành một AnimationDrawable. – TheOneX

+0

Nếu animation.xml định nghĩa một AnimationDrawable, điều này sẽ hoạt động. View.getBackground() chỉ trả về drawable đã được nạp từ tài nguyên XML nền. Tài liệu là chính xác. Có thể có một vấn đề khác. – alanv

Trả lời

6

Tôi đã tìm ra giải pháp cho vấn đề này.

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

Tôi không biết tại sao tài liệu của Google lại sử dụng nền, nhưng sử dụng công việc setImageDrawable và getDrawable. Trung thực nó có ý nghĩa hơn nó sẽ làm việc theo cách này hơn so với cách khác anyways.

2

Tôi gặp vấn đề tương tự. Tôi biết chủ đề này là một số tháng tuổi, nhưng có lẽ ai đó những gì để đọc về kinh nghiệm của tôi.

Tôi không biết tại sao, nhưng google không chấp nhận Nhãn hiệu như "_" trong Picturenames của mình trong khi sử dụng nó cho hoạt ảnh. Tôi sử dụng các tên như "loading_frame1" và nó không hoạt động. Tôi đã thay đổi tên để một cái gì đó như "loadingframe1" và nó hoạt động ....

Trước:

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

Sau:

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

Và ở đây Liệt kê 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

Mã của Google hoạt động. Các "không thể được đúc vấn đề" mà dẫn tôi ở đây, là bởi vì tôi đã không chú ý và đặt animation.xml của tôi trong res.anim thay vì res.drawable.

Tuy nhiên, tôi đồng ý sử dụng setImageDrawable và getDrawable hoạt động tốt hơn.

Các vấn đề liên quan