zoukankan      html  css  js  c++  java
  • android 缓存Bitmap 使用内存缓存

    private LruCache<String, Bitmap> mMemoryCache;

    /** * 判断内存大小 设置位图的缓存空间 */ private void judgeMemory() { final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); final int cacheSize = maxMemory / 8; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getByteCount() / 1024; } }; }
        /**
         * 将位图添加到缓存中
         *
         * @param key    key值
         * @param bitmap 放入的位图
         */
        private void addBitmapToMemoryCache(String key, Bitmap bitmap) {
            if (getBitmapFromMemCache(key) == null) {
                mMemoryCache.put(key, bitmap);
            }
        }
        /**
         * 从缓存中得到位图
         *
         * @param key key值
         * @return 返回取出的位图
         */
        private Bitmap getBitmapFromMemCache(String key) {
            return mMemoryCache.get(key);
        }
        /**
         * 检索AsyncTask是否已经被分配到指定的ImageView:
         *
         * @param imageView 传入imageview控件
         * @return 返回已经被分配指定的imageview
         */
        private static BitmapWorkTask getBitmapWorkerTask(ImageView imageView) {
            if (imageView != null) {
                final Drawable drawable = imageView.getDrawable();
                if (drawable instanceof AsyncDrawable) {
                    final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
                    return asyncDrawable.getBitmapWorkTask();
                }
            }
            return null;
        }
        /**
         * 创建一个专用的Drawable的子类来储存任务的引用。
         */
        class AsyncDrawable extends BitmapDrawable {
            private final WeakReference bitmapWorkerTaskReference;
    
            public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkTask bitmapWorkTask) {
                super(res, bitmap);
                bitmapWorkerTaskReference = new WeakReference(bitmapWorkTask);
            }
    
            public BitmapWorkTask getBitmapWorkTask() {
                return (BitmapWorkTask) bitmapWorkerTaskReference.get();
            }
        }

    在doInBackground中添加到缓存

            protected Bitmap doInBackground(Integer... integers) {
                final Bitmap bitmap = decodeSampleBitmapFromResources(getResources(), integers[0], 100, 100);
                addBitmapToMemoryCache(String.valueOf(integers[0]), bitmap);
                return bitmap;
            }

    然后在oncreate中 加载,首先判断从缓存中加载。

    if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                } else {
                    final BitmapWorkTask task = new BitmapWorkTask(imageView);
                    final AsyncDrawable asyncDrawable = new AsyncDrawable(getResources(), decodeSampleBitmapFromResources(getResources(), resId, imageView.getMaxWidth(), imageView.getMaxHeight()), task);
                    imageView.setImageDrawable(asyncDrawable);
                    task.execute(resId);
                }
  • 相关阅读:
    什么是长尾关键词?【摘】
    Visual Studio 2010已经发布,简单列举一下新特性,重点学习一下
    增加反向链接的35个技巧【摘】
    鼠标移动上变为手的样式的css,cursor: pointer或者CURSOR: hand
    百度空间地址栏的logo咋成了迅雷的logo了,被攻击后遗症?
    百度优化技巧和方法【摘】
    asp入门之分页
    asp入门之操作数据库
    asp入门之简单介绍
    asp入门之在线编辑器(QQMail HtmlEditor 菜刀版 1.2版)改进版
  • 原文地址:https://www.cnblogs.com/android-host/p/5319888.html
Copyright © 2011-2022 走看看