summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/graphics/RadialGradientView.java
blob: cf6851c000090b3edd7297917e12f35640b4464c (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * 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.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.android.launcher3.R;
import com.android.launcher3.Utilities;

/**
 * Draws a translucent radial gradient background from an initial state with progress 0.0 to a
 * final state with progress 1.0;
 */
public class RadialGradientView extends View {

    public static final int DEFAULT_COLOR_1 = Color.WHITE;
    public static final int DEFAULT_COLOR_2 = Color.BLACK;

    private static Bitmap sFinalGradientMask;
    private static Bitmap sAlphaGradientMask;

    // TODO needs to be cleaned up once design finalizes
    static class Config {
        // dimens
        final float gradientCenterX = 0.5f;
        final float gradientCenterY = 1.05f;
        final float gradientHeadStartFactor = 0.35f;
        final float gradientAlphaMaskLengthDp = 700;
        // interpolation
        final boolean useGradientAlphaDecel = false;
        final float decelFactorForGradientAlpha = 2f;
        // colors
        final float finalGradientAlpha = 0.75f;
        int color1 = DEFAULT_COLOR_1;
        int color2 = DEFAULT_COLOR_2;
    }

    private final RectF mAlphaMaskRect = new RectF();
    private final RectF mFinalMaskRect = new RectF();
    private final Paint mPaint = new Paint();
    private final Config mConfig = new Config();
    private final DecelerateInterpolator mDecelInterpolator;
    private float mProgress;
    private int mWidth;
    private int mHeight;
    private final int mMaskHeight;
    private final Context mAppContext;

    public RadialGradientView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mAppContext = context.getApplicationContext();
        this.mDecelInterpolator = new DecelerateInterpolator(mConfig.decelFactorForGradientAlpha);
        this.mMaskHeight = Utilities.pxFromDp(mConfig.gradientAlphaMaskLengthDp,
                mAppContext.getResources().getDisplayMetrics());

        if (sFinalGradientMask == null) {
            sFinalGradientMask = Utilities.convertToAlphaMask(
                    Utilities.createOnePixBitmap(), mConfig.finalGradientAlpha);
        }
        if (sAlphaGradientMask == null) {
            Bitmap alphaMaskFromResource = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.all_apps_alpha_mask);
            sAlphaGradientMask = Utilities.convertToAlphaMask(
                    alphaMaskFromResource, mConfig.finalGradientAlpha);
        }
    }

    public void onExtractedColorsChanged(int color1, int color2) {
        mConfig.color1 = color1;
        mConfig.color2 = color2;
        if (mWidth + mHeight > 0) {
            createRadialShader();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        this.mWidth = getMeasuredWidth();
        this.mHeight = getMeasuredHeight();
        if (mWidth + mHeight > 0) {
            createRadialShader();
        }
    }

    // only being called when colors change
    private void createRadialShader() {
        float radius = Math.max(mHeight, mWidth) * mConfig.gradientCenterY;

        float posScreenBottom = (radius - mHeight) / radius; // center lives below screen
        RadialGradient shader = new RadialGradient(
                mWidth * mConfig.gradientCenterX,
                mHeight * mConfig.gradientCenterY,
                radius,
                new int[] {mConfig.color1, mConfig.color1, mConfig.color2},
                new float[] {0f, posScreenBottom, 1f},
                Shader.TileMode.CLAMP);
        mPaint.setShader(shader);
    }

    public void setProgress(float progress) {
        this.mProgress = progress;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float head = mConfig.gradientHeadStartFactor;
        float linearProgress = head + (mProgress * (1f - head));
        float adjustProgress = mConfig.useGradientAlphaDecel
                ? mDecelInterpolator.getInterpolation(linearProgress)
                : linearProgress;
        float startMaskY = (1f - adjustProgress) * mHeight - mMaskHeight * adjustProgress;

        mAlphaMaskRect.set(0, startMaskY, mWidth, startMaskY + mMaskHeight);
        mFinalMaskRect.set(0, startMaskY + mMaskHeight, mWidth, mHeight);
        canvas.drawBitmap(sAlphaGradientMask, null, mAlphaMaskRect, mPaint);
        canvas.drawBitmap(sFinalGradientMask, null, mFinalMaskRect, mPaint);
    }

}