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
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3.graphics;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.graphics.ColorUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Interpolator;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
public class ScrimView extends View {
private static final boolean DEBUG = false;
private static final int MASK_HEIGHT_DP = 300;
private static final float MASK_START_LENGTH_FACTOR = 1f;
private static final float FINAL_ALPHA = 0.87f;
private static final int SCRIM_COLOR = ColorUtils.setAlphaComponent(
Color.WHITE, (int) (FINAL_ALPHA * 255));
private static final boolean APPLY_ALPHA = true;
private static Bitmap sFinalScrimMask;
private static Bitmap sAlphaScrimMask;
private final int mMaskHeight;
private int mVisibleHeight;
private final int mHeadStart;
private final RectF mAlphaMaskRect = new RectF();
private final RectF mFinalMaskRect = new RectF();
private final Paint mPaint = new Paint();
private float mProgress;
private final Interpolator mAccelerator = new AccelerateInterpolator();
private final Paint mDebugPaint = DEBUG ? new Paint() : null;
public ScrimView(Context context, AttributeSet attrs) {
super(context, attrs);
mMaskHeight = Utilities.pxFromDp(MASK_HEIGHT_DP, getResources().getDisplayMetrics());
mHeadStart = (int) (mMaskHeight * MASK_START_LENGTH_FACTOR);
mPaint.setColor(SCRIM_COLOR);
if (sFinalScrimMask == null) {
sFinalScrimMask = Utilities.convertToAlphaMask(
Utilities.createOnePixBitmap(), FINAL_ALPHA);
}
if (sAlphaScrimMask == null) {
Bitmap alphaMaskFromResource = BitmapFactory.decodeResource(getResources(),
R.drawable.all_apps_alpha_mask);
sAlphaScrimMask = Utilities.convertToAlphaMask(alphaMaskFromResource, FINAL_ALPHA);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
mVisibleHeight = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, mVisibleHeight * 2);
setProgress(mProgress);
}
public void setProgress(float progress) {
mProgress = progress;
float initialY = mVisibleHeight - mHeadStart;
float fullTranslationY = mVisibleHeight;
float linTranslationY = initialY - progress * fullTranslationY;
setTranslationY(linTranslationY);
if (APPLY_ALPHA) {
int alpha = 55 + (int) (200f * mAccelerator.getInterpolation(progress));
mPaint.setAlpha(alpha);
invalidate();
}
}
@Override
protected void onDraw(Canvas canvas) {
mAlphaMaskRect.set(0, 0, getWidth(), mMaskHeight);
mFinalMaskRect.set(0, mMaskHeight, getWidth(), getHeight());
canvas.drawBitmap(sAlphaScrimMask, null, mAlphaMaskRect, mPaint);
canvas.drawBitmap(sFinalScrimMask, null, mFinalMaskRect, mPaint);
if (DEBUG) {
mDebugPaint.setColor(0xFF0000FF);
canvas.drawLine(0, mAlphaMaskRect.top, getWidth(), mAlphaMaskRect.top, mDebugPaint);
canvas.drawLine(0, mAlphaMaskRect.bottom, getWidth(), mAlphaMaskRect.bottom, mDebugPaint);
}
}
}
|