2011-02-25 24 views

Respuesta

25

También puede probar BitmapDrawable en lugar de Bitmap. Si esto es útil para usted depende de la forma de usar el mapa de bits ...

Edición

Como comentarista le preguntó cómo se puede almacenar el mapa de bits con alfa, he aquí algo de 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); 
+0

estoy usando BitmapDrawable para establecer el alfa es el éxito, pero después de aplicar el alfa dado como resultado la imagen dibujable guardado en mi tarjeta sd.cómo almacenar resultado dibujables? Gracias de antemano – Rajesh

+0

@Rajesh necesita dibujar el mapa de bits con alfa en otro mapa de bits vacío y almacenarlo en la carpeta de la aplicación. – WarrenFaith

+0

Gracias por su respuesta, no entiendo su respuesta. Este es mi código BitmapDrawable drawable = new BitmapDrawable (getResources(), bitmap); drawable.setAlpha (42); ¿Cómo puedo guardar el dibujable en mi dispositivo? Proporcione cualquier código de muestra para esto. Gracias – Rajesh

74

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

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

Si estás utilizando un lienzo, a continuación, es necesario utilizar Paint.setAlpha():

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

Además, la incorporación de la respuesta de WarrenFaith, si va a utilizar el mapa de bits, donde se requiere un estirable, puede utilizar BitmapDrawable.setAlpha().

+1

en el ¡dinero! – Li3ro

+0

Dado que 'BitmapDrawable.setAlpha (int)' está haciendo lo mismo que describió aquí. También puede establecer filtros de color complejos en el mapa de bits, usando 'Paint.setColorFilter (ColorFIlter)'. –

17
Bitmap bgr = BitmapFactory.decodeResource(getResources(),R.drawable.main_logo_2);  
Paint transparentpainthack = new Paint(); 
transparentpainthack.setAlpha(100); 
canvas.drawBitmap(bgr, 0, 0, transparentpainthack); 
14
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; 
} 
1

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 reajustar) la transparencia de la imagen ya transparente, es decir, puede hacer que la imagen sea más y más transparente.

0

Si está utilizando un Disponibles para mostrar la imagen, se 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); 
    } 
} 
Cuestiones relacionadas