1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.launcher3.graphics;
18 
19 import static com.android.launcher3.icons.GraphicsUtils.setColorAlphaBound;
20 
21 import android.graphics.Canvas;
22 import android.util.FloatProperty;
23 import android.view.View;
24 
25 import com.android.launcher3.R;
26 
27 /**
28  * Contains general scrim properties such as wallpaper-extracted color that subclasses can use.
29  */
30 public class Scrim {
31 
32     public static final FloatProperty<Scrim> SCRIM_PROGRESS =
33             new FloatProperty<Scrim>("scrimProgress") {
34                 @Override
35                 public Float get(Scrim scrim) {
36                     return scrim.mScrimProgress;
37                 }
38 
39                 @Override
40                 public void setValue(Scrim scrim, float v) {
41                     scrim.setScrimProgress(v);
42                 }
43             };
44 
45     protected final View mRoot;
46 
47     protected float mScrimProgress;
48     protected int mScrimColor;
49     protected int mScrimAlpha = 0;
50 
Scrim(View view)51     public Scrim(View view) {
52         mRoot = view;
53         mScrimColor = mRoot.getContext().getColor(R.color.wallpaper_popup_scrim);
54     }
55 
draw(Canvas canvas)56     public void draw(Canvas canvas) {
57         canvas.drawColor(setColorAlphaBound(mScrimColor, mScrimAlpha));
58     }
59 
setScrimProgress(float progress)60     private void setScrimProgress(float progress) {
61         if (mScrimProgress != progress) {
62             mScrimProgress = progress;
63             mScrimAlpha = Math.round(255 * mScrimProgress);
64             mRoot.invalidate();
65         }
66     }
67 }
68