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...
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!
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.
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.
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.
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