zoukankan      html  css  js  c++  java
  • 对图片进行管理的工具类

    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.util.LruCache;
    
    /**
     * 对图片进行管理的工具类。
     * 
     * @author Tony
     */
    public class ImageLoader {
    
        /**
         * 图片缓存技术的核心类,用于缓存所有下载好的图片,在程序内存达到设定值时会将最少最近使用的图片移除掉。
         */
        private static LruCache<String, Bitmap> mMemoryCache;
    
        /**
         * ImageLoader的实例。
         */
        private static ImageLoader mImageLoader;
    
        private ImageLoader() {
            // 获取应用程序最大可用内存
            int maxMemory = (int) Runtime.getRuntime().maxMemory();
            int cacheSize = maxMemory / 8;
            // 设置图片缓存大小为程序最大可用内存的1/8
            mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    return bitmap.getByteCount();
                }
            };
        }
    
        /**
         * 获取ImageLoader的实例。
         * 
         * @return ImageLoader的实例。
         */
        public static ImageLoader getInstance() {
            if (mImageLoader == null) {
                mImageLoader = new ImageLoader();
            }
            return mImageLoader;
        }
    
        /**
         * 将一张图片存储到LruCache中。
         * 
         * @param key
         *            LruCache的键,这里传入图片的URL地址。
         * @param bitmap
         *            LruCache的键,这里传入从网络上下载的Bitmap对象。
         */
        public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
            if (getBitmapFromMemoryCache(key) == null) {
                mMemoryCache.put(key, bitmap);
            }
        }
    
        /**
         * 从LruCache中获取一张图片,如果不存在就返回null。
         * 
         * @param key
         *            LruCache的键,这里传入图片的URL地址。
         * @return 对应传入键的Bitmap对象,或者null。
         */
        public Bitmap getBitmapFromMemoryCache(String key) {
            return mMemoryCache.get(key);
        }
    
        public static int calculateInSampleSize(BitmapFactory.Options options,
                int reqWidth) {
            // 源图片的宽度
            final int width = options.outWidth;
            int inSampleSize = 1;
            if (width > reqWidth) {
                // 计算出实际宽度和目标宽度的比率
                final int widthRatio = Math.round((float) width / (float) reqWidth);
                inSampleSize = widthRatio;
            }
            return inSampleSize;
        }
    
        public static Bitmap decodeSampledBitmapFromResource(String pathName,
                int reqWidth) {
            // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(pathName, options);
            // 调用上面定义的方法计算inSampleSize值
            options.inSampleSize = calculateInSampleSize(options, reqWidth);
            // 使用获取到的inSampleSize值再次解析图片
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(pathName, options);
        }
    
    }
  • 相关阅读:
    信息安全系统设计基础第十二周学习总结
    day07-流程控制之while循环
    day07-深浅拷贝
    第一次小测
    day05-与用户交互与运算符
    day05学习笔记-垃圾回收机制
    day04学习笔记-变量
    day03-python-学习笔记
    P4323-[JSOI2016]独特的树叶【换根dp,树哈希】
    CF990G-GCD Counting【dfs】
  • 原文地址:https://www.cnblogs.com/androidsj/p/4762284.html
Copyright © 2011-2022 走看看