2011-11-13 10 views

Respuesta

85

Su pregunta es un poco corto de información sobre lo que quiero lograr, pero supongo que tiene un mapa de bits y quiere escalarlo a un nuevo tamaño y que la escala debe ser don e como "centerCrop" funciona para ImageViews.

De Docs

escala de la imagen de manera uniforme (mantener la relación de aspecto de la imagen) de modo que ambas dimensiones (anchura y altura) de la imagen será igual o mayor que la dimensión correspondiente de la vista (menos relleno).

Por lo que yo sé, no hay un trazador de líneas para hacerlo (corríjanme, si me equivoco), pero podría escribir su propio método para hacerlo. El siguiente método calcula cómo escalar el mapa de bits original al nuevo tamaño y dibujarlo centrado en el mapa de bits resultante.

Espero que ayude!

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

hola, ¿puedo saber cómo le doy un borde blanco en este mapa de bits? – ericlee

+0

¿Desea un borde blanco en la parte superior del mapa de bits escalado o desea un borde blanco, como relleno, a su alrededor? – Albin

+0

@ericlee esa es una nueva pregunta. – StackOverflowed

Cuestiones relacionadas