Android中Bitmap

api地址

加載資源圖片(通過BitmapFactory獲取)

Bitmap不能new(不用jni情況下),用BitmapFactory獲得bitmap

//這裏先不用BitmapFactory.Options
 Bitmap bitmap= BitmapFactory.decodeResource(getActivity().getResources(),R.mipmap.jinsixiong);
        Log.i(TAG, "with: "+bitmap.getWidth());
        Log.i(TAG, "height: "+bitmap.getWidth());

結果–也就是說我的圖片是1066*1066像素
這裏寫圖片描述

10-12 14:08:16.371 9402-9402/doimage.szgroup.wy.doimage I/BitmapTestFragment: with: 1066
10-12 14:08:16.371 9402-9402/doimage.szgroup.wy.doimage I/BitmapTestFragment: height: 1038

加載圖片設置option.inSampleSize

(圖片壓縮比例)
英文API:
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.
百度翻譯:如果設置的值大於1,要求解碼器子樣的原始圖像,返回一個較小的圖像保存記憶。
我理解:請求值要大於1,對原始圖像請求解析子圖像,返回一個更小的圖像保存

       BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize=2;

        Bitmap bitmap= BitmapFactory.decodeResource(getActivity().getResources(),R.mipmap.jinsixiong,options);
        Log.i(TAG, "with: "+bitmap.getWidth());
        Log.i(TAG, "height: "+bitmap.getWidth());

也就是我的小老鼠縮小到了1/4

10-12 14:13:43.364 14341-14341/doimage.szgroup.wy.doimage I/BitmapTestFragment: with: 533
10-12 14:13:43.364 14341-14341/doimage.szgroup.wy.doimage I/BitmapTestFragment: height: 519

option.inJustDecodeBounds

英文Api:
If set to true, the decoder will return null (no bitmap), but the out… fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
百度翻譯:
如果設置爲真,解碼器將返回空(沒有位圖),但輸出…字段仍然將被設置,允許調用方查詢位圖,而不必爲其像素分配內存。
我理解:
如果設置爲真則返回解析返回爲空,bitmap爲null,option取到值

  BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize=2;
        options.inJustDecodeBounds=true;

        Bitmap bitmap= BitmapFactory.decodeResource(getActivity().getResources(),R.mipmap.jinsixiong,options);
        Log.i(TAG, "with: "+options.outWidth);
        Log.i(TAG, "height: "+options.outHeight);

        Log.i(TAG, "with: "+bitmap.getWidth());
        Log.i(TAG, "height: "+bitmap.getWidth());

這段代碼後兩行會報錯,因爲設置options.inJustDecodeBounds=true所以bitmap是null但是options取到了值options.outWidth

這樣就可以在不加載原圖的情況下將他縮略

這裏有個問題 不知道爲什麼這個options.outWidth
輸出的結果爲1279 不是1066變大了(在設置options.inJustDecodeBounds=true)
情況下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章