get Color from ColorDrawable - Android Graphics

Android examples for Graphics:Drawable

Description

get Color from ColorDrawable

Demo Code


//package com.java2s;
import android.graphics.drawable.ColorDrawable;
import java.lang.reflect.Field;

public class Main {
    public static int getColor(ColorDrawable drawable) {
        int color = 0;
        try {//from   w w  w  .  j a  v a2  s .  c om
            Field mStateField = drawable.getClass().getDeclaredField(
                    "mState");
            mStateField.setAccessible(true);
            Object colorState = mStateField.get(drawable);
            Field mBaseColorField = colorState.getClass().getDeclaredField(
                    "mBaseColor");
            mBaseColorField.setAccessible(true);
            color = mBaseColorField.getInt(colorState);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return color;
    }
}

Related Tutorials