How can I make an image transparent on Android?
android:alpha
does this in XML:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/blah"
android:alpha=".75"/>
Try this:
ImageView myImage = (ImageView) findViewById(R.id.myImage);
myImage.setAlpha(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
Note: setAlpha(int)
is deprecated in favor of setAlpha(float)
where 0 is fully transparent and 1 is fully opaque. Use it like: myImage.setAlpha(0.5f)
Set an id attribute on the ImageView:
<ImageView android:id="@+id/myImage"
In your code where you wish to hide the image, you'll need the following code.
First, you'll need a reference to the ImageView:
ImageView myImage = (ImageView) findViewById(R.id.myImage);
Then, set Visibility to GONE:
myImage.setVisibility(View.GONE);
If you want to have code elsewhere that makes it visible again, just set it to Visible the same way:
myImage.setVisibility(View.VISIBLE);
If you mean "fully transparent", the above code works. If you mean "partially transparent", use the following method:
int alphaAmount = 128; // Some value 0-255 where 0 is fully transparent and 255 is fully opaque
myImage.setAlpha(alphaAmount);