Example usage for android.graphics.drawable ColorDrawable getClass

List of usage examples for android.graphics.drawable ColorDrawable getClass

Introduction

In this page you can find the example usage for android.graphics.drawable ColorDrawable getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

public static int colorDrawableGetColor(ColorDrawable d) {
    try {//from w  w  w  .  j  a v  a2  s  .  c  o  m
        Method getColor = d.getClass().getMethod("getColor");
        return (Integer) getColor.invoke(d);
    } catch (NoSuchMethodException ignore) {
    } catch (InvocationTargetException ignore) {
    } catch (IllegalAccessException ignore) {
    } catch (ClassCastException ignore) {
    }

    // For some reason the following doesn't work on Android 15, but the above does, and the below
    // works for Android <= 10, so the function as a whole works but is a dirty hack.

    Bitmap b = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    d.draw(new Canvas(b));
    return b.getPixel(0, 0);
}

From source file:com.xperia64.timidityae.Globals.java

@SuppressLint("NewApi")
public static int getBackgroundColor(TextView textView) {
    Drawable drawable = textView.getBackground();
    if (drawable instanceof ColorDrawable) {
        ColorDrawable colorDrawable = (ColorDrawable) drawable;
        if (Build.VERSION.SDK_INT >= 11) {
            return colorDrawable.getColor();
        }//from   w w w .j a  va  2s.  c  om
        try {
            Field field = colorDrawable.getClass().getDeclaredField("mState");
            field.setAccessible(true);
            Object object = field.get(colorDrawable);
            field = object.getClass().getDeclaredField("mUseColor");
            field.setAccessible(true);
            return field.getInt(object);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    return 0;
}

From source file:ggikko.me.steppertest.stepper.RoundedView.java

private int getBackgroundColor(View view) {
    ColorDrawable drawable = (ColorDrawable) view.getBackground();
    if (drawable != null) {
        if (Build.VERSION.SDK_INT >= 11) {
            return drawable.getColor();
        }//from ww  w  . j a v  a2 s.  c  o  m
        try {
            Field field = drawable.getClass().getDeclaredField("mState");
            field.setAccessible(true);
            Object object = field.get(drawable);
            field = object.getClass().getDeclaredField("mUseColor");
            field.setAccessible(true);
            return field.getInt(object);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    return 0;
}

From source file:despotoski.nikola.github.com.bottomnavigationlayout.BottomTabLayout.java

public int getBackgroundColor() {
    Drawable drawable = getBackground();
    if (drawable instanceof ColorDrawable) {
        ColorDrawable colorDrawable = (ColorDrawable) drawable;
        if (Build.VERSION.SDK_INT >= 11) {
            return colorDrawable.getColor();
        }/*from w ww .  j  av  a 2s.  c o  m*/
        try {
            Field field = colorDrawable.getClass().getDeclaredField("mState");
            field.setAccessible(true);
            Object object = field.get(colorDrawable);
            field = object.getClass().getDeclaredField("mUseColor");
            field.setAccessible(true);
            return field.getInt(object);
        } catch (Exception ignore) {
        }
    }
    return Color.TRANSPARENT;
}