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; }