Android小白研究下Bitmap

在Android开发中咱们会在不少时候用到图片,这个时候咱们就须要用到Bitmap了,在Android开发中咱们使用的图都要转换成位图。可是咱们并不能经过Bitmap的构造方法来实例化一个Bitmap,官方提供了BitmapFactory来的静态方法来实例化Bitmap
当咱们使用Bitmap的时候很容易的就会致使应用程序的内存被消耗完,因此使用Bitmap的时候必定要作好优化。java

Config

在Bitmap类的内部有个Config枚举:优化

public enum Config {
      /**
      *每个像素存一个alpha通道的值,不存储颜色信息,适合作遮罩层。每一个像素占1byte。
      */
        ALPHA_8     (1),
        /**
        *每一个像素占2byte,只有RBG通道色值,Red占5bit,Green占6bit,Blue占5bit。
        RGB_565     (3),

        /**
        *由于质量过低,推荐使用ARGB_8888代替
        */
        @Deprecated
        ARGB_4444   (4),

        /**
        * 官方推荐使用
        *每个像素占4byte,每个通道(ARGB)占8bit(256个值).
        *灵活切画面质量好
        */
        ARGB_8888   (5);

    }

他的主要做用就是让咱们来设置画面的质量的,this

建立一个Bitmap

查看bitmap的源码咱们会看到一些createBitmap()方法,可是咱们建立Bitmap使用的最多的是BitmapFactory类的方法。spa

Bitmap bitmap = Bitmap.createBitmap(mWidth,mHeight, Bitmap.Config.ARGB_8888);

经过上面的方法能够建立一个空白的Bitmap。code

BitmapFactory

Options类

Options类用来设置解码的参数。其中主要有:对象

  • public Bitmap inBitmap;blog

  • public boolean inJustDecodeBounds; 若是设置为true,解析器将返回null,可是out...字段会设置上值。图片

  • public int inSampleSize; 若是设置的值>1那么解析器将会对原始图片进行抽取,返回一个更小的图片。解析器会使用2的次方,其余的值会向下转为最近的2的幂。ip

  • public boolean inDither;内存

  • public int inDensity; bitmpa使用的像素密度。

  • public int inTargetDensity;

  • public int inScreenDensity;

  • public boolean inScaled;

  • public int outWidth; 图片的宽度

  • public int outHeight; 图片的高度

  • public String outMimeType; 若是知道图片的MIME类型就设置上,若是不知道就设置为null

当咱们想对图片进行压缩的时候,咱们就要使用到Options。先看下代码:

BitmapFactory.Options options = new BitmapFactory.Options();
        //设置为true,先让解析器解析出图片的大小信息。
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher,options);
        //以后他们经过这个获得了图片大小信息的options来计算压缩的比例。
        options.inSampleSize = calculateInSampleSize(options,200,200);
        options.inJustDecodeBounds = false; //以后设置为false,为了获取到bitmap。
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher, options);//以后就能够经过这个options来获取本身指望的bitmap了。
        
        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;
    }

这里的calculateInSampleSize方法是官方文档中提供的用来计算InSampleSize值的方法。

decode方法

使用decode系列方法来获得bitmap对象
图片描述

Recycle

Bitmap再也不使用的时候记得将其回收,以避免内存泄漏

if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }

复制Bitmap

Bitmap有一个coty()方法用来复制一个Bitmap.源码以下

/**
     * Tries to make a new bitmap based on the dimensions of this bitmap,
     * setting the new bitmap's config to the one specified, and then copying
     * this bitmap's pixels into the new bitmap. If the conversion is not
     * supported, or the allocator fails, then this returns NULL.  The returned
     * bitmap initially has the same density as the original.
     *
     * @param config    The desired config for the resulting bitmap
     * @param isMutable True if the resulting bitmap should be mutable (i.e.
     *                  its pixels can be modified)
     * @return the new bitmap, or null if the copy could not be made.
     */
    public Bitmap copy(Config config, boolean isMutable) {
        checkRecycled("Can't copy a recycled bitmap");
        Bitmap b = nativeCopy(mNativePtr, config.nativeInt, isMutable);
        if (b != null) {
            b.setPremultiplied(mRequestPremultiplied);
            b.mDensity = mDensity;
        }
        return b;
    }

有时咱们建立的Bitmap是没法更改的。可是有时候咱们可能须要对Bitmap进行更改,这个时候咱们就可使用copy(Config,true)

相关文章
相关标签/搜索