Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set alpha on ImageView without setAlpha

Can I programatically set the alpha of an ImageView without the need for being dependent on API level 11 where setAlpha was introduced?

like image 654
joynes Avatar asked Mar 13 '12 21:03

joynes


People also ask

How do you set up Alpha?

To get α subtract your confidence level from 1. For example, if you want to be 95 percent confident that your analysis is correct, the alpha level would be 1 – . 95 = 5 percent, assuming you had a one tailed test. For two-tailed tests, divide the alpha level by 2.

How do you change opacity in XML?

For a fully opaque option i.e. 100% opacity, you do not need to use #FFC0C0C0 or #100C0C0C0(Note: this would show nothing). Just leave it as #C0C0C0 instead. The alpha channel A is a hex value, just like the RGB channels. #50C0C0C0 will give an opacity of about 30%, not 50%.

How do I change the opacity of a picture on my Android?

Tap the image you want to adjust. Tap Recolor to choose a filter. Tap Adjustments to adjust transparency, brightness, and contrast.


2 Answers

ImageView has a method setAlpha(int) since API leve 1. So you can use it in any API level.
It is the setAlpha(float) method of View which is introduced in API level 11.

like image 112
Muhammad Nabeel Arif Avatar answered Jan 22 '23 02:01

Muhammad Nabeel Arif


I used code to setAlpha of the image itself, not the view. This is available from API level 1..

public void toggleButton(int i) {
    if (indImageBtnEnabled[i]) {

        int di = getDrawableId(findViewById(myImagebtns[i]));
        Drawable d = this.getResources().getDrawable(di);
        d.setAlpha(25);

        ((ImageView) findViewById(myImagebtns[i])).setImageDrawable(d);

        indImageBtnEnabled[i] = false;
    } else {
        // findViewById(myImagebtns[i]).setAlpha(1f) << NEEDS API11;
        int di = getDrawableId(findViewById(myImagebtns[i]));
        Drawable d = this.getResources().getDrawable(di);
        d.setAlpha(255);

        ((ImageView) findViewById(myImagebtns[i])).setImageDrawable(d);

        indImageBtnEnabled[i] = true;
    }
}
like image 32
Kees Koenen Avatar answered Jan 22 '23 03:01

Kees Koenen