1 /*
2  * Copyright 2021 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 androidx.camera.integration.view;
18 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.graphics.Color;
22 import android.graphics.Paint;
23 import android.graphics.RectF;
24 import android.util.AttributeSet;
25 import android.widget.FrameLayout;
26 
27 import org.jspecify.annotations.NonNull;
28 import org.jspecify.annotations.Nullable;
29 
30 /**
31  * A overlay view for drawing a tile with {@link Canvas}.
32  */
33 public final class OverlayView extends FrameLayout {
34 
35     private RectF mTile;
36     private final Paint mPaint = new Paint();
37 
OverlayView(@onNull Context context)38     public OverlayView(@NonNull Context context) {
39         super(context);
40     }
41 
42 
OverlayView(@onNull Context context, @Nullable AttributeSet attrs)43     public OverlayView(@NonNull Context context, @Nullable AttributeSet attrs) {
44         super(context, attrs);
45     }
46 
setTileRect(RectF tile)47     void setTileRect(RectF tile) {
48         mTile = tile;
49     }
50 
51     @Override
onDraw(@onNull Canvas canvas)52     public void onDraw(@NonNull Canvas canvas) {
53         super.onDraw(canvas);
54         if (mTile == null) {
55             return;
56         }
57 
58         // The tile paint is black stroke with a white glow so it's always visible regardless of
59         // the background.
60         mPaint.setStyle(Paint.Style.STROKE);
61         mPaint.setColor(Color.WHITE);
62         mPaint.setStrokeWidth(10);
63         canvas.drawRect(mTile, mPaint);
64 
65         mPaint.setColor(Color.BLACK);
66         mPaint.setStrokeWidth(5);
67         canvas.drawRect(mTile, mPaint);
68     }
69 }
70