1 /* 2 * Copyright (C) 2015 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 androidx.leanback.widget; 15 16 import android.graphics.Outline; 17 import android.view.View; 18 import android.view.ViewOutlineProvider; 19 20 import androidx.annotation.RequiresApi; 21 22 @RequiresApi(21) 23 class ShadowHelperApi21 { 24 25 static class ShadowImpl { 26 View mShadowContainer; 27 float mNormalZ; 28 float mFocusedZ; 29 } 30 31 static final ViewOutlineProvider sOutlineProvider = new ViewOutlineProvider() { 32 @Override 33 public void getOutline(View view, Outline outline) { 34 outline.setRect(0, 0, view.getWidth(), view.getHeight()); 35 outline.setAlpha(1.0f); 36 } 37 }; 38 39 /* add shadows and return a implementation detail object */ 40 public static Object addDynamicShadow( 41 View shadowContainer, float unfocusedZ, float focusedZ, int roundedCornerRadius) { 42 if (roundedCornerRadius > 0) { 43 RoundedRectHelperApi21.setClipToRoundedOutline(shadowContainer, true, 44 roundedCornerRadius); 45 } else { 46 shadowContainer.setOutlineProvider(sOutlineProvider); 47 } 48 ShadowImpl impl = new ShadowImpl(); 49 impl.mShadowContainer = shadowContainer; 50 impl.mNormalZ = unfocusedZ; 51 impl.mFocusedZ = focusedZ; 52 shadowContainer.setZ(impl.mNormalZ); 53 return impl; 54 } 55 56 /* set shadow focus level 0 for unfocused 1 for fully focused */ 57 public static void setShadowFocusLevel(Object object, float level) { 58 ShadowImpl impl = (ShadowImpl) object; 59 impl.mShadowContainer.setZ(impl.mNormalZ + level * (impl.mFocusedZ - impl.mNormalZ)); 60 } 61 62 private ShadowHelperApi21() { 63 } 64 } 65