ColorOverlayDimmer.java revision cf94c5fa8ae8edb7e26a623133207415ceeed187
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 */ 14package android.support.v17.leanback.graphics; 15 16import android.content.Context; 17import android.graphics.Canvas; 18import android.graphics.Color; 19import android.graphics.Paint; 20import android.support.v17.leanback.R; 21import android.view.View; 22 23/** 24 * Helper class for assigning dim color to Paint. 25 * The class holds alpha value according to current active level. 26 */ 27public final class ColorOverlayDimmer { 28 29 private final float mActiveLevel; 30 private final float mDimmedLevel; 31 32 private final Paint mPaint; 33 34 private int mAlpha; 35 private float mAlphaFloat; 36 37 /** 38 * Constructor for this ColorOverlayDimmer class. 39 * 40 * @param dimColor The color for fully dimmed. Only r/g/b are used, alpha channel is 41 * ignored. 42 * @param activeLevel The Level of dimming for when the view is in its Active state. Must be a 43 * float value between 0.0 and 1.0. 44 * @param dimmedLevel The Level of dimming for when the view is in its Dimmed state. Must be a 45 * float value between 0.0 and 1.0. 46 */ 47 public static ColorOverlayDimmer createOverlayColorDimmer(int dimColor, float activeLevel, 48 float dimmedLevel) { 49 return new ColorOverlayDimmer(dimColor, activeLevel, dimmedLevel); 50 } 51 52 /** 53 * Constructor to create a default ColorOverlayDimmer. 54 */ 55 public static ColorOverlayDimmer createDefault(Context context) { 56 return new ColorOverlayDimmer( 57 context.getResources().getColor(R.color.lb_view_dim_mask_color), 0, 58 context.getResources().getFraction(R.dimen.lb_view_dimmed_level, 1, 1)); 59 } 60 61 private ColorOverlayDimmer(int dimColor, float activeLevel, float dimmedLevel) { 62 if (activeLevel < 0 || activeLevel > 1) { 63 throw new IllegalArgumentException("activeLevel must be between 0 and 1"); 64 } 65 if (dimmedLevel < 0 || dimmedLevel > 1) { 66 throw new IllegalArgumentException("dimmedLevel must be between 0 and 1"); 67 } 68 mPaint = new Paint(); 69 dimColor = Color.rgb(Color.red(dimColor), Color.green(dimColor), Color.blue(dimColor)); 70 mPaint.setColor(dimColor); 71 mActiveLevel = activeLevel; 72 mDimmedLevel = dimmedLevel; 73 } 74 75 /** 76 * Set level of active and change alpha value and paint object. 77 * @param level Between 0 for dim and 1 for fully active. 78 */ 79 public void setActiveLevel(float level) { 80 mAlphaFloat = (mDimmedLevel + level * (mActiveLevel - mDimmedLevel)); 81 mAlpha = (int) (255 * mAlphaFloat); 82 mPaint.setAlpha(mAlpha); 83 } 84 85 /** 86 * Returns true if dimmer needs to draw. 87 */ 88 public boolean needsDraw() { 89 return mAlpha != 0; 90 } 91 92 /** 93 * Returns the alpha value for dimmer. 94 */ 95 public int getAlpha() { 96 return mAlpha; 97 } 98 99 /** 100 * Returns the float value between 0~1, corresponding to alpha between 0~255. 101 */ 102 public float getAlphaFloat() { 103 return mAlphaFloat; 104 } 105 106 /** 107 * Returns the paint object set to current alpha value. 108 */ 109 public Paint getPaint() { 110 return mPaint; 111 } 112 113 /** 114 * Change r,g,b of color according to current dim level. Keeps alpha of color. 115 */ 116 public int applyToColor(int color) { 117 float f = 1 - mAlphaFloat; 118 return Color.argb(Color.alpha(color), 119 (int)(Color.red(color) * f), 120 (int)(Color.green(color) * f), 121 (int)(Color.blue(color) * f)); 122 } 123 124 /** 125 * Draw a dim color overlay on top of a child view inside canvas of parent view. 126 * @param c Canvas of parent view. 127 * @param v Child of parent view. 128 * @param includePadding Set to true to draw overlay on padding area of the view. 129 */ 130 public void drawColorOverlay(Canvas c, View v, boolean includePadding) { 131 c.save(); 132 float dx = v.getLeft() + v.getTranslationX(); 133 float dy = v.getTop() + v.getTranslationY(); 134 c.translate(dx, dy); 135 c.concat(v.getMatrix()); 136 c.translate(-dx, -dy); 137 if (includePadding) { 138 c.drawRect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom(), mPaint); 139 } else { 140 c.drawRect(v.getLeft() + v.getPaddingLeft(), 141 v.getTop() + v.getPaddingTop(), 142 v.getRight() - v.getPaddingRight(), 143 v.getBottom() - v.getPaddingBottom(), mPaint); 144 } 145 c.restore(); 146 } 147} 148