Cómo redimensionar un mapa de bits eficientemente y sin perder calidad en android
Tengo un Bitmap
con un tamaño de 320x480
y tengo que estirarlo en diferentes pantallas de dispositivo, he intentado usar esto:
Rect dstRect = new Rect(); canvas.getClipBounds(dstRect); canvas.drawBitmap(frameBuffer, null, dstRect, null);
funciona, la imagen llena toda la pantalla como quería pero la imagen está pixelada y se ve mal. Entonces lo intenté:
- Android cómo dibujar un mapa de bits sobre lienzo semi transparente
- Cómo cambiar el color de fondo de un mapa de bits transparente guardado
- Android Native crash en /system/lib/libskia.so
- ¿Por qué el tamaño de mapa de bits es más grande en la memoria que en el disco en Android?
- Android: mapa de bits baja escala de baja calidad
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 se ve perfecto, agradable y suave, pero este código tiene que estar en mi bucle de juego principal y la creación de Bitmap
s cada iteración lo hace muy lento. ¿Cómo puedo cambiar el tamaño de mi imagen para que no se pixelado y se hace rápido?
ENCONTRADO LA SOLUCIÓN:
Paint paint = new Paint(); paint.setFilterBitmap(); canvas.drawBitmap(bitmap, matrix, paint);
- ArrayIndexOutOfBoundsException en android.graphics.Bitmap $ Config.nativeToConfig en dispositivos Samsung
- ¿Cómo se utilizan los valores de android.graphics.Matrix?
- Cómo desenfocar un mapa de bits (Android)?
- Android - Dibujar en la parte superior de una imagen
- Android: ¿cómo administrar el tamaño de mapa de bits al dibujar a pantallas de diferentes tamaños?
- ¿Cómo posiciono mi mapa de bits en el lienzo usando una matriz?
- Android: BitmapDrawable.Draw (lienzo) No parece funcionar
- problema en el uso de mapa de bits en android
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; }
Bastante autoexplicativo: simplemente ingrese el objeto Bitmap original y las dimensiones deseadas del Bitmap, y este método le devolverá el mapa de bits recién redimensionado. Puede ser, es útil para usted.
Estoy utilizando la solución anterior para cambiar el tamaño del mapa de bits. Pero la parte de los resultados 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; }
Tamaño de la superficie de vista previa: 352: 288 Anchura de mapa de bits antes de cambiar el tamaño: 320 Altura: 240 Anchura de la vista de la cámara: 1080 Altura: 1362 Anchura de mapa de bits con reajuste: 1022 Altura: 1307