2013-04-02 7 views
26

bazı belirtilen piksele Bitmap boyutunu azaltın ben 640px maksimum My Bitmap görüntü boyutunu küçültmek istiyorum. Mesela ben 1200 x 1200 px .. ben 640px bunu azaltabilir Nasıl boyutta Bitmap görüntü var. Bu yöntemAndroid

/** 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; 
} 

cevap

72

width ve height sonra kullanın:

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

aynı bitmap oranını korumak, ancak maksimum kenar uzunluğu, kullanımına azaltmak isterseniz:

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

teşekkür lvl API beri var. "(BitmapRatio> 1) ise" değil 0. yüksekliği daha büyük olsa bile, negatif bir orana sahip olmayacak, denetlemek zorundadır sert bir hata var. – ClemM

+0

Bu çözümü her yerde görüyorum. Ama neden bitmapimi mahvediyor? Sadece üst kısımdan ayrıldım ve ekranın sağına da kaydırılmış ( – Sermilion

11

Kullanım:

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

filtreyi geçme = false bloklu, pixellated görüntüde sonuçlanacaktır.

filtreyi geçme = size pürüzsüz kenarları verecek gerçek. Eğer bit eşlemi geçerseniz

10

veya böyle bunu yapabilir

+0

nasıl bu yöntemi kullanabilir misin? Bu yöntem mevcut değil! – coolcool1994

+3

Reddettiniz mi? bunu da mevcut değil de ne demek? Bitmap.createScaledBitmap bu pasajı için 1 –