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
?
- Android cómo dibujar un mapa de bits sobre lienzo semi transparente
- ¿Cómo cambiar de forma programática el contraste de un mapa de bits en android?
- llamada a Bitmap.compress no devuelve - y ninguna excepción
- Extraer mapa de bits del vídeo en android
- Mapa de bits a NinePatch a nuevo mapa de bits escalado
- GetDrawingCache devuelve siempre el mismo Bitmap
- Convertir Android.Graphics.Bitmap a la imagen
- Android - nuevo BitmapDrawable obsoleto; Alternativa Bitmap.createBitmap tiene que tener w / h> 0
- Lienzo: intentando usar un mapa de bits reciclado android.graphics.Bitmap en android
- Cambiar el tamaño de la imagen en la notificación de estilo bigpicture
- Convertir url de imagen en mapa de bits rápidamente
- Uso de la clase EffectFactory en mapa de bits - Android
- Android, ¿Cómo almacenar la imagen en almacenamiento interno?
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); } }
- No se muestran los elementos de acción del fragmento inicial de Viewpager
- Desinstala la aplicación de Android Wear desde un dispositivo real