Example usage for android.graphics.drawable InsetDrawable getDrawable

List of usage examples for android.graphics.drawable InsetDrawable getDrawable

Introduction

In this page you can find the example usage for android.graphics.drawable InsetDrawable getDrawable.

Prototype

@Nullable
public Drawable getDrawable() 

Source Link

Usage

From source file:com.forrestguice.suntimeswidget.SuntimesUtils.java

/**
 * @param context context used to get resources
 * @param resourceID drawable resource ID to an InsetDrawable
 * @param fillColor fill color to apply to drawable
 * @param strokeColor stroke color to apply to drawable
 * @param strokePx width of stroke/*from  w w  w.j a  v  a 2 s.co  m*/
 * @return a Bitmap of the drawable
 */
public static Bitmap insetDrawableToBitmap(Context context, int resourceID, int fillColor, int strokeColor,
        int strokePx) {
    Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), resourceID, null);
    InsetDrawable inset = (InsetDrawable) drawable;

    int w = 1, h = 1;
    if (inset != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Drawable wrapped = inset.getDrawable();
            if (wrapped != null) {
                w = wrapped.getIntrinsicWidth();
                h = wrapped.getIntrinsicHeight();
            }
        } else {
            w = inset.getIntrinsicWidth();
            h = inset.getIntrinsicHeight();
        }
    }

    Drawable tinted = tintDrawable(inset, fillColor, strokeColor, strokePx);
    return drawableToBitmap(context, tinted, w, h, true);
}

From source file:com.forrestguice.suntimeswidget.SuntimesUtils.java

/**
 * @param drawable a ShapeDrawable//ww  w.  j  a v  a  2  s .  co  m
 * @param fillColor the fill color
 * @param strokeColor the stroke color
 * @return a GradientDrawable with the given fill and stroke
 */
public static Drawable tintDrawable(InsetDrawable drawable, int fillColor, int strokeColor, int strokePixels) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        try {
            GradientDrawable gradient = (GradientDrawable) drawable.getDrawable();
            if (gradient != null) {
                SuntimesUtils.tintDrawable(gradient, fillColor, strokeColor, strokePixels);
                return drawable;

            } else {
                Log.w("tintDrawable", "failed to apply color! Null inset drawable.");
                return drawable;
            }
        } catch (ClassCastException e) {
            Log.w("tintDrawable", "failed to apply color! " + e);
            return drawable;
        }
    } else {
        Log.w("tintDrawable", "failed to apply color! InsetDrawable.getDrawable requires api 19+");
        return drawable; // not supported
    }
}