android公式サイト処理画像コード

8349 ワード

 1   /**  2  *          (           )  3  *  4  * @param res  5  * @param resId  6  * @param reqWidth  7  *               8  * @param reqHeight  9  *              10  * @return 11 */ 12 public static Bitmap decodeSampledBitmapFromResource(Resources res, 13 int resId, int reqWidth, int reqHeight) { 14 15 //        ,        16 final BitmapFactory.Options options = new BitmapFactory.Options(); 17 //  inJustDecodeBounds  true ,                18 options.inJustDecodeBounds = true; 19 //              options   ,decode      bitmap   20  BitmapFactory.decodeResource(res, resId, options); 21 22 //       , inSampleSize=4 ,         1/4 23 options.inSampleSize = calculateInSampleSize(options, reqWidth, 24  reqHeight); 25 26 //  inJustDecodeBounds  false ,BitmapFactory.decode...          27 options.inJustDecodeBounds = false; 28 //                    29 return BitmapFactory.decodeResource(res, resId, options); 30  } 31 32 /** 33  *         (           ) 34  * 35  * @param options 36  *           37  * @param reqWidth 38  *              39  * @param reqHeight 40  *              41  * @return 42 */ 43 public static int calculateInSampleSize(BitmapFactory.Options options, 44 int reqWidth, int reqHeight) { 45 //          46 final int height = options.outHeight; 47 final int width = options.outWidth; 48 //         1 49 int inSampleSize = 1; 50 51 //                       ,         52 if (height > reqHeight || width > reqWidth) { 53 54 final int halfHeight = height / 2; 55 final int halfWidth = width / 2; 56 57 //              , 58 //                  ~          59 while ((halfHeight / inSampleSize) >= reqHeight 60 && (halfWidth / inSampleSize) >= reqWidth) { 61 inSampleSize *= 2; 62  } 63  } 64 return inSampleSize; 65 }