对于6220816字节分配

对于6220816字节分配

我正在尝试使用15到20张图像在固定时间一张一张地显示在动态壁纸背景上,但是我面临着增长堆问题(对于6220816字节分配,增长堆(frag情况)为8.987MB)无法通过这个
任何帮助请求。

这是我的代码

公共类LiveWallpaperService扩展了WallpaperService {

private float x;
private int y;
private int angle;
private int speed;

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public Engine onCreateEngine() {
    stopSelf();
    return new MyWallpaperEngine();
}

class MyWallpaperEngine extends Engine {

    private final Handler handle = new Handler();
    private final Runnable drawRunner = new Runnable() {

        @Override
        public void run() {
            draw();
        }
    };

    private Boolean visble = true;
    public Bitmap mBackgroundImage;
    public String name;
    int[] mImagesArray;
    private int mImagesArrayIndex = 0;

    MyWallpaperEngine() {

        mImagesArray = new int[] { R.drawable.love_1_mini,
                R.drawable.love_2_mini, R.drawable.love_3_mini,
                R.drawable.love_4_mini, R.drawable.love_5_mini,

                R.drawable.love_6_mini, R.drawable.love_7_mini,
                R.drawable.love_8_mini, R.drawable.love_9_mini,
                R.drawable.love_10_mini,

                R.drawable.love_11_mini, R.drawable.love_12_mini,
                R.drawable.love_13_mini, R.drawable.love_14_mini,
                R.drawable.love_15_mini, R.drawable.love_16_mini,
                R.drawable.love_17_mini, R.drawable.love_18_mini,
                R.drawable.love_19_mini, R.drawable.love_20_mini };

        if (mImagesArrayIndex == 0) {
            x = -330; // initialize x position
            y = 0; // initialize y position
        } else {
            x = -330; // initialize x position
            y = 0; // initialize y position
        }

    }

    @Override
    public Bundle onCommand(String action, int x, int y, int z,
            Bundle extras, boolean resultRequested) {
        return super.onCommand(action, x, y, z, extras, resultRequested);
    }

    @Override
    public void onCreate(SurfaceHolder surfaceHolder) {
        super.onCreate(surfaceHolder);
    }

    @Override
    public void onOffsetsChanged(float xOffset, float yOffset,
            float xOffsetStep, float yOffsetStep, int xPixelOffset,
            int yPixelOffset) {
        draw();
    }

    @Override
    public void onSurfaceCreated(SurfaceHolder holder) {
        super.onSurfaceCreated(holder);
        this.visble = false;

    }

    @Override
    public void onTouchEvent(MotionEvent event) {

    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        this.visble = visible;
        if (visible) {
            handle.post(drawRunner);
        } else {
            handle.removeCallbacks(drawRunner);
        }

        super.onVisibilityChanged(visible);
    }

    private void incrementCounter() {
        mImagesArrayIndex++;
        if (mImagesArrayIndex >= mImagesArray.length) {
            mImagesArrayIndex = 0;

        }

    }

    @SuppressWarnings("deprecation")
    void draw() {

        int count = mImagesArray[mImagesArrayIndex];
        final SurfaceHolder holder = getSurfaceHolder();
        Canvas c = null;
        int scale = 2;
        Resources res = getResources();
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inPreferredConfig = Bitmap.Config.RGB_565;
        o2.inSampleSize = scale;
        o2.inPurgeable = true;
        o2.inInputShareable = true;
        Bitmap image = BitmapFactory.decodeResource(res, count, o2);

        Calendar mCalendar = Calendar.getInstance();
        int h = mCalendar.get(Calendar.HOUR);
        int m = mCalendar.get(Calendar.MINUTE);
        int s = mCalendar.get(Calendar.SECOND);

        if (s == 10) {
            mImagesArrayIndex = 0;
        }

        try {
            Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
            c = holder.lockCanvas();
            c.drawColor(Color.RED);

            if (c != null) {
                int a = 0;
                int counting = 0;
                if (counting == a) {
                    a++;
                    counting++;

                    c.drawBitmap(image, x, y, paint);

                    int widthhhh = c.getWidth();

                    x = (float) (x + 10.10);

                    if (x > widthhhh + 400) {

                        image.recycle();
                        image = null;
                        incrementCounter();

                    }

                }

                paint.setColor(Color.RED);

            }

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {

            try {
                if (c != null) {
                    holder.unlockCanvasAndPost(c);
                }
                handle.removeCallbacks(drawRunner);
                if (visble) {
                    handle.postDelayed(drawRunner, 10);
                }
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }
    }

}


}

最佳答案

位图图像= BitmapFactory.decodeResource(getResources(),mImagesArray [mImagesArrayIndex]);


完成后,调用image.recycle()(请参阅javadoc)。

08-17 17:45