2013-04-02 10 views

Odpowiedz

72

Jeśli zdasz bitmapę width i height następnie użyć:

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

Jeśli chcesz zachować proporcje mapy bitowej to samo, ale ograniczyć go do maksymalnej długości boku, przeznaczenie:

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

Dzięki za ten fragment. Wystąpił błąd, musisz sprawdzić "if (bitmapRatio> 1)" nie 0. Nawet jeśli wysokość jest większa, nie będziesz miał ujemnego współczynnika. – ClemM

+0

Widzę to rozwiązanie wszędzie. Ale dlaczego przycina moją bitmapę? Zostawiłem tylko górną część i przesunięto ją również na prawo od ekranu ( – Sermilion

11

użyć tej metody

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

lub możesz zrobić to tak:

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

Przechodząc filtr = false spowoduje blokowe, pikselową obrazu.

Przejście filtru = true zapewni gładsze krawędzie.

+0

jak czy możesz użyć tej metody? Ta metoda nie istnieje! – coolcool1994

+3

Zgłosiłeś? Co masz na myśli, że nie istnieje? Bitmap.createScaledBitmap istnieje od api lvl 1 –