Android中Bitmap

来源:互联网 发布:中兴沈阳软件产业园 编辑:程序博客网 时间:2024/06/05 12:49

api地址

加载资源图片(通过BitmapFactory获取)

Bitmap不能new(不用jni情况下),用BitmapFactory获得bitmap

//这里先不用BitmapFactory.Options Bitmap bitmap= BitmapFactory.decodeResource(getActivity().getResources(),R.mipmap.jinsixiong);        Log.i(TAG, "with: "+bitmap.getWidth());        Log.i(TAG, "height: "+bitmap.getWidth());

结果–也就是说我的图片是1066*1066像素
这里写图片描述

10-12 14:08:16.371 9402-9402/doimage.szgroup.wy.doimage I/BitmapTestFragment: with: 106610-12 14:08:16.371 9402-9402/doimage.szgroup.wy.doimage I/BitmapTestFragment: height: 1038

加载图片设置option.inSampleSize

(图片压缩比例)
英文API:
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.
百度翻译:如果设置的值大于1,要求解码器子样的原始图像,返回一个较小的图像保存记忆。
我理解:请求值要大于1,对原始图像请求解析子图像,返回一个更小的图像保存

       BitmapFactory.Options options = new BitmapFactory.Options();        options.inSampleSize=2;        Bitmap bitmap= BitmapFactory.decodeResource(getActivity().getResources(),R.mipmap.jinsixiong,options);        Log.i(TAG, "with: "+bitmap.getWidth());        Log.i(TAG, "height: "+bitmap.getWidth());

也就是我的小老鼠缩小到了1/4

10-12 14:13:43.364 14341-14341/doimage.szgroup.wy.doimage I/BitmapTestFragment: with: 53310-12 14:13:43.364 14341-14341/doimage.szgroup.wy.doimage I/BitmapTestFragment: height: 519

option.inJustDecodeBounds

英文Api:
If set to true, the decoder will return null (no bitmap), but the out… fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
百度翻译:
如果设置为真,解码器将返回空(没有位图),但输出…字段仍然将被设置,允许调用方查询位图,而不必为其像素分配内存。
我理解:
如果设置为真则返回解析返回为空,bitmap为null,option取到值

  BitmapFactory.Options options = new BitmapFactory.Options();        options.inSampleSize=2;        options.inJustDecodeBounds=true;        Bitmap bitmap= BitmapFactory.decodeResource(getActivity().getResources(),R.mipmap.jinsixiong,options);        Log.i(TAG, "with: "+options.outWidth);        Log.i(TAG, "height: "+options.outHeight);        Log.i(TAG, "with: "+bitmap.getWidth());        Log.i(TAG, "height: "+bitmap.getWidth());

这段代码后两行会报错,因为设置options.inJustDecodeBounds=true所以bitmap是null但是options取到了值options.outWidth

这样就可以在不加载原图的情况下将他缩略

这里有个问题 不知道为什么这个options.outWidth
输出的结果为1279 不是1066变大了(在设置options.inJustDecodeBounds=true)
情况下

0 0