2011-11-30 5 views
14

Tengo un Bitmap con un tamaño de 320x480 y tengo que estirar en diferentes pantallas de dispositivos, he intentado usar esto:Cómo cambiar el tamaño de un mapa de bits eficiently y con una calidad de perder en Android

Rect dstRect = new Rect(); 
canvas.getClipBounds(dstRect); 
canvas.drawBitmap(frameBuffer, null, dstRect, null); 

que funciona , la imagen llena toda la pantalla como yo quería, pero la imagen está pixelada y se ve mal. Luego probé:

float scaleWidth = (float) newWidth/width; 
float scaleHeight = (float) newHeight/height; 
Matrix matrix = new Matrix(); 
matrix.postScale(scaleWidth, scaleHeight); 
Bitmap resizedBitmap = Bitmap.createBitmap(frameBuffer, 0, 0, 
       width, height, matrix, true); 
canvas.drawBitmap(resizedBitmap, 0, 0, null); 

esta vez parece perfecto, agradable y suave, pero el código tiene que estar en mi bucle de juego principal y la creación de Bitmap s cada iteración hace que sea muy lento. ¿Cómo puedo cambiar el tamaño de mi imagen para que no se pixele y se haga rápido?

encontrado la solución:

Paint paint = new Paint(); 
paint.setFilterBitmap(); 
canvas.drawBitmap(bitmap, matrix, paint); 
+2

Si encontró su propia solución, debe agregarla como respuesta a su pregunta y aceptarla usted mismo para que otros no agreguen respuestas no deseadas a su pregunta. – Kuffs

Respuesta

33

Cambiar el tamaño de un mapa de bits:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) 
{ 
    int width = bm.getWidth(); 
    int height = bm.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(bm, 0, 0, width, height, matrix, false); 
    return resizedBitmap; 
} 

auto Pretty explicativa: simplemente de entrada del objeto original de mapa de bits y las dimensiones deseadas del mapa de bits, y este método se ¡Devolverte el mapa de bits recién redimensionado! Puede ser, es útil para usted.

+0

@ user924941 Este cambio de tamaño por única vez es mejor que cambiar el tamaño de cada fotograma. –

+4

para una imagen escalada de mejor calidad Bitmap resizedBitmap = Bitmap.createBitmap (bm, 0, 0, ancho, alto, matriz, verdadero); –

+0

@ BorisKarloff: Establecer el parámetro de filtro no tiene ningún efecto cuando se escala, lo que al OP le interesa. –

0

Estoy usando la solución anterior para cambiar el tamaño del mapa de bits. Pero resulta que la porción de la imagen se pierde.

Aquí está mi código.

BitmapFactory.Options bmFactoryOptions = new BitmapFactory.Options(); 
      bmFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888; 
      bmFactoryOptions.inMutable = true; 
      bmFactoryOptions.inSampleSize = 2; 
      Bitmap originalCameraBitmap = BitmapFactory.decodeByteArray(pData, 0, pData.length, bmFactoryOptions); 
      rotatedBitmap = getResizedBitmap(originalCameraBitmap, cameraPreviewLayout.getHeight(), cameraPreviewLayout.getWidth() - preSizePriviewHight(), (int) rotationDegrees); 

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight, int angle) { 
     int width = bm.getWidth(); 
     int height = bm.getHeight(); 
     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(angle); 
     matrix.postScale(scaleWidth, scaleHeight); 
     Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true); 
     DeliverItApplication.getInstance().setImageCaptured(true); 
     return resizedBitmap; 
    } 

Y aquí es la altura y la anchura: Tamaño Vista previa de superficie: 352: 288 Antes de ancho de mapa de bits redimensionada: 320 Altura: 240 Ancho CameraPreviewLayout: 1080 Altura: 1362 redimensionada ancho de mapa de bits: 1022 Altura: 1307

Cuestiones relacionadas