From c1e1395b30358ec317f294ceb589263ff6ac3abd Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Sat, 27 Aug 2016 18:14:58 -0400 Subject: [PATCH] Add support background setting --- .../lightning/utils/DrawableUtils.java | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/utils/DrawableUtils.java b/app/src/main/java/acr/browser/lightning/utils/DrawableUtils.java index 49bb6b5..93fd7de 100644 --- a/app/src/main/java/acr/browser/lightning/utils/DrawableUtils.java +++ b/app/src/main/java/acr/browser/lightning/utils/DrawableUtils.java @@ -1,5 +1,7 @@ package acr.browser.lightning.utils; +import android.content.Context; +import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; @@ -7,6 +9,13 @@ import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.RectF; import android.graphics.Typeface; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.support.annotation.AttrRes; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.TypedValue; +import android.view.View; public class DrawableUtils { @@ -65,10 +74,29 @@ public class DrawableUtils { int endG = (endInt >> 8) & 0xff; int endB = endInt & 0xff; - return (startA + (int)(fraction * (endA - startA))) << 24 | - (startR + (int)(fraction * (endR - startR))) << 16 | - (startG + (int)(fraction * (endG - startG))) << 8 | - (startB + (int)(fraction * (endB - startB))); + return (startA + (int) (fraction * (endA - startA))) << 24 | + (startR + (int) (fraction * (endR - startR))) << 16 | + (startG + (int) (fraction * (endG - startG))) << 8 | + (startB + (int) (fraction * (endB - startB))); + } + + public static Drawable resolveDrawableAttribute(@NonNull Context context, @AttrRes int res) { + int[] attribute = new int[]{res}; + int indexOfAttrTextSize = 0; + TypedValue typedValue = new TypedValue(); + TypedArray a = context.obtainStyledAttributes(typedValue.data, attribute); + Drawable drawable = a.getDrawable(indexOfAttrTextSize); + a.recycle(); + return drawable; + } + + public static void setBackground(@NonNull View view, @Nullable Drawable drawable) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + view.setBackground(drawable); + } else { + //noinspection deprecation + view.setBackgroundDrawable(drawable); + } } }