Android Bitmap在不加载图片的前提获取宽高
来源:互联网 发布:cab格式软件 编辑:程序博客网 时间:2024/06/05 02:06
代码参考:
/** * 根据View(主要是ImageView)的宽和高来获取图片的缩略图 * @param path * @param viewWidth * @param viewHeight * @return */ private Bitmap decodeThumbBitmapForFile(String path, int viewWidth, int viewHeight){ BitmapFactory.Options options = new BitmapFactory.Options(); //设置为true,表示解析Bitmap对象,该对象不占内存 options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); //设置缩放比例 options.inSampleSize = computeScale(options, viewWidth, viewHeight); //设置为false,解析Bitmap对象加入到内存中 options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(path, options); } /** * 根据View(主要是ImageView)的宽和高来计算Bitmap缩放比例。默认不缩放 * @param options * @param width * @param height */ private int computeScale(BitmapFactory.Options options, int viewWidth, int viewHeight){ int inSampleSize = 1; if(viewWidth == 0 || viewHeight == 0){ return inSampleSize; } int bitmapWidth = options.outWidth; int bitmapHeight = options.outHeight; //假如Bitmap的宽度或高度大于我们设定图片的View的宽高,则计算缩放比例 if(bitmapWidth > viewWidth || bitmapHeight > viewWidth){ int widthScale = Math.round((float) bitmapWidth / (float) viewWidth); int heightScale = Math.round((float) bitmapHeight / (float) viewWidth); //为了保证图片不缩放变形,我们取宽高比例最小的那个 inSampleSize = widthScale < heightScale ? widthScale : heightScale; } return inSampleSize; }另外可参照:
BitmapFactory.Options options = new BitmapFactory.Options(); /** * 最关键在此,把options.inJustDecodeBounds = true; * 这里再decodeFile(),返回的bitmap为空,但此时调用options.outHeight时,已经包含了图片的高了 */ options.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/test.jpg", options); // 此时返回的bitmap为null /** *options.outHeight为原始图片的高 */ Log.e("Test", "Bitmap Height == " + options.outHeight);
根据这些方法,可以在实际加载Bitmap图片之前根据需要设置图片缩小为原来的(1/2,1/4,1/8,1/16..)
下面是Android源码中关于 inSampleSize的说明:
<span style="background-color: rgb(51, 255, 51);">public int inSampleSize</span>Added in API level 1If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder uses a final value based on powers of 2,<span style="background-color: rgb(51, 255, 51);"> any other value will be rounded down to the nearest power of 2</span>.
1 0
- Android Bitmap在不加载图片的前提获取宽高
- android在不加载图片的前提下获得图片的宽高
- android开发之在不加载图片的前提下获得图片的宽高
- 不加载图片 获取图片宽高
- Android不加载图片获取原图宽高
- android不加载图片获得图片的宽高
- android simpledraweeview 获取已经加载的图片bitmap
- 在不加载图片的情况下读取图片宽高
- android---不打开图片获取图片宽高
- 怎样在图片未加载完成之前预先获取图片的宽高
- Android:图片不加载到内存获取图片的大小
- android 网络下载图片,加载Bitmap 不oo控件
- Android不加载获取网络、本地图片的尺寸
- Bitmap 的图片加载
- Android获取bitmap图片的大小
- 举例说明android中Bitmap、ListView以及ImageView的综合使用---显示图片并获取图片的宽和高
- Android如何在初始化的时候获取加载的布局的宽高
- Android如何在初始化的时候获取加载的布局的宽高
- android利用post方式与web服务器通信
- leetcode 日经贴,Cpp code -Binary Tree Right Side View
- UESTC 852 -- 方老师与迷宫 (bfs)
- The 15th Zhejiang University Programming Contest
- 拷贝global memory,cudaMemcpyToSymbol 和cudaMemcpy函数是否有区别
- Android Bitmap在不加载图片的前提获取宽高
- 第六周 项目四-成员函数、友元函数和一般函数有区别
- 蓝桥杯省赛编程大题——骰子问题
- 腾讯2015实习面试
- js 打印 HTML 隐藏不想打印的元素
- ORACLE RAC数据库的启动与关闭
- 用DPM(Deformable Part Model,voc-release3.1)算法在INRIA数据集上训练自己的人体检测模型
- 《一个都不能死》 游戏开发 (物理引擎)
- C++定义的创建异常类