当前位置:   article > 正文

Bitmap详解与Bitmap的内存优化_bitmap的数据有很多ff是什么意思

bitmap的数据有很多ff是什么意思

感觉这里的排版看着更舒服些 Bitmap详解与Bitmap的内存优化

一、Bitmap

Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。
常用方法:
+ 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等无损格式的图片,会忽略此项设置。
stream: OutputStream中写入压缩数据。
return: 是否成功压缩到指定的流。
+ public static Bitmap createBitmap(Bitmap src)  //以src为原图生成不可变得新图像
+ public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) //以src为原图,创建新的图像,指定新图像的高宽以及是否可变。
+ public static Bitmap createBitmap(int width, int height, Config config) //创建指定格式、大小的位图
+ public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) //以source为原图,创建新的图片,指定起始坐标以及新图像的高宽。


二、BitmapFactory工厂类:

Option 参数类:
+ public boolean inJustDecodeBounds //如果设置为true,不获取图片,不分配内存,但会返回图片的高度宽度信息。
如果将这个值置为true,那么在解码的时候将不会返回bitmap,只会返回这个bitmap的尺寸。这个属性的目的是,如果你只想知道一个bitmap的尺寸,但又不想将其加载到内存时。这是一个非常有用的属性。
+ public int inSampleSize //图片缩放的倍数
这个值是一个int,当它小于1的时候,将会被当做1处理,如果大于1,那么就会按照比例(1 / inSampleSize)缩小bitmap的宽和高、降低分辨率,大于1时这个值将会被处置为2的倍数。例如,width=100,height=100,inSampleSize=2,那么就会将bitmap处理为,width=50,height=50,宽高降为1 / 2,像素数降为1 / 4。
+ public int outWidth //获取图片的宽度值
+ public int outHeight //获取图片的高度值
表示这个Bitmap的宽和高,一般和inJustDecodeBounds一起使用来获得Bitmap的宽高,但是不加载到内存。
+ public int inDensity //用于位图的像素压缩比
+ public int inTargetDensity //用于目标位图的像素压缩比(要生成的位图)
+ public byte[] inTempStorage  //创建临时文件,将图片存储
+ public boolean inScaled //设置为true时进行图片压缩,从inDensity到inTargetDensity
+ public boolean inDither  //如果为true,解码器尝试抖动解码
+ public Bitmap.Config inPreferredConfig  //设置解码器
这个值是设置色彩模式,默认值是ARGB_8888,在这个模式下,一个像素点占用4bytes空间,一般对透明度不做要求的话,一般采用RGB_565模式,这个模式下一个像素点占用2bytes。
+ public String outMimeType  //设置解码图像
+ public boolean inPurgeable //当存储Pixel的内存空间在系统内存不足时是否可以被回收
+ public boolean inInputShareable  //inPurgeable为true情况下才生效,是否可以共享一个InputStream
+ public boolean inPreferQualityOverSpeed  //为true则优先保证Bitmap质量其次是解码速度
+ public boolean inMutable  //配置Bitmap是否可以更改,比如:在Bitmap上隔几个像素加一条线段
+ public int inScreenDensity  //当前屏幕的像素密度

工厂方法:
+ public static Bitmap decodeFile(String pathName, Options opts)  //从文件读取图片
+ public static Bitmap decodeFile(String pathName)
+ public static Bitmap decodeStream(InputStream is)  //从输入流读取图片
+ public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts)
+ public static Bitmap decodeResource(Resources res, int id)  //从资源文件读取图片
+ public static Bitmap decodeResource(Resources res, int id, Options opts)
+ public static Bitmap decodeByteArray(byte[] data, int offset, int length)  //从数组读取图片
+ public static Bitmap decodeByteArray(byte[] data, int offset, int length, Options opts)
+ public static Bitmap decodeFileDescriptor(FileDescriptor fd) //从文件读取文件 与decodeFile不同的是这个直接调用JNI函数进行读取 效率比较高
+ public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts)

* Bitmap.Config inPreferredConfig :*
枚举变量 (位图位数越高代表其可以存储的颜色信息越多,图像越逼真,占用内存越大)
+ public static final Bitmap.Config ALPHA_8  //代表8位Alpha位图 每个像素占用1byte内存
+ public static final Bitmap.Config ARGB_4444  //代表16位ARGB位图 每个像素占用2byte内存
+ public static final Bitmap.Config ARGB_8888  //代表32位ARGB位图 每个像素占用4byte内存
+ public static final Bitmap.Config RGB_565  //代表8位RGB位图 每个像素占用2byte内存

Android中一张图片(BitMap)占用的内存主要和以下几个因数有关:图片长度,图片宽度,单位像素占用的字节数。一张图片(BitMap)占用的内存=图片长度*图片宽度*单位像素占用的字节数。


三、Bitmap加载方式

Bitmap的加载方式有Resource资源加载、本地(SDcard)加载、网络加载等加载方式。

1. 从本地(SDcard)文件读取
  • 方式一
/**
     * 获取缩放后的本地图片
     *
     * @param filePath 文件路径
     * @param width    宽
     * @param height   高
     * @return
     */
    public static Bitmap readBitmapFromFile(String filePath, int width, int height) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        float srcWidth = options.outWidth;
        float srcHeight = options.outHeight;
        int inSampleSize = 1;

        if (srcHeight > height || srcWidth > width) {
            if (srcWidth > srcHeight) {
                inSampleSize = Math.round(srcHeight / height);
            } else {
                inSampleSize = Math.round(srcWidth / width);
            }
        }

        options.inJustDecodeBounds = false;
        options.inSampleSize = inSampleSize;

        return BitmapFactory.decodeFile(filePath, options);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 方式二 (效率高于方式一)
/**
     * 获取缩放后的本地图片
     *
     * @param filePath 文件路径
     * @param width    宽
     * @param height   高
     * @return
     */
    public static Bitmap readBitmapFromFileDescriptor(String filePath, int width, int height) {
        try {
            FileInputStream fis = new FileInputStream(filePath);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fis.getFD(), null, options);
            float srcWidth = options.outWidth;
            
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/73670
推荐阅读
相关标签
  

闽ICP备14008679号