<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 將字節(jié)數(shù)組轉(zhuǎn)換為ImageView可調(diào)用的Bitmap對(duì)象

     * @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;

    }

    這里我們主要來(lái)介紹一個(gè)BitmapFactory.Options這個(gè)類
        

    BitmapFactory.Options這個(gè)類的詳解如下:

    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.

    這個(gè)表格是從android sdk文檔里摘出來(lái)的,簡(jiǎn)單看一下說(shuō)明就明白是什么意思了。
    下面我們回到我們的主題上來(lái):怎樣獲取圖片的大小?
    思路很簡(jiǎn)單:
    首先我們把這個(gè)圖片轉(zhuǎn)成Bitmap,然后再利用Bitmap的getWidth()和getHeight()方法就可以取到圖片的寬高了。
    新問(wèn)題又來(lái)了,在通過(guò)BitmapFactory.decodeFile(String path)方法將突破轉(zhuǎn)成Bitmap時(shí),遇到大一些的圖片,我們經(jīng)常會(huì)遇到OOM(Out Of Memory)的問(wèn)題。怎么避免它呢?
    這就用到了我們上面提到的BitmapFactory.Options這個(gè)類。

    BitmapFactory.Options這個(gè)類,有一個(gè)字段叫做 inJustDecodeBounds 。SDK中對(duì)這個(gè)成員的說(shuō)明是這樣的:
    If set to true, the decoder will return null (no bitmap), but the out…
    也就是說(shuō),如果我們把它設(shè)為true,那么BitmapFactory.decodeFile(String path, Options opt)并不會(huì)真的返回一個(gè)Bitmap給你,它僅僅會(huì)把它的寬,高取回來(lái)給你,這樣就不會(huì)占用太多的內(nèi)存,也就不會(huì)那么頻繁的發(fā)生OOM了。
    示例代碼如下:

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

    復(fù)制代碼

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

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

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

    復(fù)制代碼

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

    1. inSampleSize = options.outWidth / 200;

    另外,為了節(jié)約內(nèi)存我們還可以使用下面的幾個(gè)字段:

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

        

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

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊(cè)

    收藏夾

    Java

    搜索

    •  

    最新隨筆

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 88av免费观看| 91在线视频免费91| 免费观看成人毛片a片2008| 亚洲成a人片77777kkkk| 国产精品亚洲五月天高清| 日韩免费一区二区三区在线| 亚洲综合久久1区2区3区| 中文无码成人免费视频在线观看| 亚洲精品高清一二区久久| 亚洲国产成人久久综合| 国产va精品免费观看| 免费人成网站永久| 亚洲裸男gv网站| 8x成人永久免费视频| 理论亚洲区美一区二区三区| 国产aa免费视频| www.av在线免费观看| 插B内射18免费视频| 亚洲色成人网站WWW永久四虎| 国产va精品免费观看| 97国免费在线视频| 亚洲一区二区三区影院| 美女巨胸喷奶水视频www免费| 亚洲AV无码久久寂寞少妇| 无码国产精品一区二区免费式芒果| 国产成人亚洲精品青草天美| 日韩激情无码免费毛片| 亚洲国产欧美国产综合一区| 亚洲AV成人精品网站在线播放| 色猫咪免费人成网站在线观看| 国产精品亚洲一区二区无码| 91亚洲精品麻豆| 国产精品无码一区二区三区免费| 免费视频成人国产精品网站| 亚洲国产成AV人天堂无码| 亚洲欧洲成人精品香蕉网| 四虎永久免费网站免费观看| 青青草免费在线视频| 亚洲处破女AV日韩精品| 四虎影永久在线高清免费| 久久精品网站免费观看|