Guardar mapa de bits Android sin área transparente

Quiero guardar bitmap sin área transparente.

El mapa de bits tiene píxeles transparentes grandes.

Así que quiero quitar eso

¿Cómo puedo hacer esto?

No puedo añadir imagen para explicar con símbolos.

No quiero recortar la función. Espero utilizar filtro

┌────────────────────────┐

│ área transparente

│ ┌────────┐

Cosechar esto
└────────┘
└────────────────────────┘

Para encontrar el área no transparente de su mapa de bits, iterar a través del mapa de bits en xey, y encontrar el mínimo y máximo de la región no transparente. Luego recorte el mapa de bits a esas coordenadas.

Bitmap CropBitmapTransparency(Bitmap sourceBitmap) { int minX = sourceBitmap.getWidth(); int minY = sourceBitmap.getHeight(); int maxX = -1; int maxY = -1; for(int y = 0; y < sourceBitmap.getHeight(); y++) { for(int x = 0; x < sourceBitmap.getWidth(); x++) { int alpha = (sourceBitmap.getPixel(x, y) >> 24) & 255; if(alpha > 0) // pixel is not 100% transparent { if(x < minX) minX = x; if(x > maxX) maxX = x; if(y < minY) minY = y; if(y > maxY) maxY = y; } } } if((maxX < minX) || (maxY < minY)) return null; // Bitmap is entirely transparent // crop bitmap to non-transparent area and return: return Bitmap.createBitmap(sourceBitmap, minX, minY, (maxX - minX) + 1, (maxY - minY) + 1); } 

Coste el borde transparente con este github .

 public Bitmap crop (Bitmap bitmap){ int height = bitmap.getHeight(); int width = bitmap.getWidth(); int[] empty = new int[width]; int[] buffer = new int[width]; Arrays.fill(empty,0); int top = 0; int left = 0; int botton = height; int right = width; for (int y = 0; y < height; y++) { bitmap.getPixels(buffer, 0, width, 0, y, width, 1); if (!Arrays.equals(empty, buffer)) { top = y; break; } } for (int y = height - 1; y > top; y--) { bitmap.getPixels(buffer, 0, width, 0, y, width, 1); if (!Arrays.equals(empty, buffer)) { botton = y; break; } } int bufferSize = botton -top +1; empty = new int[bufferSize]; buffer = new int[bufferSize]; Arrays.fill(empty,0); for (int x = 0; x < width; x++) { bitmap.getPixels(buffer, 0, 1, x, top + 1, 1, bufferSize); if (!Arrays.equals(empty, buffer)) { left = x; break; } } for (int x = width - 1; x > left; x--) { bitmap.getPixels(buffer, 0, 1, x, top + 1, 1, bufferSize); if (!Arrays.equals(empty, buffer)) { right = x; break; } } Bitmap cropedBitmap = Bitmap.createBitmap(bitmap, left, top, right-left, botton-top); return cropedBitmap; } 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.