Bitmap位图采样及LurCache缓存

本文介绍了一种在Android应用中实现图片缓存的方法,并通过位图采样来减少图片占用的内存大小,以提高应用性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过位图采样,来缩小图片占用内存大小
通过缓存加载图片时来缓存中拿

public class MainActivity extends Activity {
    private ImageView iv;
    private LruCache<String, Bitmap> lc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取当前内存大小
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        int memoryClass = am.getMemoryClass();
        // 定义缓存的大小(内存的八分之一)
        int cachesize = memoryClass / 8 * 1024 * 1024;
        // 添加到缓存
        lc = new LruCache<String, Bitmap>(cachesize);
    }

    // 从缓存中获取bitmap
    public Bitmap getBitmapFormCache(String key) {
        return lc.get(key);
    }

    // 将图片添加到缓存
    public void addBitmptoCache(String key, Bitmap bt) {
        if (getBitmapFormCache(key) == null) {
            lc.put(key, bt);
        }
    }

    //通过ImageView监听加载
    public void show(View view) {
        String key = String.valueOf(R.drawable.ic_launcher);
        Bitmap bitmap = getBitmapFormCache(key);
        if (bitmap == null) {// 如果是空
            // 100指目标
            bitmap = setBiamap(getResources(), R.drawable.ic_launcher, 100, 100);
            addBitmptoCache(key, bitmap);// 如果没有添加到缓存中
        }
        iv.setImageBitmap(bitmap);
    }

    // 返回缩小以后的位图
    public Bitmap setBiamap(Resources res, int resid, int weighe, int height) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        // 只解析边界不加载内存
        options.inJustDecodeBounds = true;
        // 得到图片资源
        BitmapFactory.decodeResource(res, resid, options);
        // 设置采样比例
        options.inSampleSize = setsize(options, weighe, height);
        // 得到缩小后的图片加载到内存
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeResource(res, resid);
        return bitmap;
    }

    // 根据大小来设置采样比例
    private int setsize(BitmapFactory.Options options, int resultwieght,
            int resuktheight) {
        int w = options.outWidth;
        int h = options.outHeight;
        int round = 1;
        if (w > resuktheight || h > resultwieght) {
            if (w > h) {
                // 取小的进行设置比例
                round = Math.round((float) h / (float) resuktheight);
            } else {
                round = Math.round((float) w / (float) resultwieght);
            }
        }
        return round;
    }
}

《Android版本更新、热更新》系列课程视频

版本更新6.0,7.0统统搞定!!

热修复不在麻烦,再也不用担心上线后出bug!!

http://edu.youkuaiyun.com/course/detail/6523

http://edu.youkuaiyun.com/course/play/6523/131198

《Kotlin语法基础到实战开发》系列课程视频

http://edu.youkuaiyun.com/course/detail/6409?locationNum=7&fps=1&ref=srch&loc=1

http://edu.youkuaiyun.com/course/play/6409/123752

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值