Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 技術總結(010)—— 加載大尺寸的bitmap

Android 技術總結(010)—— 加載大尺寸的bitmap

編輯:關於Android編程

[java] 
BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeResource(getResources(), R.id.myimage, options); 
int imageHeight = options.outHeight; 
int imageWidth = options.outWidth; 
String imageType = options.outMimeType; 

[java] 
public static int calculateInSampleSize( 
            BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 
 
    if (height > reqHeight || width > reqWidth) { 
        if (width > height) { 
            inSampleSize = Math.round((float)height / (float)reqHeight); 
        } else { 
            inSampleSize = Math.round((float)width / (float)reqWidth); 
        } 
    } 
    return inSampleSize; 

[java] 
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
        int reqWidth, int reqHeight) { 
 
    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 
 
    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 
 
    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeResource(res, resId, options); 

[java] view plaincopy
mImageView.setImageBitmap( 
    decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100)); 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved