2014-03-25 30 views
0

我想縮放現有的位圖。因此,我使用Bitmap.createBitmap(位圖b,int x,int y,int width,int height,Matrix m,布爾過濾器)來創建縮放位圖。如果Matrix對象的縮放比例小於1,則此方法運行良好。但是,如果比率等於或大於1,該方法將返回一個位圖,其中包含我希望的寬度和高度,但沒有圖像(透明)。我想知道爲什麼以及如何解決這個問題。createBitmap()返回一個未繪製的位圖

Bitmap imageBitmap; 
    imageBitmap = weiboView.getDrawingCache(); 
    Log.d("cosmo", "bitmap generated: "+imageBitmap.getWidth()+" * "+imageBitmap.getHeight()); 

    //Init and configure and load the image view 
    ImageView imageView = new ImageView(this); 
    imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
    imageView.setScaleType(ScaleType.FIT_CENTER); 
    containerLayout.addView(imageView); 

    //create a scaled bitmap to assign to the image view 
    Bitmap scaledImageBitmap = MyBitmapUtils.getBitmapScaledToFitWidth(getWindowManager().getDefaultDisplay().getWidth(), imageBitmap); 
    Log.d("cosmo", "scaled: "+scaledImageBitmap.getWidth()+" * "+scaledImageBitmap.getHeight()); 
    //Here if I set imageBitmap as the image of imageView it works well 
    imageView.setImageBitmap(scaledImageBitmap); 

這裏是MyBitmapUtils.getBitmapScaledToFitWidth:

public static Bitmap getBitmapScaledToFitWidth(int targetWidth, Bitmap bitmap) { 
    float ratio = (float)targetWidth/(float)bitmap.getWidth(); 
    Log.d("cosmo", "ratio is "+ratio); 
    //ratio = 0.5f; 
    Matrix matrix = new Matrix(); 
    matrix.postScale(ratio, ratio); 
    Bitmap target = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,false); 
    return target; 
} 

我知道這是怎麼回事。這是因爲該圖像的高度大於2048,並在Android的系統創建一個大於2048 * 2048位圖將導致OOM(我的logcat沒有報告這個錯誤,奇怪)

+0

,爲什麼你需要縮放的位圖?你想把它保存在SD卡上還是發送到某個地方? – pskink

+0

只是想縮放它以適應指定寬度的圖像視圖 – cosmozhang

+0

不,不,不要這樣做,使用scaleType =「matrix」並調用setImageMatrix,或者使用任何其他長寬比來保持scaleType – pskink

回答

0

爲什麼你不只是使用createScaledBitmap

Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true); 

您可以直接提供targetWidth和targetHeight,它應該是比自己一個Matrix縮放位圖更可靠。

如果你只是提供targetWidth到你的方法,那麼你必須計算targetHeight這將是這個樣子:

float ratio = (float)targetWidth/(float)bitmap.getWidth(); 
int targetHeight = (int)((float)bitmap.getHeight * ratio); 

如果你把你的方法,它是這樣的:

public static Bitmap getBitmapScaledToFitWidth(double targetWidth, Bitmap bitmap) { 
    double bitmapWidth = bitmap.getWidth(); 
    double bitmapHeight = bitmap.getHeight(); 

    double widthRatio = targetWidth/bitmapWidth; 
    double targetHeight = widthRatio * bitmapHeight; 

    Bitmap target = Bitmap.createScaledBitmap(bitmap, (int)targetWidth, (int)targetHeight, true); 
    return target; 
} 

也不要忘了recycle()不需要位圖,因爲經常和儘快。可以縮放它,你可以在你的helper方法直接回收後,它阻止你運行到內存的問題,例如,如果你不需要的不縮放位圖了:

public static Bitmap getBitmapScaledToFitWidth(double targetWidth, Bitmap bitmap) { 
    double bitmapWidth = bitmap.getWidth(); 
    double bitmapHeight = bitmap.getHeight(); 

    double widthRatio = targetWidth/bitmapWidth; 
    double targetHeight = widthRatio * bitmapHeight; 

    Bitmap target = Bitmap.createScaledBitmap(bitmap, (int)targetWidth, (int)targetHeight, true); 

    // Recycle old bitmap to free up memory. 
    bitmap.recycle(); 

    return target; 
} 
+0

嗯..我didn不知道那種方法。我會考慮使用它。 – cosmozhang

+0

但我仍然想知道爲什麼會發生這種情況,請原諒我...... – cosmozhang

+0

這可能與您的Matrix有關。嘗試在不調用'postScale(...)'的情況下創建'Bitmap'。位圖不會縮放,但是如果它顯示的正確性比您知道它是'postScale(...)'的錯誤。 –