Android Training精要(五)讀取Bitmap對象實際的尺寸和類型
讀取Bitmap對象實際的尺寸和類型
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
計算inSampleSize來控制縮放比例,值最好設為2的指數.例如inSampleSize為2,則長和寬縮短為原來的1/2,這樣整個圖片縮小為原來的1/4.
完整的從資源加載Bitmap的代碼
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options);
}public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of imagefinal 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; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize;}
转载于:https://www.cnblogs.com/krislight1105/p/3748336.html
Android Training精要(五)讀取Bitmap對象實際的尺寸和類型相关推荐
- Android Training精要(六)如何防止Bitmap对象出现OOM
1.使用AsyncTask異步加載bitmap圖片避免OOM: class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> ...
- matlab读取igs文件,Matlab 等間隔讀取IGS對流層天頂延遲(ZPD)文件
IGS(International GNSS Service)提供對流層天頂延遲(ZPD)文件,可從cddis分析中心下載(ftp://cddis.gsfc.nasa.gov/gps/products ...
- Android进阶知识(二十五):Bitmap简介及其高效加载
Android进阶知识(二十五):Bitmap简介及其高效加载 一.Bitmap Bitmap代表一个位图,在Android中指的是一张图片,可以是png.jpg等格式的图片.BitmapDraw ...
- android ui 最新教程,Android更新UI的五种方式,androidui五种
Android更新UI的五种方式,androidui五种handler.post activity.runOnUiThread view.post handler+Thread AsyncTask 例 ...
- 列表怎么有限的初始化为零_《零基础学习Android开发》第五课 类与面向对象编程1-1...
视频:<零基础学习Android开发>第五课 类与面向对象编程1-1 类的定义.成员变量.构造方法.成员方法 一.从数据与逻辑相互关系审视代码 通过前面的课程,我们不断接触Java语言的知 ...
- Android开发笔记(五十七)录像录音与播放
媒体录制MediaRecorder MediaRecorder是Android自带的录制工具,通过操纵摄像头和麦克风完成媒体录制,既可录制视频,也可单独录制音频.其中对摄像头Camera的介绍参见&l ...
- Android开发笔记(五十四)数据共享接口ContentProvider
ContentProvider 前面几节介绍了进程间通信的几种方式,包括消息包级别的Messenger.接口调用级别的AIDL.启动页面/服务级别的Notification,还有就是本节这个数据库级别 ...
- 《android开发艺术探索》笔记之Bitmap的加载和Cache
<Android开发艺术探索>笔记之Bitmap的加载和Cache<一> 我放暑假前,就在图书馆借了一本<Android开发艺术探索>,这也是我看到很多人推荐的.之 ...
- Android kotlin 将Base64字符串转换成Bitmap,并在jetpack compose的Image控件中显示
Android kotlin 将Base64字符串转换成Bitmap 前言 代码 将Base64字符串转换成Bitmap 在jetpack compose的Image控件中显示Bitmap 完事 前言 ...
- [Android开发]Android更新UI的五种方式
Android更新UI的五种方式: 1.handler.post 2.activity.runOnUiThread 3.view.post 4.handler+Thread 5.AsyncTask 下 ...
最新文章
- iOS标准时间与时间戳相互转换
- 7.Windows线程切换_时钟中断切换
- fasttext的基本使用 java 、python为例子
- Lind.DDD.RedisClient~对StackExchange.Redis调用者的封装及多路复用技术
- (五)官方Neo4j 3.3.9 Java API例子
- javascript调用服务端验证控件
- c语言学习-使用指针求一个字符串的长度
- 解决mysql大小写敏感问题
- jQuery图片垂直滚动焦点图
- 研究WPS Office的LINUX postinst脚本,发现了错误,应该学习柳氏风格
- 易语言linux时间戳转换,易语言unix时间戳转易语言格式源码
- mysql 收货地址表_会员收货地址 - 数据库设计 - 数据库表结构 - 果创云
- Win8串口驱动完美支持
- vue如何集成阿里云视频服务组件(aliplayer)视频功能是使用el-dialog 弹出aliplayer播放
- 百度BAE平台使用(一)注册开发者BAE环境
- Ish iphone和iPad上的终端模拟器-ISO-linux-Shell
- 嵌入式系统设计师学习笔记二十八:嵌入式程序设计③——高级程序设计语言
- Week2:区分己烷的同分异构体
- 第一章 开启安卓之路
- java 爬取快递100 快递信息