Android LruCache和Animationdrawable

时间:2016-07-25 12:13:08

标签: android

我有一个活动,在这个活动中,我做了一个动画。 但是有一个内存泄漏,那么我使用LruCache。

我的问题是如何在我的动画中设置LruCache的图像?

1 个答案:

答案 0 :(得分:0)

LruCache:

private LruCache<Integer, Bitmap> mBitmapCache;

public final static Integer[] resIdList = new Integer[] {
        R.drawable.image0, R.drawable.image1, R.drawable.image2,
        R.drawable.image3, R.drawable.image4, R.drawable.image5,
        R.drawable.image6, R.drawable.image7, R.drawable.image8,
        R.drawable.image10};


private void initializeCache() {

    final int memClass = ((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();

    mCache = new LruCache<Integer, Bitmap>(1024 * 1024 * (memClass / 4)) {

        @SuppressLint("NewApi")
        protected int sizeOf(String key, Bitmap value) {
            return value.getByteCount();
        }
    };

    int imgId = 0;

    for (int i = 0; i < resIdList.length; i++) {
        imgId = resIdList[i];
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), pingImgResId);
        mBitmapCache.put(pingImgResId, bitmap);
    }