1 package com.android.launcher3.allapps; 2 3 import android.annotation.TargetApi; 4 import android.content.res.Resources; 5 import android.graphics.Rect; 6 import android.graphics.drawable.GradientDrawable; 7 import android.os.Build; 8 import android.support.v7.widget.RecyclerView; 9 import android.view.View; 10 import android.view.ViewGroup; 11 import android.view.ViewOutlineProvider; 12 import android.widget.FrameLayout; 13 14 import com.android.launcher3.R; 15 16 /** 17 * Helper class for controlling the header elevation in response to RecyclerView scroll. 18 */ 19 public abstract class HeaderElevationController extends RecyclerView.OnScrollListener { 20 21 private int mCurrentY = 0; 22 23 public void reset() { 24 mCurrentY = 0; 25 onScroll(mCurrentY); 26 } 27 28 @Override 29 public final void onScrolled(RecyclerView recyclerView, int dx, int dy) { 30 mCurrentY += dy; 31 onScroll(mCurrentY); 32 } 33 34 public void updateBackgroundPadding(Rect bgPadding) { } 35 36 abstract void onScroll(int scrollY); 37 38 public static class ControllerV16 extends HeaderElevationController { 39 40 private final View mShadow; 41 private final float mScrollToElevation; 42 43 public ControllerV16(View header) { 44 Resources res = header.getContext().getResources(); 45 mScrollToElevation = res.getDimension(R.dimen.all_apps_header_scroll_to_elevation); 46 47 mShadow = new View(header.getContext()); 48 mShadow.setBackground(new GradientDrawable( 49 GradientDrawable.Orientation.TOP_BOTTOM, new int[] {0x1E000000, 0x00000000})); 50 mShadow.setAlpha(0); 51 52 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams( 53 FrameLayout.LayoutParams.MATCH_PARENT, 54 res.getDimensionPixelSize(R.dimen.all_apps_header_shadow_height)); 55 lp.topMargin = ((FrameLayout.LayoutParams) header.getLayoutParams()).height; 56 57 ((ViewGroup) header.getParent()).addView(mShadow, lp); 58 } 59 60 @Override 61 public void onScroll(int scrollY) { 62 float elevationPct = (float) Math.min(scrollY, mScrollToElevation) / 63 mScrollToElevation; 64 mShadow.setAlpha(elevationPct); 65 } 66 67 @Override 68 public void updateBackgroundPadding(Rect bgPadding) { 69 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mShadow.getLayoutParams(); 70 lp.leftMargin = bgPadding.left; 71 lp.rightMargin = bgPadding.right; 72 mShadow.requestLayout(); 73 } 74 } 75 76 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 77 public static class ControllerVL extends HeaderElevationController { 78 79 private final View mHeader; 80 private final float mMaxElevation; 81 private final float mScrollToElevation; 82 83 public ControllerVL(View header) { 84 mHeader = header; 85 mHeader.setOutlineProvider(ViewOutlineProvider.BOUNDS); 86 87 Resources res = header.getContext().getResources(); 88 mMaxElevation = res.getDimension(R.dimen.all_apps_header_max_elevation); 89 mScrollToElevation = res.getDimension(R.dimen.all_apps_header_scroll_to_elevation); 90 } 91 92 @Override 93 public void onScroll(int scrollY) { 94 float elevationPct = Math.min(scrollY, mScrollToElevation) / mScrollToElevation; 95 float newElevation = mMaxElevation * elevationPct; 96 if (Float.compare(mHeader.getElevation(), newElevation) != 0) { 97 mHeader.setElevation(newElevation); 98 } 99 } 100 } 101 } 102