ShadowOverlayContainer.java revision 9240e796bc63422c28f2707840bd99c48573279b
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.widget; 15 16import android.content.Context; 17import android.support.v17.leanback.R; 18import android.util.AttributeSet; 19import android.view.LayoutInflater; 20import android.view.View; 21import android.view.ViewGroup; 22import android.widget.FrameLayout; 23 24/** 25 * ShadowOverlayContainer Provides a SDK version independent wrapper container 26 * to take care of shadow and/or color overlay. 27 * <p> 28 * Shadow and color dimmer overlay are both optional. When shadow is used, it's 29 * user's responsibility to properly call setClipChildren(false) on parent views if 30 * the shadow can appear outside bounds of parent views. 31 * {@link #prepareParentForShadow(ViewGroup)} must be called on parent of container 32 * before using shadow. Depending on sdk version, optical bounds might be applied 33 * to parent. 34 * </p> 35 * <p> 36 * {@link #initialize(boolean, boolean)} must be first called on the container to initialize 37 * shadows and/or color overlay. Then call {@link #wrap(View)} to insert wrapped view 38 * into container. 39 * </p> 40 * <p> 41 * Call {@link #setShadowFocusLevel(float)} to control shadow alpha. 42 * </p> 43 * <p> 44 * Call {@link #setOverlayColor(int)} to control overlay color. 45 * </p> 46 */ 47public class ShadowOverlayContainer extends FrameLayout { 48 49 private boolean mInitialized; 50 private View mColorDimOverlay; 51 private Object mShadowImpl; 52 53 public ShadowOverlayContainer(Context context) { 54 this(context, null, 0); 55 } 56 57 public ShadowOverlayContainer(Context context, AttributeSet attrs) { 58 this(context, attrs, 0); 59 } 60 61 public ShadowOverlayContainer(Context context, AttributeSet attrs, int defStyle) { 62 super(context, attrs, defStyle); 63 } 64 65 /** 66 * Return true if the platform sdk supports shadow. 67 */ 68 public static boolean supportsShadow() { 69 return ShadowHelper.getInstance().supportsShadow(); 70 } 71 72 /** 73 * {@link #prepareParentForShadow(ViewGroup)} must be called on parent of container 74 * before using shadow. Depending on sdk version, optical bounds might be applied 75 * to parent. 76 */ 77 public static void prepareParentForShadow(ViewGroup parent) { 78 ShadowHelper.getInstance().prepareParent(parent); 79 } 80 81 /** 82 * Initialize shadows and/or color overlay. Both are optional. 83 */ 84 public void initialize(boolean hasShadow, boolean hasColorDimOverlay) { 85 if (mInitialized) { 86 throw new IllegalStateException(); 87 } 88 mInitialized = true; 89 if (hasShadow) { 90 mShadowImpl = ShadowHelper.getInstance().addShadow(this); 91 } 92 if (hasColorDimOverlay) { 93 mColorDimOverlay = LayoutInflater.from(getContext()) 94 .inflate(R.layout.lb_card_color_overlay, this, false); 95 addView(mColorDimOverlay); 96 } 97 } 98 99 /** 100 * Set shadow focus level (0 to 1). 0 for unfocused, 1f for fully focused. 101 */ 102 public void setShadowFocusLevel(float level) { 103 if (mShadowImpl != null) { 104 if (level < 0f) { 105 level = 0f; 106 } else if (level > 1f) { 107 level = 1f; 108 } 109 ShadowHelper.getInstance().setShadowFocusLevel(mShadowImpl, level); 110 } 111 } 112 113 /** 114 * Set color (with alpha) of the overlay. 115 */ 116 public void setOverlayColor(int overlayColor) { 117 if (mColorDimOverlay != null) { 118 mColorDimOverlay.setBackgroundColor(overlayColor); 119 } 120 } 121 122 /** 123 * Inserts view into the wrapper. 124 */ 125 public void wrap(View view) { 126 if (!mInitialized) { 127 throw new IllegalStateException(); 128 } 129 if (mColorDimOverlay != null) { 130 addView(view, indexOfChild(mColorDimOverlay)); 131 } else { 132 addView(view); 133 } 134 } 135 136} 137