I have an background image set to an ImageView
, Now i want to change the opacity of an image without, for that i write this code to change the opacity of ImageView, but when i do so it remove the background image from the image view, So my question is how can i change the opacity of ImageView without removing background image form it.
Code i used is:
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setBackgroundResource(R.drawable.theme1_page_header); // Set background image
int opacity = 100; // from 0 to 255
imageView.setBackgroundColor(opacity * 0x1000000); // change opacity of image
Opacity is the extent to which something blocks light. You can change the opacity of layers, filters, and effects so that more (or less) of the underlying image shows through. "TASTY" becomes transparent when its opacity is set to 50%. Note that "SANDWICHES" opacity is set to 100% so it is opaque, or not transparent.
the most important part of alpha is that the value has to be decimal
0 = transparent and 1 = visible
so 0.5 is half way visible
in the XML you can do
<ImageView
android:layout_width="30dp"
android:layout_height="35dp"
android:id="@+id/imageView"
android:alpha="0.4" // <----------------- this is the fun part
android:layout_alignParentRight="false"
android:background="@drawable/imagename"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_alignWithParentIfMissing="false"
android:layout_marginLeft="100dp"
android:layout_alignParentBottom="false"
android:layout_alignParentStart="false"
android:layout_alignTop="@+id/bar"
android:layout_marginTop="30dp"/>
For Api >=16 . Use setImageAlpha as your practice because setAlpha will be deprecated in coming future. `
ImageView.setAlpha(int)
has been renamed
to ImageView.setImageAlpha(int)
to avoid confusion. See Detail Explanation here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With