Bitmap的高效加载
发布日期:2021-10-07 06:13:02
浏览次数:1
分类:技术文章
本文共 1151 字,大约阅读时间需要 3 分钟。
在任玉刚大神的书上看到的这个方法。其实Android API上也给了这个类似的代码。用来预防图片OOM的发生
public class BitmapUtil{ public BitmapUtil(){ } public static Bitmap decodeSampleedBitmapFromResource(Resources res,int resId,int reqWidth,int reqHeight){ final Options options = new Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId,options); options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight); options.inJustDecodeBounds = false; options.inPreferredConfig = Config.ARGB_8888; options.inDither = true; return BitmapFactory.decodeResource(res, resId, options); } private static int calculateInSampleSize(Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height>reqHeight ||width>reqWidth) { final int halfHeight = height /2; final int halfWidth = width /2; while (halfHeight /inSampleSize >= reqHeight && (halfWidth / inSampleSize >= reqWidth)) { inSampleSize *= 2; } } return inSampleSize; }}
转载地址:https://blog.csdn.net/mengmengkenanjun/article/details/52955124 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月21日 06时20分35秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Unity可视化编程插件: Bolt,可以像UE4的蓝图那样啦
2019-04-27
Android的.dex、.odex与.oat文件扫盲
2019-04-27
Unity移动应用如何在Bugly上查看崩溃堆栈
2021-06-30
unity3D 在屏幕边框创建碰撞框
2021-06-30
xml中常用的转义符
2021-06-30
关于MSDK的几个难点
2021-06-30
使用UnityEditor做工具
2021-06-30
Visual Studio我常用的快捷键
2021-06-30
写C# dll供Unity调用
2021-06-30
Linux制作run安装包
2021-06-30
一分钟学会C#解析XML
2021-06-30
unity AssetBundle的资源管理
2021-06-30
【转】Unity中HideInInspector和SerializeField一起使用
2021-06-30
单例模板类
2021-06-30
Unity与java相互调用
2021-06-30
android截屏代码
2021-06-30
unity NGUI图文混排
2021-06-30
Unity项目优化
2021-06-30
Unity3D Shader 入门
2021-06-30