Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the opacity of an image.

Tags:

android

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
like image 260
Rahul Avatar asked Dec 11 '12 11:12

Rahul


People also ask

What is the opacity of an 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.


2 Answers

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"/>
like image 63
user2711011 Avatar answered Oct 09 '22 09:10

user2711011


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

like image 28
Zar E Ahmer Avatar answered Oct 09 '22 10:10

Zar E Ahmer