1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package android.support.v17.leanback.graphics; 15 16 import android.content.Context; 17 import android.content.res.TypedArray; 18 import android.graphics.Canvas; 19 import android.graphics.Color; 20 import android.graphics.Paint; 21 import android.support.v17.leanback.R; 22 import android.view.View; 23 24 /** 25 * Helper class for assigning a dim color to Paint. It holds the alpha value for 26 * the current active level. 27 */ 28 public final class ColorOverlayDimmer { 29 30 private final float mActiveLevel; 31 private final float mDimmedLevel; 32 33 private final Paint mPaint; 34 35 private int mAlpha; 36 private float mAlphaFloat; 37 38 /** 39 * Creates a default ColorOverlayDimmer. 40 */ createDefault(Context context)41 public static ColorOverlayDimmer createDefault(Context context) { 42 TypedArray a = context.obtainStyledAttributes(R.styleable.LeanbackTheme); 43 44 int dimColor = a.getColor(R.styleable.LeanbackTheme_overlayDimMaskColor, 45 context.getResources().getColor(R.color.lb_view_dim_mask_color)); 46 float activeLevel = a.getFraction(R.styleable.LeanbackTheme_overlayDimActiveLevel, 1, 1, 47 context.getResources().getFraction(R.fraction.lb_view_active_level, 1, 0)); 48 float dimmedLevel = a.getFraction(R.styleable.LeanbackTheme_overlayDimDimmedLevel, 1, 1, 49 context.getResources().getFraction(R.fraction.lb_view_dimmed_level, 1, 1)); 50 a.recycle(); 51 return new ColorOverlayDimmer(dimColor, activeLevel, dimmedLevel); 52 } 53 54 /** 55 * Creates a ColorOverlayDimmer for the given color and levels. 56 * 57 * @param dimColor The color for fully dimmed. Only the RGB values are 58 * used; the alpha channel is ignored. 59 * @param activeLevel The level of dimming when the View is in its active 60 * state. Must be a float value between 0.0 and 1.0. 61 * @param dimmedLevel The level of dimming when the View is in its dimmed 62 * state. Must be a float value between 0.0 and 1.0. 63 */ createColorOverlayDimmer(int dimColor, float activeLevel, float dimmedLevel)64 public static ColorOverlayDimmer createColorOverlayDimmer(int dimColor, float activeLevel, 65 float dimmedLevel) { 66 return new ColorOverlayDimmer(dimColor, activeLevel, dimmedLevel); 67 } 68 ColorOverlayDimmer(int dimColor, float activeLevel, float dimmedLevel)69 private ColorOverlayDimmer(int dimColor, float activeLevel, float dimmedLevel) { 70 if (activeLevel > 1.0f) activeLevel = 1.0f; 71 if (activeLevel < 0.0f) activeLevel = 0.0f; 72 if (dimmedLevel > 1.0f) dimmedLevel = 1.0f; 73 if (dimmedLevel < 0.0f) dimmedLevel = 0.0f; 74 mPaint = new Paint(); 75 dimColor = Color.rgb(Color.red(dimColor), Color.green(dimColor), Color.blue(dimColor)); 76 mPaint.setColor(dimColor); 77 mActiveLevel = activeLevel; 78 mDimmedLevel = dimmedLevel; 79 setActiveLevel(1); 80 } 81 82 /** 83 * Sets the active level of the dimmer. Updates the alpha value based on the 84 * level. 85 * 86 * @param level A float between 0 (fully dim) and 1 (fully active). 87 */ setActiveLevel(float level)88 public void setActiveLevel(float level) { 89 mAlphaFloat = (mDimmedLevel + level * (mActiveLevel - mDimmedLevel)); 90 mAlpha = (int) (255 * mAlphaFloat); 91 mPaint.setAlpha(mAlpha); 92 } 93 94 /** 95 * Returns whether the dimmer needs to draw. 96 */ needsDraw()97 public boolean needsDraw() { 98 return mAlpha != 0; 99 } 100 101 /** 102 * Returns the alpha value for the dimmer. 103 */ getAlpha()104 public int getAlpha() { 105 return mAlpha; 106 } 107 108 /** 109 * Returns the float value between 0 and 1 corresponding to alpha between 110 * 0 and 255. 111 */ getAlphaFloat()112 public float getAlphaFloat() { 113 return mAlphaFloat; 114 } 115 116 /** 117 * Returns the Paint object set to the current alpha value. 118 */ getPaint()119 public Paint getPaint() { 120 return mPaint; 121 } 122 123 /** 124 * Change the RGB of the color according to current dim level. Maintains the 125 * alpha value of the color. 126 * 127 * @param color The color to apply the dim level to. 128 * @return A color with the RGB values adjusted by the alpha of the current 129 * dim level. 130 */ applyToColor(int color)131 public int applyToColor(int color) { 132 float f = 1 - mAlphaFloat; 133 return Color.argb(Color.alpha(color), 134 (int)(Color.red(color) * f), 135 (int)(Color.green(color) * f), 136 (int)(Color.blue(color) * f)); 137 } 138 139 /** 140 * Draw a dim color overlay on top of a child View inside the canvas of 141 * the parent View. 142 * 143 * @param c Canvas of the parent View. 144 * @param v A child of the parent View. 145 * @param includePadding Set to true to draw overlay on padding area of the 146 * View. 147 */ drawColorOverlay(Canvas c, View v, boolean includePadding)148 public void drawColorOverlay(Canvas c, View v, boolean includePadding) { 149 c.save(); 150 float dx = v.getLeft() + v.getTranslationX(); 151 float dy = v.getTop() + v.getTranslationY(); 152 c.translate(dx, dy); 153 c.concat(v.getMatrix()); 154 c.translate(-dx, -dy); 155 if (includePadding) { 156 c.drawRect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom(), mPaint); 157 } else { 158 c.drawRect(v.getLeft() + v.getPaddingLeft(), 159 v.getTop() + v.getPaddingTop(), 160 v.getRight() - v.getPaddingRight(), 161 v.getBottom() - v.getPaddingBottom(), mPaint); 162 } 163 c.restore(); 164 } 165 } 166