Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Image transparency - alternative for deprecated setAlpha for API < 11

I want to set transparency on background image. I tried android:alpha in xml, but it does nothing. I use setAlpha(int alpha), and works good, but it is deprecated, so I want another solution (because it might be for example removed in future). There is setImageAlpha and setAlpha(float alpha), but these are not available in api lvl 10 (and that is my target). So my question is: What is the replacement for setAlpha(int alpha) for devices with API 10 or lower (and I just want to mention, that more than 1/4 of all the devices use API 10 or lower). As I checked on stackoverflow, most people still suggest setAlpha(int alpha), but as I said - it is deprecated...

like image 948
user2855896 Avatar asked Jan 11 '14 15:01

user2855896


4 Answers

ImageView setAlpha was not deprecated until API level 16. The correct to handle this situation is to continue using it until you can target a minimum SDK level 16, and at that point, simply switch to View setAlpha.

Deprecation just means to not use in new development if you have an alternative. Since Google has not provided an alternative (i.e., via compatability library) they certainly would not remove it and break untold applications already on your phone.

Consider yourself lucky you are dealing with ImageView because it has a setAlpha you can use!

like image 165
Greg Ennis Avatar answered Nov 13 '22 20:11

Greg Ennis


You should use ImageView.setAlpha(int) in API < 16 and ImageView.setImageAlpha(int) in API 16+. They are the same methods.

ImageView.setAlpha(int) has been renamed to ImageView.setImageAlpha(int) to avoid confusion with View.setAlpha(float) which does not do the same thing. The former changes the alpha of the source image while drawing it (which is a relatively cheap operation) while the latter changes the alpha of the whole view which requires allocating a temporary buffer to draw the view and apply alpha to that buffer.

like image 31
BladeCoder Avatar answered Nov 13 '22 20:11

BladeCoder


ImageView iv_object;

void updateAlpha(View v)
{

  v.setAlpha(1);
}

I fixed the deprecation issue by updateAlpha(iv_object) since setAlpha() is not deprecated for View. Correct me if i am wrong ant any point. The advantage is pass any View class object to that method.

like image 1
Denny Mathew Avatar answered Nov 13 '22 20:11

Denny Mathew


This works fine for me:

public void setAlpha (float alpha) Added in API level 11

Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is completely transparent and 1 means the view is completely opaque.

like image 1
hannunehg Avatar answered Nov 13 '22 22:11

hannunehg