java - Android 动画可绘制代码在 2.2 中工作正常,但在 2.3 中不起作用?

标签 java android xml android-manifest

我是 Android 新手。

我的动画代码在 android 2.2 中工作正常,但在 android 2.3 中不起作用,当我在 android 2.3 中运行动画代码时,它显示如下错误:

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap
android.graphics.Bitmap@405795e8

在我的 Activity 中,我有三个按钮和一个 ImageView ,因为我单击 ImageView ,正在播放一个动画,但是当我第一次使用 ball_but 按钮时,它会播放一些动画,到目前为止它工作正常,但是当我再次单击时在 ball_but 它给出了像 log cat 中这样的强制关闭错误。

我对此一无所知。

我的动画 xml 文件:

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

    android:oneshot="true" >

    <item
        android:drawable="@drawable/cap_animation01"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation02"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation03"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation04"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation05"
        android:duration="100"/>
    <item
        android:drawable="@drawable/cap_animation01"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation02"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation03"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation04"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation05"
        android:duration="100"/>
 <item
        android:drawable="@drawable/cap_animation01"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation02"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation03"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation04"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation05"
        android:duration="100"/>
     <item
        android:drawable="@drawable/cap_animation01"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation02"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation03"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation04"
        android:duration="100"/>

    <item
        android:drawable="@drawable/cap_animation05"
        android:duration="100"/>

    <item
        android:drawable="@drawable/elephant_first"
        android:duration="100"/>
      </animation-list>

我的 Activity :

public class Home extends Activity implements OnClickListener  {
        Button ball_but,drum_but,hat_but,freeapps_but,moreapps_but,start_but;
        MediaPlayer mp;
        int flag = 0;
        ImageView elepahant_img;
        private static Boolean isSoundPlaying=false;
        static SelectTalkTask mytask;
        AnimationDrawable drawable=null;
        AnimationDrawable drawable_one=null;
        AnimationDrawable drawable_two=null;



        public void onCreate(Bundle savedInstanceState) {        
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
                super.onCreate(savedInstanceState);
                setContentView(R.layout.talking_elephant_home);
                elepahant_img=(ImageView)findViewById(R.id.elephant_img);        
                elepahant_img.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                    isSoundPlaying = true;
                    release(drawable);
                    AnimationDrawable drawable = (AnimationDrawable) getResources().getDrawable(R.layout.water);
                    ImageView im = (ImageView) findViewById(R.id.elephant_img);
                    im.setBackgroundDrawable(drawable);
                    im.post(drawable);
                    play_water(); ///play the water
                    checkIfAnimationDone(drawable);
            }

            private void play_water() {
                    if(mp!=null){
                        mp.reset();
                        mp.release();
                    }
                    mp = MediaPlayer.create(Home.this,R.raw.water);
                    mp.start();
                }
        });

                drum_but=(Button)findViewById(R.id.drum_but);
                drum_but.setOnClickListener(this);

                hat_but=(Button)findViewById(R.id.cap_but);
                hat_but.setOnClickListener(this);

                ball_but=(Button)findViewById(R.id.ball_but);
                ball_but.setOnClickListener(this);


        }


      public void onClick(View v) {
        switch (v.getId()) {
        case R.id.ball_but:

            release(drawable);          
             Log.i("TAG", "this is ball_but"+isSoundPlaying);                        
                drawable = (AnimationDrawable) getResources().getDrawable(R.layout.elephant);
                elepahant_img = (ImageView) findViewById(R.id.elephant_img);
                elepahant_img.setBackgroundDrawable(drawable);
                elepahant_img.post(drawable);
                play();
                checkIfAnimationDone(drawable);
                break;                
        case R.id.drum_but:
            isSoundPlaying = true;
            release(drawable);           
             Log.i("TAG", "this is cap_but"+isSoundPlaying);
            drawable = (AnimationDrawable) getResources().getDrawable(R.layout.drum);
            elepahant_img = (ImageView) findViewById(R.id.elephant_img);            
            elepahant_img.setBackgroundDrawable(drawable);
            elepahant_img.post(drawable);
            play_one();
            checkIfAnimationDone(drawable);
            break;
        case R.id.cap_but:
            isSoundPlaying = true;
            release(drawable);           
             Log.i("TAG", "this is cap_but"+isSoundPlaying);
            drawable = (AnimationDrawable) getResources().getDrawable(R.layout.cap);
            elepahant_img = (ImageView) findViewById(R.id.elephant_img);    
            elepahant_img.setBackgroundDrawable(drawable);
            elepahant_img.post(drawable);
            play_two();
            checkIfAnimationDone(drawable);
            break;


        }

    }
      protected void play() {
            if(mp!=null){
                mp.reset();
                mp.release();
            }
            mp = MediaPlayer.create(Home.this,R.raw.ball);
            mp.start();
            }
    private void play_two() {
        if(mp!=null){
            mp.reset();
            mp.release();
        }
        mp = MediaPlayer.create(Home.this,R.raw.cap);
        mp.start();    
    }
    private void play_one() {
        if(mp!=null){
            mp.reset();
            mp.release();
        }
        mp = MediaPlayer.create(Home.this,R.raw.drums);
        mp.start();    
    }

    protected void onPause() {
        super.onPause();
        try{
        mp.stop();
        }catch (NullPointerException e) {
            e.printStackTrace();
        }
     }





     public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                this.finish();            
                System.exit(0);
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }     

        public void onDestroy() { 
            super.onDestroy();
            System.out.println("OnDestroy");             

            System.gc();
            Runtime.getRuntime().gc();
            */
            elepahant_img = (ImageView)findViewById(R.id.elephant_img);
            if (elepahant_img != null)
            {
                ((BitmapDrawable)elepahant_img.getDrawable()).getBitmap().recycle();
                elepahant_img.setImageDrawable(null);
            }
            System.gc();

            }


         public void release(AnimationDrawable ad){
             if(ad==null)
                 return;
             ad.stop();
             for (int i = 0; i < ad.getNumberOfFrames(); ++i){
                 Drawable frame = ad.getFrame(i);
                 if (frame instanceof BitmapDrawable) {

                     ((BitmapDrawable)frame).getBitmap().recycle();
                     }
               frame.setCallback(null);
                 frame=null;
             }
             ad.setCallback(null);
              ad=null;

         }
}   

最佳答案

当您尝试回收已回收的位图时,可能会出现此错误。在对帧的位图调用回收之前添加 null 测试。

关于java - Android 动画可绘制代码在 2.2 中工作正常,但在 2.3 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11950301/

相关文章:

Android Kotlin 按钮 + 警告对话框 + 列表

java - JAXB 泛型 @XmlValue

java + xml : libraries handle encoding from the <? xml ?> 标题?

java - 我如何修复这个特定的空指针异常

使用 HibernateTemplate 时出现 java.lang.NoClassDefFoundError

java - Java 中使用 commons HttpClient 的curl 等效项

android - sencha 中的 setTimer() 和 timeInterval() 在 Android 和 iPhone 设备上不起作用

java - 如何在 android gradle 中免除 pro-guard 的库模块

python - 如何用python处理xml中的键值样式标签

java - 如何配置OSGI声明性服务范围?