2013-04-02 2 views

ответ

72

Если передать растровое width и height затем использовать:

public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, int bitmapHeight) { 
    return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight, true); 
} 

Если вы хотите сохранить соотношение растровый то же самое, но уменьшить его до максимальной длиной стороны, использование:

public Bitmap getResizedBitmap(Bitmap image, int maxSize) { 
     int width = image.getWidth(); 
     int height = image.getHeight(); 

     float bitmapRatio = (float) width/(float) height; 
     if (bitmapRatio > 1) { 
      width = maxSize; 
      height = (int) (width/bitmapRatio); 
     } else { 
      height = maxSize; 
      width = (int) (height * bitmapRatio); 
     } 

     return Bitmap.createScaledBitmap(image, width, height, true); 
} 
+7

Спасибо за этот фрагмент. Ошибка жесткая, вы должны проверить «if (bitmapRatio> 1)» не 0. Даже если высота больше, у вас не будет отрицательного отношения. – ClemM

+0

Я вижу это решение повсюду. Но почему это обрезает мое растровое изображение? Я только оставил верхнюю часть, и она также сдвигается вправо от экрана ( – Sermilion

11

Используйте этот метод

/** getResizedBitmap method is used to Resized the Image according to custom width and height 
    * @param image 
    * @param newHeight (new desired height) 
    * @param newWidth (new desired Width) 
    * @return image (new resized image) 
    * */ 
public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) { 
    int width = image.getWidth(); 
    int height = image.getHeight(); 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 
    // create a matrix for the manipulation 
    Matrix matrix = new Matrix(); 
    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 
    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height, 
      matrix, false); 
    return resizedBitmap; 
} 
10

или вы можете сделать это следующим образом:

Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter); 

Передача фильтр = ложь приведет к блочным, пикселированные изображение.

Passing filter = true даст вам более гладкие края.

+0

как можете ли вы использовать этот метод? Этот метод не существует! – coolcool1994

+3

Вы спустили вниз? Что значит, что этого не существует? Bitmap.createScaledBitmap существует, поскольку api lvl 1 –

Смежные вопросы