<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 93,  comments - 2,  trackbacks - 0

    /**
    * @param 將字節數組轉換為ImageView可調用的Bitmap對象

     * @param bytes

     * @param opts

     * @return Bitmap

     */

     public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) {

         if (bytes != null)

            if (opts != null
                 return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,  opts); 
            else

                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
         return null;

    }

    這里我們主要來介紹一個BitmapFactory.Options這個類
        

    BitmapFactory.Options這個類的詳解如下:

    public Bitmap

    inBitmap

    If set, decode methods that take the Options object will attempt to reuse this bitmap when loading content.

    public int

    inDensity

    The pixel density to use for the bitmap.

    public boolean

    inDither

    If dither is true, the decoder will attempt to dither the decoded image.

    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了。
    示例代碼如下:

    1. BitmapFactory.Options options = new BitmapFactory.Options();
    2. options.inJustDecodeBounds = true;
    3. Bitmap bmp = BitmapFactory.decodeFile(path, options);
    4. /* 這里返回的bmp是null */

    復制代碼

    這段代碼之后,options.outWidth 和 options.outHeight就是我們想要的寬和高了。

    有了寬,高的信息,我們怎樣在圖片不變形的情況下獲取到圖片指定大小的縮略圖呢?
    比如我們需要在圖片不變形的前提下得到寬度為200的縮略圖。
    那么我們需要先計算一下縮放之后,圖片的高度是多少

    1. /* 計算得到圖片的高度 */
    2. /* 這里需要主意,如果你需要更高的精度來保證圖片不變形的話,需要自己進行一下數學運算 */
    3. int height = options.outHeight * 200 / options.outWidth;
    4. options.outWidth = 200;
    5. options.outHeight = height;
    6. /* 這樣才能真正的返回一個Bitmap給你 */
    7. options.inJustDecodeBounds = false;
    8. Bitmap bmp = BitmapFactory.decodeFile(path, options);
    9. image.setImageBitmap(bmp);

    復制代碼

    這樣雖然我們可以得到我們期望大小的ImageView
    但是在執行BitmapFactory.decodeFile(path, options);時,并沒有節約內存。
    要想節約內存,還需要用到BitmapFactory.Options這個類里的 inSampleSize 這個成員變量。
    我們可以根據圖片實際的寬高和我們期望的寬高來計算得到這個值。

    1. inSampleSize = options.outWidth / 200;

    另外,為了節約內存我們還可以使用下面的幾個字段:

    1. options.inPreferredConfig = Bitmap.Config.ARGB_4444;    // 默認是Bitmap.Config.ARGB_8888
    2. /* 下面兩個字段需要組合使用 */
    3. options.inPurgeable = true;
    4. options.inInputShareable = true;

        

    posted on 2013-05-29 11:36 Terry Zou 閱讀(2829) 評論(0)  編輯  收藏 所屬分類: Tomcat+Eclipse
    <2013年5月>
    2829301234
    567891011
    12131415161718
    19202122232425
    2627282930311
    2345678

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊

    收藏夾

    Java

    搜索

    •  

    最新隨筆

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久国产精品成人片免费| 一级做受视频免费是看美女| 国产成人AV免费观看| 亚洲国产婷婷香蕉久久久久久| 国产免费黄色无码视频| 在线日韩av永久免费观看| 亚洲精品高清一二区久久| 激情婷婷成人亚洲综合| 国产一级淫片a免费播放口之 | 亚洲无码黄色网址| 免费的黄色的网站| 4455永久在线观免费看| 亚洲福利一区二区精品秒拍| 91香蕉成人免费网站| 在线亚洲精品自拍| 精品视频一区二区三区免费| 免费看片A级毛片免费看| 亚洲午夜国产精品无码老牛影视| 亚洲精品在线播放| 免费观看成人毛片a片2008| 亚洲AV永久精品爱情岛论坛| 亚洲午夜无码久久久久软件| 国产成人AV片无码免费| 亚洲精品一级无码中文字幕| 尤物视频在线免费观看| 日韩精品无码人妻免费视频| MM1313亚洲精品无码久久| 亚洲男人第一无码aⅴ网站| 水蜜桃视频在线观看免费播放高清 | av在线亚洲欧洲日产一区二区| 国产在线精品一区免费香蕉| 免费精品一区二区三区在线观看| 亚洲啪AV永久无码精品放毛片| mm1313亚洲精品无码又大又粗 | 成人a免费α片在线视频网站 | 大胆亚洲人体视频| 黄色免费在线网站| 中文字幕乱码亚洲无线三区| 又大又硬又爽免费视频| 国产一区二区三区免费观在线| 亚洲第一成年人网站|