How to Change a Bitmap's Opacity

How to change a bitmap's opacity?

You could also try BitmapDrawable instead of Bitmap. If this is useful for you depends on the way you use the bitmap...

Edit

As a commenter asked how he can store the bitmap with alpha, here is some code:

// 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);

How to dynamically change the opacity of the bitmap in android

you can use Color.argb .. here is sample code.

canvas.drawColor(Color.argb(alpha, red, green, blue));

where alpha is opacity so you can use your progess value in alpha..

here aplha, red, green & blue must be 0 to 255

in your case you have to pass everytime same RGB just change Alpha value

Android change bitmap transparency on canvas

You can use a Paint object to modify the alpha of the bitmap to be drawn:

Paint alphaPaint = new Paint();
alphaPaint.setAlpha(alpha);
canvas.drawBitmap(heartSymb, 0, 0, alphaPaint);

Then you just have to modify alpha value and perform update periodically, maybe by using a Handler.

Changing the Opacity of a Bitmap image

Try this one from CodeProject - Change Opacity of Image in C#:

/// <summary>  
/// method for changing the opacity of an image
/// </summary>
/// <param name="image">image to set opacity on</param>
/// <param name="opacity">percentage of opacity</param>
/// <returns></returns>
public Image SetImageOpacity(Image image, float opacity)
{
try
{
//create a Bitmap the size of the image provided
Bitmap bmp = new Bitmap(image.Width, image.Height);

//create a graphics object from the image
using (Graphics gfx = Graphics.FromImage(bmp)) {

//create a color matrix object
ColorMatrix matrix = new ColorMatrix();

//set the opacity
matrix.Matrix33 = opacity;

//create image attributes
ImageAttributes attributes = new ImageAttributes();

//set the color(opacity) of the image
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

//now draw the image
gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
}
return bmp;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}

How to draw a bitmap on a canvas, respecting alpha values of the bitmap?

Canvas.setXfermode(Xfermode xfermode). There are a number of Xfermodes you can choose.

How do I Draw a Bitmap with 50% Opacity?

Here is some code that adds an alpha channel to an image. If you want 50% alpha, you would set 128 as the alpha argument. Note this creates a copy of the bitmap...

    public static Bitmap AddAlpha(Bitmap currentImage, byte alpha)
{
Bitmap alphaImage;
if (currentImage.PixelFormat != PixelFormat.Format32bppArgb)
{
alphaImage = new Bitmap(currentImage.Width, currentImage.Height, PixelFormat.Format32bppArgb);
using (Graphics gr = Graphics.FromImage(tmpImage))
{
gr.DrawImage(currentImage, 0, 0, currentImage.Width, currentImage.Height);
}
}
else
{
alphaImage = new Bitmap(currentImage);
}

BitmapData bmData = alphaImage.LockBits(new Rectangle(0, 0, alphaImage.Width, alphaImage.Height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

const int bytesPerPixel = 4;
const int alphaPixel = 3;
int stride = bmData.Stride;

unsafe
{
byte* pixel = (byte*)(void*)bmData.Scan0;

for (int y = 0; y < currentImage.Height; y++)
{
int yPos = y * stride;
for (int x = 0; x < currentImage.Width; x++)
{
int pos = yPos + (x * bytesPerPixel);
pixel[pos + alphaPixel] = alphaByte;
}
}
}

alphaImage.UnlockBits(bmData);

return alphaImage;
}


Related Topics



Leave a reply



Submit