2

In my app I want to change the ImageView like the following. I want only to change the image on the body.

enter image description here

I have followed the following link to change the alpha values by using the code like that

imageView.setAlpha(255);

But it is not working.

How to Set Opacity (Alpha) for View in Android. Please anybody suggest me how to do that.

Community
  • 1
  • 1
rams
  • 1,548
  • 7
  • 26
  • 45

4 Answers4

17

Maybe you can try like this, I had troubles doing it normal way but with "animation" it was ok.

AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F); // change values as you want
alpha.setDuration(0); // Make animation instant
alpha.setFillAfter(true); // Tell it to persist after the animation ends
// And then on your imageview
yourImageView.startAnimation(alpha);
Marko Niciforovic
  • 3,718
  • 2
  • 19
  • 28
  • thank u it is working fine.my requirement is i want different changes.how can i change that alpha values.please suggest me – rams Mar 26 '13 at 10:18
  • in AlphaAnimation(first parameter is starting point, so full alpha would be 1.0, second parameter is end point, where simple 0 would be transparent); – Marko Niciforovic Mar 26 '13 at 10:20
4

Or, without using animation you can just use setAlpha(float alpha) on your ImageView.

Ovidiu Latcu
  • 70,421
  • 14
  • 74
  • 84
  • setalpha is available for api 11+! – Arash Jan 29 '14 at 16:29
  • whenever I try to put alpha by this method I cant see any changes! iv.setAlpha(50f) or iv.setImageAlpha(50) nothing happens ? WHY – Dr. aNdRO Mar 28 '14 at 05:25
  • As written in android developer doc : . This is a value from 0 to 1, where 0 means the view is completely transparent and 1 means the view is completely opaque. You should try with setAlpha(0.2f) – phyzalis Apr 04 '14 at 10:45
4

It might be because setAlpha(float Alpha) has been deprecated as of API level 16 by setImageAlpha(float Alpha)

7heViking
  • 6,529
  • 11
  • 48
  • 88
1

Opacity ranges between 0 to 1, and you have given the value as 255,
imageView.setAlpha(255).

So give the value between 0 to 1, such as when

you want the opacity of 50%, give 0.5. imageView.setAlpha(0.5f)

Also you can do it in XML file as follows:

android:layout_width="100dip"
android:layout_height="100dip"
android:id="@+id/imageViewIcon"
android:src="@drawable/icon"
android:alpha=".5"

Amit Anand
  • 11
  • 2