Exigente05
Exigente05

Reputation: 2211

How to make background transparent in android

I have two images in drawable folder and I am doing this:

image = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
background = BitmapFactory.decodeResource(getResources(), R.drawable.image2);


canvas.drawBitmap(background,0, 0, null);
canvas.drawBitmap(image, x, y, null);

There is WHITE background in my picture named "image" but i want to show only the object of that picture means want to make the background transparent. How can I do this ?

Upvotes: 0

Views: 1762

Answers (5)

Sar
Sar

Reputation: 560

If your image has white background then you can use setAlpha() for drawable. But the best way is that you should use image with no background.

Example :

 imageview.getDrawable().setAlpha(60);

Upvotes: 0

Fouad Wahabi
Fouad Wahabi

Reputation: 804

You can create a transparent drawable and make it as the background of the View .

Android uses Hex ARGB values, which are formatted as #AARRGGBB. That first pair of letters, the AA, represent the Alpha Channel. To set alpha value you must convert your decimal opacity values to a Hexdecimal value.

Here's hex Opacity Values

  • 100% — FF
  • 95% — F2
  • 90% — E6
  • 85% — D9
  • 80% — CC
  • 75% — BF
  • 70% — B3
  • 65% — A6
  • 60% — 99
  • 55% — 8C
  • 50% — 80
  • 45% — 73
  • 40% — 66
  • 35% — 59
  • 30% — 4D
  • 25% — 40
  • 20% — 33
  • 15% — 26
  • 10% — 1A
  • 5% — 0D
  • 0% — 00

Upvotes: 0

ishu
ishu

Reputation: 1332

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/myimage"
    android:alpha=".75"/>
Programatically, you can also set it with

int alphaAmount = 125; // some value 0-255 where 0 is fully transparent and 255 is fully opaque
myImage.setAlpha(alphaAmount);

Upvotes: 0

Booger
Booger

Reputation: 18725

You can always use the Transparent color as a background, which is set the same as any drawable, just use @android:color/transparent

Upvotes: 1

Mojo Risin
Mojo Risin

Reputation: 8142

You can make the canvas transparent before drawing the images.

canvas.drawColor(Color.TRANSPARENT);

Upvotes: 1

Related Questions