summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/allapps/HeaderElevationController.java
blob: ce9837c3917e563ece3cc03e6e84e5910dc6d908 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.android.launcher3.allapps;

import android.annotation.TargetApi;
import android.content.res.Resources;
import android.graphics.Outline;
import android.graphics.Rect;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.FrameLayout;

import com.android.launcher3.BaseRecyclerView;
import com.android.launcher3.R;

/**
 * Helper class for controlling the header elevation in response to RecyclerView scroll.
 */
public abstract class HeaderElevationController extends RecyclerView.OnScrollListener {

    private int mCurrentY = 0;

    public void reset() {
        mCurrentY = 0;
        onScroll(mCurrentY);
    }

    @Override
    public final void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        mCurrentY = ((BaseRecyclerView) recyclerView).getCurrentScrollY();
        onScroll(mCurrentY);
    }

    public void updateBackgroundPadding(Rect bgPadding) { }

    abstract void onScroll(int scrollY);

    public static class ControllerV16 extends HeaderElevationController {

        private final View mShadow;
        private final float mScrollToElevation;

        public ControllerV16(View header) {
            Resources res = header.getContext().getResources();
            mScrollToElevation = res.getDimension(R.dimen.all_apps_header_scroll_to_elevation);

            mShadow = new View(header.getContext());
            mShadow.setBackground(new GradientDrawable(
                    GradientDrawable.Orientation.TOP_BOTTOM, new int[] {0x1E000000, 0x00000000}));
            mShadow.setAlpha(0);

            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,
                    res.getDimensionPixelSize(R.dimen.all_apps_header_shadow_height));
            lp.topMargin = ((FrameLayout.LayoutParams) header.getLayoutParams()).height;

            ((ViewGroup) header.getParent()).addView(mShadow, lp);
        }

        @Override
        public void onScroll(int scrollY) {
            float elevationPct = (float) Math.min(scrollY, mScrollToElevation) /
                    mScrollToElevation;
            mShadow.setAlpha(elevationPct);
        }

        @Override
        public void updateBackgroundPadding(Rect bgPadding) {
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mShadow.getLayoutParams();
            lp.leftMargin = bgPadding.left;
            lp.rightMargin = bgPadding.right;
            mShadow.requestLayout();
        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static class ControllerVL extends HeaderElevationController {

        private final View mHeader;
        private final float mMaxElevation;
        private final float mScrollToElevation;

        public ControllerVL(View header) {
            mHeader = header;
            Resources res = mHeader.getContext().getResources();
            mMaxElevation = res.getDimension(R.dimen.all_apps_header_max_elevation);
            mScrollToElevation = res.getDimension(R.dimen.all_apps_header_scroll_to_elevation);

            // We need to provide a custom outline so the shadow only appears on the bottom edge.
            // The top, left and right edges are all extended out, and the shadow is clipped
            // by the parent.
            final ViewOutlineProvider vop = new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    final View parent = (View) mHeader.getParent();

                    final int left = parent.getLeft(); // Use the parent to account for offsets
                    final int top = view.getTop();
                    final int right = left + view.getWidth();
                    final int bottom = view.getBottom();

                    outline.setRect(
                            left - (int) mMaxElevation,
                            top - (int) mMaxElevation,
                            right + (int) mMaxElevation,
                            bottom);
                }
            };
            mHeader.setOutlineProvider(vop);
        }

        @Override
        public void onScroll(int scrollY) {
            float elevationPct = Math.min(scrollY, mScrollToElevation) / mScrollToElevation;
            float newElevation = mMaxElevation * elevationPct;
            if (Float.compare(mHeader.getElevation(), newElevation) != 0) {
                mHeader.setElevation(newElevation);
            }
        }
    }
}