2

I have a drawable object in my Activity. setAlpha() for drawable was introduced in API level 1 but getAlpha() was introduced in API level 19(KitKat). Is there any alternative API or support library to get the alpha/opacity value of the drawable.

And also I have NineOldAndroids library, but the getAlpha() works for only VIEWS.

Mir-Ismaili
  • 13,974
  • 8
  • 82
  • 100
Dileep Perla
  • 1,865
  • 7
  • 33
  • 54

3 Answers3

6

There is no general way of getting the alpha value of a Drawable prior of API 19. Anyway, according to what kind of Drawable you have, you can check the source code to deduct the alpha with a workaround.

For instance looking at the ColorDrawable source it's easy to see that you can port the implemention prior to Kitkat.

@Override
public int getAlpha() {
    return mColorState.mUseColor >>> 24;
}

so drawable.getAlpha() becomes drawble.getColor() >>> 24

EDIT:

here is a uncomplete attempt to make a compat method, i'll try to update this with time:

    public static int getAlphaCompat( Drawable drawable ) {
    if (VERSION.SDK_INT >= VERSION_CODES.KITKAT)
        return drawable.getAlpha();

    if( drawable instanceof ColorDrawable ) {
        return ((ColorDrawable) drawable).getColor() >>> 24;
    } else if( drawable instanceof BitmapDrawable ) {
        return ((BitmapDrawable) drawable).getPaint().getAlpha();
    } else if( drawable instanceof RotateDrawable ) {
        return getAlphaCompat( ((RotateDrawable) drawable).getDrawable() );
    } else if( drawable instanceof ScaleDrawable ) {
        return getAlphaCompat( ((ScaleDrawable) drawable).getDrawable() );
    } else if( drawable instanceof ClipDrawable ) {
        //TODO: possible with reflection
    } else if( drawable instanceof  ShapeDrawable ) {
        //TODO: possible with reflection
    } else if( drawable instanceof DrawableContainer ) {
       //TODO: possible with reflection
    } else if( drawable instanceof GradientDrawable ) {
        //TODO: possible with reflection
    }

    return -1;
}
Mario Lenci
  • 10,422
  • 5
  • 39
  • 50
  • I suggest insert `if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) return drawable.getAlpha();` at the first. – Mir-Ismaili Jul 26 '18 at 03:36
  • And `return innerDrawable == null ? 255 : getAlphaCompat(innerDrawable)` for `RotateDrawable` and `ScaleDrawable` where `innerDrawable` is `drawable.getDrawable()`. – Mir-Ismaili Jul 26 '18 at 03:40
5

Adding this library to the build.gradle dependencies section:

com.android.support:support-v4:24.0.0

The next method becomes available on API level 4 and up:

DrawableCompat.getAlpha(Drawable d)

[Edit] Attention DrawableCompat.getAlpha just returns 0 for versions below 19. See source code

MyDogTom
  • 4,486
  • 1
  • 28
  • 41
josue.0
  • 775
  • 1
  • 10
  • 23
  • 2
    The implementation for this for pre-19/KitKat SDKs is simply `return 0;` - beware this won't return the actual alpha value before KitKat. – Adam S Oct 24 '16 at 13:54
-2

Try this way,hope this will help you to solve your problem.

        if(Build.VERSION.SDK_INT<Build.VERSION_CODES.HONEYCOMB){
            v.setAlpha((Math.round(alpha*255)));
        }else{
            v.setAlpha(alpha);
        }
Javier
  • 23
  • 2
  • 9