10

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
Daniel PP Cabral
  • 1,604
  • 1
  • 13
  • 19
Rahul
  • 1,667
  • 6
  • 21
  • 38

7 Answers7

33

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"/>
Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
user2711011
  • 331
  • 3
  • 4
11

You can use

imageView.setAlpha(yourValue); //  some value 0-255 where 0 is fully transparent and 255 is fully opaque

See the documentation

Renjith
  • 5,783
  • 9
  • 31
  • 42
5
ImageView imageView = (ImageView) findViewById(R.id.image_view);
Drawable dPage_header= getResources().getDrawable(R.drawable.theme1_page_header);

// setting the opacity (alpha)
dPage_header.setAlpha(10);

// setting the images on the ImageViews
imageView.setImageDrawable(dPage_header);
Talha
  • 12,673
  • 5
  • 49
  • 68
  • But it is not about your first question. You can do this by checking your work percentage. you can create an method which return complatedWorkPercentage() and gives this valu to dPage_header.setAlpha(complatedWorkPercentage()); – Talha Dec 11 '12 at 11:51
  • i dont know what is the work ? Could you send me detail email to talhakosen@gmail.com – Talha Dec 11 '12 at 11:55
  • Hi talhakosen...i have send a reply to ur mail..can u please check it out – Rahul Dec 12 '12 at 07:49
2

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

Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
1

Use this option in layout xml for opacity as well as to turn the Android Material icons from Black to Grey. android:alpha=0.5

Sachiin Gupta
  • 378
  • 2
  • 9
1

In Kotlin we can change alpha in code like this:

myImageView.alpha = 0.5f

Change alpha value to whatever you want between 0.0f to 1f

ShadeToD
  • 403
  • 9
  • 22
0

Option 1

Use imageView.setAlpha(100).

If you're using Android 2.3 you have to do a horrible hack with a zero-length animation using nineolddroids.

Option 2

Subclass ImageView and override its onDraw() method to draw the image transparently.

Option 3

Actually modify the image pixels using get/setPixel(). This'll be very slow though; there's probably a faster way to do this (e.g. using renderscript).

Timmmm
  • 88,195
  • 71
  • 364
  • 509