1 package com.android.launcher3.allapps; 2 3 import android.content.res.Resources; 4 import android.graphics.Outline; 5 import android.support.v7.widget.RecyclerView; 6 import android.view.View; 7 import android.view.ViewOutlineProvider; 8 9 import com.android.launcher3.BaseRecyclerView; 10 import com.android.launcher3.R; 11 import com.android.launcher3.Utilities; 12 13 /** 14 * Helper class for controlling the header elevation in response to RecyclerView scroll. 15 */ 16 public class HeaderElevationController extends RecyclerView.OnScrollListener { 17 18 private final View mHeader; 19 private final float mMaxElevation; 20 private final float mScrollToElevation; 21 22 private int mCurrentY = 0; 23 24 public HeaderElevationController(View header) { 25 mHeader = header; 26 final Resources res = mHeader.getContext().getResources(); 27 mMaxElevation = res.getDimension(R.dimen.all_apps_header_max_elevation); 28 mScrollToElevation = res.getDimension(R.dimen.all_apps_header_scroll_to_elevation); 29 30 // We need to provide a custom outline so the shadow only appears on the bottom edge. 31 // The top, left and right edges are all extended out, and the shadow is clipped 32 // by the parent. 33 final ViewOutlineProvider vop = new ViewOutlineProvider() { 34 @Override 35 public void getOutline(View view, Outline outline) { 36 final View parent = (View) mHeader.getParent(); 37 38 final int left = parent.getLeft(); // Use the parent to account for offsets 39 final int top = view.getTop(); 40 final int right = left + view.getWidth(); 41 final int bottom = view.getBottom(); 42 43 final int offset = Utilities.pxFromDp(mMaxElevation, res.getDisplayMetrics()); 44 outline.setRect(left - offset, top - offset, right + offset, bottom); 45 } 46 }; 47 mHeader.setOutlineProvider(vop); 48 } 49 50 public void reset() { 51 mCurrentY = 0; 52 onScroll(mCurrentY); 53 } 54 55 @Override 56 public final void onScrolled(RecyclerView recyclerView, int dx, int dy) { 57 mCurrentY = ((BaseRecyclerView) recyclerView).getCurrentScrollY(); 58 onScroll(mCurrentY); 59 } 60 61 private void onScroll(int scrollY) { 62 float elevationPct = Math.min(scrollY, mScrollToElevation) / mScrollToElevation; 63 float newElevation = mMaxElevation * elevationPct; 64 if (Float.compare(mHeader.getElevation(), newElevation) != 0) { 65 mHeader.setElevation(newElevation); 66 } 67 } 68 69 } 70