ColorFilterDimmer.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.ColorFilter; 18import android.graphics.Paint; 19import android.view.View; 20import android.support.v17.leanback.R; 21 22/** 23 * Helper class for applying dim level to view(s). The ColorFilterDimmer 24 * holds a Paint object and ColorFilter corresponding to current "active" level. 25 */ 26public final class ColorFilterDimmer { 27 28 private final ColorFilterCache mColorDimmer; 29 30 private final float mActiveLevel; 31 private final float mDimmedLevel; 32 33 private final Paint mPaint; 34 private ColorFilter mFilter; 35 36 /** 37 * Create a default ColorFilterDimmer. 38 */ 39 public static ColorFilterDimmer createDefault(Context context) { 40 return new ColorFilterDimmer(ColorFilterCache.getColorFilterCache( 41 context.getResources().getColor(R.color.lb_view_dim_mask_color)), 42 0, context.getResources().getFraction(R.dimen.lb_view_dimmed_level, 1, 1)); 43 } 44 45 /** 46 * Create a ColorFilterDimmer. 47 * 48 * @param dimmer The ColorFilterCache for dim color. 49 * @param activeLevel The level of dimming for when the view is in its active state. Must be a 50 * float value between 0.0 and 1.0. 51 * @param dimmedLevel The level of dimming for when the view is in its dimmed state. Must be a 52 * float value between 0.0 and 1.0. 53 */ 54 public static ColorFilterDimmer create(ColorFilterCache dimmer, 55 float activeLevel, float dimmedLevel) { 56 return new ColorFilterDimmer(dimmer, activeLevel, dimmedLevel); 57 } 58 59 private ColorFilterDimmer(ColorFilterCache dimmer, float activeLevel, float dimmedLevel) { 60 mColorDimmer = dimmer; 61 mActiveLevel = activeLevel; 62 mDimmedLevel = dimmedLevel; 63 mPaint = new Paint(); 64 } 65 66 /** 67 * Apply current ColorFilter to a view, will assign and remove hardware layer of the view. 68 */ 69 public void applyFilterToView(View view) { 70 if (mFilter != null) { 71 view.setLayerType(View.LAYER_TYPE_HARDWARE, mPaint); 72 } else { 73 view.setLayerType(View.LAYER_TYPE_NONE, null); 74 } 75 // FIXME: Current framework has bug that not triggering invalidate when change layer 76 // paint. Will add conditional sdk version check once bug is fixed in released 77 // framework. 78 view.invalidate(); 79 } 80 81 /** 82 * Sets the active level and change internal filter and paint. 83 * @param level Between 0 for dim and 1 for fully active. 84 */ 85 public void setActiveLevel(float level) { 86 mFilter = mColorDimmer.getFilterForLevel( 87 mDimmedLevel + level * (mActiveLevel - mDimmedLevel)); 88 mPaint.setColorFilter(mFilter); 89 } 90 91 /** 92 * Gets the color filter set to current dim level. 93 */ 94 public ColorFilter getColorFilter() { 95 return mFilter; 96 } 97 98 /** 99 * Gets the paint object set to current dim level. 100 */ 101 public Paint getPaint() { 102 return mPaint; 103 } 104 105} 106