Android Training精要(五)讀取Bitmap對象實際的尺寸和類型
来源:互联网 发布:淘宝开店星密码靠谱吗 编辑:程序博客网 时间:2024/06/08 03:06
讀取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.
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 image 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; // 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; }
0 0
- Android Training精要(五)讀取Bitmap對象實際的尺寸和類型
- Android Training精要(六)如何防止Bitmap对象出现OOM
- Android Training精要(二)開啟ActionBar的Overlay模式
- Android Training精要(七)内存管理
- 【Android Training - 09】高效地显示Bitmap图片 [ Lesson 1 - 有效率地加载大尺寸的位图]
- Android Training - 支持不同的尺寸
- Android Training精要(一)ActionBar上级菜单导航图标
- Android Training精要(三)不同分辨率图片缩放倍数
- Android Training精要(四) Intent注意事项
- Android图片压缩(质量压缩和尺寸压缩 File, Stream和Bitmap)
- Android图片压缩(质量压缩和尺寸压缩 File, Stream和Bitmap)
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Gray Code
- OCP-1Z0-053-200题-195题-255
- Cocos2d-x教程 Lua脚本(1) 如何在Cocos2d-x中使用Lua脚本
- Android Training精要(四) Intent注意事项
- app后端设计(5)-- 表情的处理
- Android Training精要(五)讀取Bitmap對象實際的尺寸和類型
- 用php过滤文字中的表情字符
- 2014年值得关注的10个开源项目(下)
- Mac 精品软件
- Cloud Foundry中cloud_controller_ng架构简析
- 京东股权结构曝光
- 【COCOS2DX-LUA 脚本开发之一】在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途!
- Codeforces Round #227 (Div. 2)A. George and Sleep
- Codeforces Round #227 (Div. 2)B. George and Round