Cómo cambiar la opacidad de un mapa de bits?

Tengo un mapa de bits:

Bitmap bitmap = BitmapFactory.decodeFile("some/arbitrary/path/image.jpg"); 

Pero no voy a mostrar la imagen al usuario. Quiero que el alfa sea 100 (de 255). Si esto no es posible, ¿puedo configurar la opacidad del Bitmap de Bitmap ?

También puede intentar BitmapDrawable en lugar de Bitmap . Si esto es útil para usted, depende de la forma en que utilice el mapa de bits …

Editar

Como un comentarista preguntó cómo puede almacenar el mapa de bits con alfa, aquí hay algún código:

 // lets create a new empty bitmap Bitmap newBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), originalBitmap.getHeight(), Bitmap.Config.ARGB_8888); // create a canvas where we can draw on Canvas canvas = new Canvas(newBitmap); // create a paint instance with alpha Paint alphaPaint = new Paint(); alphaPaint.setAlpha(42); // now lets draw using alphaPaint instance canvas.drawBitmap(originalBitmap, 0, 0, alphaPaint); // now lets store the bitmap to a file - the canvas has drawn on the newBitmap, so we can just store that one // please add stream handling with try/catch blocks FileOutputStream fos = new FileOutputStream(new File("/awesome/path/to/bitmap.png")); newBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); 

Por lo que sé, la opacidad u otros filtros de color no se puede establecer en el mapa de bits en sí. Tendrá que configurar el alfa cuando use la imagen:

Si está utilizando ImageView, hay ImageView.setAlpha () .

Si estás usando una lona, ​​entonces necesitas usar Paint.setAlpha () :

 Paint paint = new Paint(); paint.setAlpha(100); canvas.drawBitmap(bitmap, src, dst, paint); 

Además, si incorporas la respuesta de WarrenFaith, si usas el Bitmap donde se requiere un drawable , puedes usar BitmapDrawable.setAlpha () .

 Bitmap bgr = BitmapFactory.decodeResource(getResources(),R.drawable.main_logo_2); Paint transparentpainthack = new Paint(); transparentpainthack.setAlpha(100); canvas.drawBitmap(bgr, 0, 0, transparentpainthack); 
 public Bitmap makeTransparent(Bitmap src, int value) { int width = src.getWidth(); int height = src.getHeight(); Bitmap transBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(transBitmap); canvas.drawARGB(0, 0, 0, 0); // config paint final Paint paint = new Paint(); paint.setAlpha(value); canvas.drawBitmap(src, 0, 0, paint); return transBitmap; } 

https://dzone.com/articles/adjusting-opacity-android propone:

 /** * @param bitmap The source bitmap. * @param opacity a value between 0 (completely transparent) and 255 (completely * opaque). * @return The opacity-adjusted bitmap. If the source bitmap is mutable it will be * adjusted and returned, otherwise a new bitmap is created. */ private Bitmap adjustOpacity(Bitmap bitmap, int opacity) { Bitmap mutableBitmap = bitmap.isMutable() ? bitmap : bitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(mutableBitmap); int colour = (opacity & 0xFF) << 24; canvas.drawColor(colour, PorterDuff.Mode.DST_IN); return mutableBitmap; } 

Tenga en cuenta que con DST_IN puede modificar (en lugar de restablecer) la transparencia de la imagen ya transparente, es decir, puede hacer que la imagen sea cada vez más transparente.

Si está usando un dibujable para mostrar la imagen, puede cambiar el alfa de la siguiente manera:

 private Drawable mTriangle; mTriangle = context.getResources().getDrawable(R.drawable.triangle_arrow_for_radar); ... protected void onDraw(Canvas canvas) { // Draw the triangle arrow float imageTargetWidth = getWidth() / 15; float scale = mTriangle.getIntrinsicWidth() / imageTargetWidth; int imgWidth = (int)(imageTargetWidth); int imgHeight = (int)(mTriangle.getIntrinsicHeight() / scale); if (mTriangle != null) { mTriangle.setBounds(getWidth() / 2 - imgWidth / 2, getHeight() / 2 - imgHeight / 2, getWidth() / 2 + imgWidth / 2, getHeight() / 2 + imgHeight / 2); mTriangle.setAlpha(150); // from (transparent) to 255 (opaque) mTriangle.draw(canvas); } } 
  • Imagen sin recortar
  • Estiramiento Mapa de bits en una esquina específica
  • ¿Por qué el tamaño de mapa de bits es más grande en la memoria que en el disco en Android?
  • Android procesamiento de imágenes de alta resolución
  • ArrayIndexOutOfBoundsException en android.graphics.Bitmap $ Config.nativeToConfig en dispositivos Samsung
  • Tamaño del mapa de bits devuelto por la cámara a través de la intención?
  • Dibujar texto en la parte superior de mapa de bits no
  • Android drawBitmap rendimiento de lotes de mapas de bits?
  • Android: reduce el tamaño del archivo de imagen
  • Recortar una transformación de perspectiva de la imagen en Android
  • Guardar un edittext en un mapa de bits
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.