2011-11-13 11 views

Odpowiedz

85

Twoje pytanie jest nieco krótka informacja na co Cię chcesz to osiągnąć, ale domyślam się, że masz Bitmapę i chcesz ją skalować do nowego rozmiaru i że skalowanie powinno być e jako "centerCrop" działa dla ImageViews.

Z Docs

skali obraz równomiernie (utrzymać współczynnik kształtu obrazu), tak że zarówno wymiary (szerokość i wysokość) obrazu będzie równa lub większy niż odpowiedni wymiar widok (minus dopełnienie).

O ile mi wiadomo, nie ma na to jednego liniowca (proszę poprawić mnie, jeśli się mylę), ale można napisać własną metodę, aby to zrobić. Poniższa metoda oblicza, jak skalować oryginalną bitmapę do nowego rozmiaru i narysować ją w środku w wynikowej bitmapie.

Mam nadzieję, że pomoże!

public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) { 
    int sourceWidth = source.getWidth(); 
    int sourceHeight = source.getHeight(); 

    // Compute the scaling factors to fit the new height and width, respectively. 
    // To cover the final image, the final scaling will be the bigger 
    // of these two. 
    float xScale = (float) newWidth/sourceWidth; 
    float yScale = (float) newHeight/sourceHeight; 
    float scale = Math.max(xScale, yScale); 

    // Now get the size of the source bitmap when scaled 
    float scaledWidth = scale * sourceWidth; 
    float scaledHeight = scale * sourceHeight; 

    // Let's find out the upper left coordinates if the scaled bitmap 
    // should be centered in the new size give by the parameters 
    float left = (newWidth - scaledWidth)/2; 
    float top = (newHeight - scaledHeight)/2; 

    // The target rectangle for the new, scaled version of the source bitmap will now 
    // be 
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight); 

    // Finally, we create a new bitmap of the specified size and draw our new, 
    // scaled bitmap onto it. 
    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig()); 
    Canvas canvas = new Canvas(dest); 
    canvas.drawBitmap(source, null, targetRect, null); 

    return dest; 
} 
+0

Witam, czy mogę wiedzieć, w jaki sposób mogę nadać mu białą ramkę z tej bitmapy? – ericlee

+0

Czy chcesz białą ramkę na szczycie skalowanej bitmapy, czy chcesz obramować ją białą ramką? – Albin

+0

@ericlee to nowe pytanie. – StackOverflowed

Powiązane problemy