1 /*
2  * Copyright (C) 2018 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 package com.android.launcher3.views;
17 
18 import android.content.Context;
19 import android.graphics.Canvas;
20 import android.graphics.Paint;
21 import android.graphics.Path;
22 import android.graphics.RectF;
23 import android.util.AttributeSet;
24 import android.widget.FrameLayout;
25 
26 import com.android.launcher3.R;
27 import com.android.launcher3.util.Themes;
28 
29 /**
30  * View with top rounded corners.
31  */
32 public class TopRoundedCornerView extends SpringRelativeLayout {
33 
34     private final RectF mRect = new RectF();
35     private final Path mClipPath = new Path();
36     private float[] mRadii;
37 
38     private final Paint mNavBarScrimPaint;
39     private int mNavBarScrimHeight = 0;
40 
TopRoundedCornerView(Context context, AttributeSet attrs, int defStyleAttr)41     public TopRoundedCornerView(Context context, AttributeSet attrs, int defStyleAttr) {
42         super(context, attrs, defStyleAttr);
43 
44         int radius = getResources().getDimensionPixelSize(R.dimen.bg_round_rect_radius);
45         mRadii = new float[] {radius, radius, radius, radius, 0, 0, 0, 0};
46 
47         mNavBarScrimPaint = new Paint();
48         mNavBarScrimPaint.setColor(Themes.getAttrColor(context, R.attr.allAppsNavBarScrimColor));
49     }
50 
TopRoundedCornerView(Context context, AttributeSet attrs)51     public TopRoundedCornerView(Context context, AttributeSet attrs) {
52         this(context, attrs, 0);
53     }
54 
setNavBarScrimHeight(int height)55     public void setNavBarScrimHeight(int height) {
56         if (mNavBarScrimHeight != height) {
57             mNavBarScrimHeight = height;
58             invalidate();
59         }
60     }
61 
62     @Override
draw(Canvas canvas)63     public void draw(Canvas canvas) {
64         canvas.save();
65         canvas.clipPath(mClipPath);
66         super.draw(canvas);
67         canvas.restore();
68 
69         if (mNavBarScrimHeight > 0) {
70             canvas.drawRect(0, getHeight() - mNavBarScrimHeight, getWidth(), getHeight(),
71                     mNavBarScrimPaint);
72         }
73     }
74 
75     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)76     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
77         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
78         mRect.set(0, 0, getMeasuredWidth(), getMeasuredHeight());
79         mClipPath.reset();
80         mClipPath.addRoundRect(mRect, mRadii, Path.Direction.CW);
81     }
82 }
83