Bitmap类、BitmapFactory及BitmapFactory类中的常用方法

1.Bitmap

1.1非静态方法

  • public void recycle()——回收位图占用的内存空间,把位图标记为Dead
  • public final boolean isRecycled() ——判断位图内存是否已释放
  • public final int getWidth()——获取位图的宽度
  • public final int getHeight()——获取位图的高度
  • public final boolean isMutable()——图片是否可修改
  • public int getScaledWidth(Canvas canvas)——获取指定密度转换后的图像的宽度
  • public int getScaledHeight(Canvas canvas)——获取指定密度转换后的图像的高度
  • public boolean compress(CompressFormat format, int quality, - - --OutputStream stream)——按指定的图片格式以及画质,将图片转换为输出流。
  • format:Bitmap.CompressFormat.PNG或Bitmap.CompressFormat.JPEG quality:画质,0-100.0表示最低画质压缩,100以最高画质压缩。对于PNG等无损格式的图片,会忽略此项设置

1.2 静态方法

  • public void recycle()——回收位图占用的内存空间,把位图标记为Dead
  • public final boolean isRecycled() ——判断位图内存是否已释放
  • public final int getWidth()——获取位图的宽度
  • public final int getHeight()——获取位图的高度
  • public final boolean isMutable()——图片是否可修改
  • public int getScaledWidth(Canvas canvas)——获取指定密度转换后的图像的宽度
  • public int getScaledHeight(Canvas canvas)——获取指定密度转换后的图像的高度
  • public boolean compress(CompressFormat format, int quality, - - --OutputStream stream)——按指定的图片格式以及画质,将图片转换为输出流。
  • format:Bitmap.CompressFormat.PNG或Bitmap.CompressFormat.JPEG quality:画质,0-100.0表示最低画质压缩,100以最高画质压缩。对于PNG等无损格式的图片,会忽略此项设置

2.BitmapFactory

2.1 BitmapFactory创建Bitmap的几种方法说明

1.public static Bitmap decodeByteArray (byte[] data, int offset, int length, BitmapFactory.Options opts)
参数
data    压缩图像数据的字节数组
offset  图像数据偏移量,用于解码器开始从哪儿解析.
length  字节数,以偏移量开始,去解析
opts    可以为空,控制采样率和实付图像完全被解码的选项,或者仅仅返回大小
返回值
    返回解码后的位图,或者如果图像数据不能被解码返回为空,或者 如果选项不是空,如果选项要求仅仅返回大小(opts.outWidth and opts.outHeight)。
从指定的字节数组中解码一个不可变的位图。

2. public static Bitmap decodeByteArray (byte[] data, int offset, int length)
参数
data 压缩图像数据的字节数组
offset 图像数据偏移量,用于解码器开始从哪儿解析.
length 字节数,以偏移量开始,去解析
返回值
返回解码后的位图,或者如果图像数据不能被解码返回为空
从指定的字节数组中解码一个不可变的位图。

 

3. public static Bitmap decodeFile (String pathName)
参数
pathName    解码文件的全路径名
返回值
    返回结果是解码的位图,或者如果不能解码则返回空。
从文件中解码生成一个位图。如果支付的文件名为空,或者不能解码出一个位图,方法将返回空。
4. public static Bitmap decodeFile (String pathName, BitmapFactory.Options opts)
参数
pathName    解码文件的全路径名
opts            可以为空,控制采样率和实付图像完全被解码的选项,或者仅仅返回大小
Returns
    返回解码后的位图,或者如果图像数据不能被解码返回为空,或者 如果选项不是空,如果选项要求仅仅返回大小(opts.outWidth and opts.outHeight)。
从文件中解码生成一个位图。如果支付的文件名为空,或者不能解码出一个位图,方法将返回空。
5. public static Bitmap decodeFileDescriptor (FileDescriptor fd)
参数
fd  包含解码位图数据的文件路径
返回值
   返回解码的位图或者空。
6. public static Bitmap decodeFileDescriptor (FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts)
参数
fd          包含解码位图数据的<span style="font-family: Arial, Helvetica, sans-serif;">文件路径</span>
outPadding  如果不为空,返回矩形的内边距如果位图存在,否则设置内边距为(-1,-1,-1,-1).如果没有位图返回空,内边距不改变
opts            可以为空,控制采样率和实付图像完全被解码的选项,或者仅仅返回大小
返回值
    返回解码的位图或者空。
7. public static Bitmap decodeResource (Resources res, int id, BitmapFactory.Options opts)
参数
res     包含图像数据的资源对象
id  图像数据的资源的id
opts    可以为空,控制采样率和实付图像完全被解码的选项,或者仅仅返回大小
返回值
8. public static Bitmap decodeResourceStream (Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts)
从输入流中解码一个新位图。输入了获得资源,我们可以缩放位图。
9. public static Bitmap decodeResource (Resources res, int id)
参数
res         包含图像数据的资源对象
id      图像数据的资源的id
返回值
    返回解码后的位图,或者如果图像数据不能被解码返回为空
从输入流中解码位图。与decodeResource(Resources, int, android.graphics.BitmapFactory.Options)当Options是空时同义,
10. public static Bitmap decodeResource (Resources res, int id, BitmapFactory.Options opts)
参数
res     包含图像数据的资源对象
id  图像数据的资源的id
opts    可以为空,控制采样率和实付图像完全被解码的选项,或者仅仅返回大小
返回值
   返回解码后的位图,或者如果图像数据不能被解码返回为空,或者 如果选项不是空,如果选项要求仅仅返回大小(opts.outWidth and opts.outHeight)。
从资源中解码一个位图。与decodeResourceStream(Resources, TypedValue, InputStream, Rect, BitmapFactory.Options)同义.
11. public static Bitmap decodeStream (InputStream is, Rect outPadding, BitmapFactory.Options opts)
参数
is          持有原始数据用于解码位图的输入流
outPadding  如果不为空,返回矩形的内边距如果位图存在,否则设置内边距为(-1,-1,-1,-1).如果没有位图返回空,内边距不改变
opts            可以为空,控制采样率和实付图像完全被解码的选项,或者仅仅返回大小
Returns
    返回解码后的位图,或者如果图像数据不能被解码返回为空,或者 如果选项不是空,如果选项要求仅仅返回大小(opts.outWidth and opts.outHeight)。
从输入流中解码一个位图。如果输入了为空,或者不能解码位图,方法返回空。流的位置觉得解码数据从哪儿读取。
12. public static Bitmap decodeStream (InputStream is)
Decode an input stream into a bitmap. If the input stream is null, or cannot be used to decode a bitmap, the function returns null. The stream's position will be where ever it was after the encoded data was read.
参数
is  持有原始数据用于解码位图的输入流
返回值
    返回解码后的位图,或者如果图像数据不能被解码返回为空

2.2 BitmapFactory.Options说明以及使用

位图在内存中的占用空间计算:
ALPHA_8:每个像素占用1byte内存
ARGB_4444:每个像素占用2byte内存
ARGB_8888:每个像素占用4byte内存
RGB_565:每个像素占用2byte内存
举个例子,如果一个图片的分辨率是1024*768,采用ARGB_8888,那么占用的空间就是1024*768*4=3MB

通过BitmapFactory.Options降低bitmap加载到内存中的内存及改变色彩设置防止OOM

BitmapFactory.Options的常用参数

inJustDecodeBounds:

如果将这个值置为true,那么在解码的时候将不会返回bitmap,只会返回这个bitmap的尺寸。这个属性的目的是,如果你只想知道一个bitmap的尺寸,但又不想将其加载到内存时。这是一个非常有用的属性。

inSampleSize:

缩放比例。当它小于1的时候,将会被当做1处理,这个参数需要是2的幂函数。例如,width=100,height=100,inSampleSize=2,那么就会将bitmap处理为,width=50,height=50,宽高各降为1 / 2,像素数降为1 / 4。

inPreferredConfig:

这个值是设置色彩模式,默认值是ARGB_8888,在这个模式下,一个像素点占用4bytes空间,一般对透明度不做要求的话,一般采用RGB_565模式,这个模式下一个像素点占用2bytes。

outWidth和outHeight:

表示这个Bitmap的宽和高,一般和inJustDecodeBounds一起使用来获得Bitmap的宽高,但是不加载到内存。

从资源图片加载一个大图,并显示出来

BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeResource(getResources(),R.drawable.big6,options);
int ratio=calculateInSampleSize(options,1080,1920);//计算压缩比
options.inSampleSize=ratio;
options.inPreferredConfig= Bitmap.Config.RGB_565;
options.inJustDecodeBounds=false;
Log.e(TAG, "onCreate: ratio:"+ratio );
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.big6,options);
img.setImageBitmap(bitmap);

计算图片压缩比方法

  /**
     * 计算压缩值
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
        int width=options.outWidth;
        int height=options.outHeight;
        int inSampleSize=1;
        if(width>reqWidth||height>reqHeight){
            inSampleSize++;
            int widthRatio=reqWidth/width;
            int heightRatio=reqHeight/height;
            int ratio=Math.min(widthRatio,heightRatio);
            while(ratio>inSampleSize){
                inSampleSize*=2;
            }

        }
        return inSampleSize;
    }
原文地址:https://www.cnblogs.com/wangjiaghe/p/7358445.html