public Bitmap html |
inBitmap android |
If set, decode methods that take the Options object will attempt to reuse this bitmap when loading content. windows |
public int post |
inDensity this |
The pixel density to use for the bitmap. spa |
public boolean .net |
inDither code |
If dither is true, the decoder will attempt to dither the decoded image. htm |
public boolean 圖片 |
inInputShareable |
This field works in conjuction with inPurgeable. |
public boolean |
inJustDecodeBounds |
If set to true, the decoder will return null (no bitmap), but the out… |
public boolean |
inMutable |
If set, decode methods will always return a mutable Bitmap instead of an immutable one. |
public boolean |
inPreferQualityOverSpeed |
If inPreferQualityOverSpeed is set to true, the decoder will try to decode the reconstructed image to a higher quality even at the expense of the decoding speed. |
publicBitmap.Config |
inPreferredConfig |
If this is non-null, the decoder will try to decode into this internal configuration. |
public boolean |
inPurgeable |
If this is set to true, then the resulting bitmap will allocate its pixels such that they can be purged if the system needs to reclaim memory. |
public int |
inSampleSize |
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. |
public boolean |
inScaled |
When this flag is set, if inDensity and inTargetDensity are not 0, the bitmap will be scaled to match inTargetDensity when loaded, rather than relying on the graphics system scaling it each time it is drawn to a Canvas. |
public int |
inScreenDensity |
The pixel density of the actual screen that is being used. |
public int |
inTargetDensity |
The pixel density of the destination this bitmap will be drawn to. |
public byte[] |
inTempStorage |
Temp storage to use for decoding. |
public boolean |
mCancel |
Flag to indicate that cancel has been called on this object. |
public int |
outHeight |
The resulting height of the bitmap, set independent of the state of inJustDecodeBounds. |
public String |
outMimeType |
If known, this string is set to the mimetype of the decoded image. |
public int |
outWidth |
The resulting width of the bitmap, set independent of the state of inJustDecodeBounds. |
這個表格是從android sdk文檔裏摘出來的,簡單看一下說明就明白是什麼意思了。
下面咱們回到咱們的主題上來:怎樣獲取圖片的大小?
思路很簡單:
首先咱們把這個圖片轉成Bitmap,而後再利用Bitmap的getWidth()和getHeight()方法就能夠取到圖片的寬高了。
新問題又來了,在經過BitmapFactory.decodeFile(String path)方法將突破轉成Bitmap時,遇到大一些的圖片,咱們常常會遇到OOM(Out Of Memory)的問題。怎麼避免它呢?
這就用到了咱們上面提到的BitmapFactory.Options這個類。
BitmapFactory.Options這個類,有一個字段叫作 inJustDecodeBounds 。SDK中對這個成員的說明是這樣的:
If set to true, the decoder will return null (no bitmap), but the out…
也就是說,若是咱們把它設爲true,那麼BitmapFactory.decodeFile(String path, Options opt)並不會真的返回一個Bitmap給你,它僅僅會把它的寬,高取回來給你,這樣就不會佔用太多的內存,也就不會那麼頻繁的發生OOM了。
示例代碼以下:
複製代碼
這段代碼以後,options.outWidth 和 options.outHeight就是咱們想要的寬和高了。
有了寬,高的信息,咱們怎樣在圖片不變形的狀況下獲取到圖片指定大小的縮略圖呢?
好比咱們須要在圖片不變形的前提下獲得寬度爲200的縮略圖。
那麼咱們須要先計算一下縮放以後,圖片的高度是多少
複製代碼
這樣雖然咱們能夠獲得咱們指望大小的ImageView
可是在執行BitmapFactory.decodeFile(path, options);時,並無節約內存。要想節約內存,還須要用到BitmapFactory.Options這個類裏的 inSampleSize 這個成員變量。
咱們能夠根據圖片實際的寬高和咱們指望的寬高來計算獲得這個值。
另外,爲了節約內存咱們還可使用下面的幾個字段: