在Android開發中咱們會在不少時候用到圖片,這個時候咱們就須要用到Bitmap
了,在Android開發中咱們使用的圖都要轉換成位圖。可是咱們並不能經過Bitmap
的構造方法來實例化一個Bitmap
,官方提供了BitmapFactory
來的靜態方法來實例化Bitmap
。
當咱們使用Bitmap
的時候很容易的就會致使應用程序的內存被消耗完,因此使用Bitmap
的時候必定要作好優化。java
在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的源碼咱們會看到一些createBitmap()
方法,可是咱們建立Bitmap
使用的最多的是BitmapFactory
類的方法。spa
Bitmap bitmap = Bitmap.createBitmap(mWidth,mHeight, Bitmap.Config.ARGB_8888);
經過上面的方法能夠建立一個空白的Bitmap。code
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系列方法來獲得bitmap對象
當Bitmap
再也不使用的時候記得將其回收,以避免內存泄漏
if (!bitmap.isRecycled()) { bitmap.recycle(); }
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)