5

アプリの多くのアクティビティでanimationdrawablesを使用しています。これは、それらを初期化するためのコードです。

 public void prepareVideo (int resourceBall, String animation, int width, int height ){

    imgBall = (ImageView)findViewById(resourceBall);
    imgBall.setVisibility(ImageView.VISIBLE);
    String resourceNameAnimation = animation;
    int id_res = getResources().getIdentifier(resourceNameAnimation, "drawable", getPackageName());
    imgBall.setBackgroundResource(id_res);
        LayoutParams latoutFrame = imgBall.getLayoutParams();
    latoutFrame.width = width;
    latoutFrame.height = height;
    imgBall.setLayoutParams(latoutFrame); 
    frame = (AnimationDrawable) imgBall.getBackground();


}

次に、次のように呼び出します。imgBall.post(new StartAnimation());

ランナブルと呼ばれるもの:

class StartAnimation implements Runnable {

    public void run() {

frame.start();

    }

}

問題は、同じxml(フレームごと)を使用する多くのアニメーションを使用することです。同じアニメーションの多くのアクティビティでこのメソッドを呼び出す必要があります。最後に、メモリ不足エラーが発生します。画面間でメモリを解放しようとしています:

for (int i = 0; i < frame.getNumberOfFrames(); ++i){
     Drawable frame_rescue = frame.getFrame(i);
     if (frame_rescue instanceof BitmapDrawable) {
         ((BitmapDrawable)frame_rescue).getBitmap().recycle();
     }
     frame_rescue.setCallback(null);

問題は、リソースをもう一度使用しようとすると、「リサイクルされたビットマップを使用しようとしています」という他のエラーが発生することです。

誰もがメモリを解放する他の方法を知っていますか?

4

1 に答える 1

0

私は同じ問題を抱えていました、問題は実行可能でした。各アニメーションに独立したスレッドを作成すると、アクティビティを破棄してビューとコールバックをクリアした後も、animationDrawablesが存続します。したがって、独立したスレッドなしでアニメーションを開始すると、GCは未使用のリソースを収集できます。

于 2015-07-21T07:07:06.113 に答える