ShadowHelperApi21.java revision 254b417129de2a8c5612826a152f8a26c8f1d0e8
119312c5f247559a9483d874e62150c49d36aa478Dake Gu/* 2254b417129de2a8c5612826a152f8a26c8f1d0e8Dake Gu * Copyright (C) 2015 The Android Open Source Project 319312c5f247559a9483d874e62150c49d36aa478Dake Gu * 419312c5f247559a9483d874e62150c49d36aa478Dake Gu * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 519312c5f247559a9483d874e62150c49d36aa478Dake Gu * in compliance with the License. You may obtain a copy of the License at 619312c5f247559a9483d874e62150c49d36aa478Dake Gu * 719312c5f247559a9483d874e62150c49d36aa478Dake Gu * http://www.apache.org/licenses/LICENSE-2.0 819312c5f247559a9483d874e62150c49d36aa478Dake Gu * 919312c5f247559a9483d874e62150c49d36aa478Dake Gu * Unless required by applicable law or agreed to in writing, software distributed under the License 1019312c5f247559a9483d874e62150c49d36aa478Dake Gu * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 1119312c5f247559a9483d874e62150c49d36aa478Dake Gu * or implied. See the License for the specific language governing permissions and limitations under 1219312c5f247559a9483d874e62150c49d36aa478Dake Gu * the License. 1319312c5f247559a9483d874e62150c49d36aa478Dake Gu */ 1419312c5f247559a9483d874e62150c49d36aa478Dake Gupackage android.support.v17.leanback.widget; 1519312c5f247559a9483d874e62150c49d36aa478Dake Gu 1619312c5f247559a9483d874e62150c49d36aa478Dake Guimport android.support.v17.leanback.R; 1719312c5f247559a9483d874e62150c49d36aa478Dake Guimport android.content.res.Resources; 1819312c5f247559a9483d874e62150c49d36aa478Dake Guimport android.graphics.Color; 19b10662684a404989de6058e146565e5f39e7897dDake Guimport android.graphics.Outline; 2019312c5f247559a9483d874e62150c49d36aa478Dake Guimport android.graphics.drawable.ColorDrawable; 2119312c5f247559a9483d874e62150c49d36aa478Dake Guimport android.graphics.drawable.Drawable; 2269e74bd8956577d9a3414b81ec661fd5fee42e19Craig Stoutimport android.view.View; 23254b417129de2a8c5612826a152f8a26c8f1d0e8Dake Guimport android.view.ViewGroup; 24b10662684a404989de6058e146565e5f39e7897dDake Guimport android.view.ViewOutlineProvider; 2519312c5f247559a9483d874e62150c49d36aa478Dake Gu 2619312c5f247559a9483d874e62150c49d36aa478Dake Guclass ShadowHelperApi21 { 2719312c5f247559a9483d874e62150c49d36aa478Dake Gu 28f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout static class ShadowImpl { 29254b417129de2a8c5612826a152f8a26c8f1d0e8Dake Gu View mShadowContainer; 30f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout float mNormalZ; 31f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout float mFocusedZ; 32f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout } 33f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout 34b10662684a404989de6058e146565e5f39e7897dDake Gu static final ViewOutlineProvider sOutlineProvider = new ViewOutlineProvider() { 35b10662684a404989de6058e146565e5f39e7897dDake Gu @Override 36b10662684a404989de6058e146565e5f39e7897dDake Gu public void getOutline(View view, Outline outline) { 37b10662684a404989de6058e146565e5f39e7897dDake Gu outline.setRect(0, 0, view.getWidth(), view.getHeight()); 38b10662684a404989de6058e146565e5f39e7897dDake Gu outline.setAlpha(1.0f); 39b10662684a404989de6058e146565e5f39e7897dDake Gu } 40b10662684a404989de6058e146565e5f39e7897dDake Gu }; 4119312c5f247559a9483d874e62150c49d36aa478Dake Gu 4219312c5f247559a9483d874e62150c49d36aa478Dake Gu /* add shadows and return a implementation detail object */ 43f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout public static Object addDynamicShadow( 44254b417129de2a8c5612826a152f8a26c8f1d0e8Dake Gu View shadowContainer, float unfocusedZ, float focusedZ, boolean roundedCorners) { 454f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout if (roundedCorners) { 466b447e693090017258eb48a51ae4119fb0f5119eCraig Stout RoundedRectHelperApi21.setClipToRoundedOutline(shadowContainer, true); 474f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout } else { 484f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout shadowContainer.setOutlineProvider(sOutlineProvider); 494f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout } 50f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout ShadowImpl impl = new ShadowImpl(); 51f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout impl.mShadowContainer = shadowContainer; 52f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout impl.mNormalZ = unfocusedZ; 53f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout impl.mFocusedZ = focusedZ; 54f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout shadowContainer.setZ(impl.mNormalZ); 55254b417129de2a8c5612826a152f8a26c8f1d0e8Dake Gu if (shadowContainer instanceof ViewGroup) { 56254b417129de2a8c5612826a152f8a26c8f1d0e8Dake Gu ((ViewGroup) shadowContainer).setTransitionGroup(true); 57254b417129de2a8c5612826a152f8a26c8f1d0e8Dake Gu } 58f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout return impl; 5919312c5f247559a9483d874e62150c49d36aa478Dake Gu } 6019312c5f247559a9483d874e62150c49d36aa478Dake Gu 6119312c5f247559a9483d874e62150c49d36aa478Dake Gu /* set shadow focus level 0 for unfocused 1 for fully focused */ 62f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout public static void setShadowFocusLevel(Object object, float level) { 63f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout ShadowImpl impl = (ShadowImpl) object; 64f4acd3cf076435ce836a6d4a9027b73ec3050defCraig Stout impl.mShadowContainer.setZ(impl.mNormalZ + level * (impl.mFocusedZ - impl.mNormalZ)); 6519312c5f247559a9483d874e62150c49d36aa478Dake Gu } 6669e74bd8956577d9a3414b81ec661fd5fee42e19Craig Stout 67c62efa44831b1c60dcbdfd968735e27ac8294439Craig Stout public static void setZ(View view, float z) { 68c62efa44831b1c60dcbdfd968735e27ac8294439Craig Stout view.setZ(z); 69adf55abedd17eb9484d03da4b521209f15724f1fCraig Stout } 7019312c5f247559a9483d874e62150c49d36aa478Dake Gu} 71