33import android .content .Context ;
44import android .graphics .Canvas ;
55import android .graphics .Color ;
6- import android .graphics .Paint ;
6+ import android .graphics .Path ;
77import android .graphics .Rect ;
8+ import android .graphics .RectF ;
89import android .graphics .Region ;
910import androidx .annotation .NonNull ;
1011import androidx .annotation .Nullable ;
1112import android .util .AttributeSet ;
1213import android .view .View ;
1314
1415public class ScanIndicatorView extends View {
15- private Paint rectPaint = new Paint ();
16+ public enum Style {
17+ RECT ,
18+ QUAD
19+ }
1620
1721 private float scale = 1.0f ;
1822
19- private Rect rect = new Rect ();
20- private Rect borderRect = new Rect ();
23+ private RectF rect = new RectF ();
24+ private Path path = new Path ();
2125
2226 private int offsetX ;
2327 private int offsetY ;
24- private int borderColor ;
28+ private int backgroundColor ;
2529
2630 private int minPaddingLeft ;
2731 private int minPaddingTop ;
2832 private int minPaddingRight ;
2933 private int minPaddingBottom ;
3034
35+ private Style style = Style .RECT ;
36+
37+
3138 public ScanIndicatorView (@ NonNull Context context ) {
3239 super (context );
3340 init ();
@@ -44,14 +51,10 @@ public ScanIndicatorView(@NonNull Context context, @Nullable AttributeSet attrs,
4451 }
4552
4653 private void init () {
47- rectPaint .setColor (Color .WHITE );
48- rectPaint .setStyle (Paint .Style .STROKE );
49- rectPaint .setStrokeWidth (dp2px (1 ));
50- rectPaint .setAlpha (51 );
54+ backgroundColor = Color .parseColor ("#99222222" );
5155
52- borderColor = Color .parseColor ("#99222222" );
53-
54- setMinPadding (dp2px (8 ), dp2px (80 ), dp2px (8 ), dp2px (80 ));
56+ setMinPadding (dp2px (24 ), dp2px (80 ), dp2px (24 ), dp2px (80 ));
57+ setStyle (Style .RECT );
5558 }
5659
5760 @ Override
@@ -67,8 +70,16 @@ private void update() {
6770 int maxWidth = w - minPaddingLeft - minPaddingRight ;
6871 int maxHeight = h - minPaddingTop - minPaddingBottom ;
6972
70- int rectWidth = dp2px (360 ) - minPaddingLeft - minPaddingRight ;
71- int rectHeight = dp2px (170 );
73+ int rectWidth ;
74+ int rectHeight ;
75+
76+ if (style == Style .RECT ) {
77+ rectWidth = getWidth () - minPaddingLeft - minPaddingRight ;
78+ rectHeight = Math .round (rectWidth * 0.55f );
79+ } else {
80+ rectWidth = dp2px (360 ) - minPaddingLeft - minPaddingRight ;
81+ rectHeight = Math .round (rectWidth * 0.85f );
82+ }
7283
7384 if (rectHeight > maxHeight ) {
7485 float scaleFactor = ((float ) maxHeight / (float ) rectHeight );
@@ -84,21 +95,21 @@ private void update() {
8495 rectHeight = Math .round ((float ) rectHeight * scaleFactor );
8596 }
8697
87- rect .left = w / 2 - rectWidth / 2 ;
88- rect .right = w / 2 + rectWidth / 2 ;
89- rect .top = ( h / 2 - rectHeight / 2 ) ;
90- rect .bottom = ( h / 2 + rectHeight / 2 ) ;
98+ rect .left = w / 2.0f - rectWidth / 2.0f ;
99+ rect .right = w / 2.0f + rectWidth / 2.0f ;
100+ rect .top = h / 2.0f - rectHeight / 2.0f ;
101+ rect .bottom = h / 2.0f + rectHeight / 2.0f ;
91102
92- int rw = rect .width ();
93- int rh = rect .height ();
103+ float rw = rect .width ();
104+ float rh = rect .height ();
94105
95- rect .left = offsetX + w / 2 - Math .round (rw / 2 * scale );
96- rect .right = offsetX + w / 2 + Math .round (rw / 2 * scale );
97- rect .top = offsetY + h / 2 - Math .round (rh / 2 * scale );
98- rect .bottom = offsetY + h / 2 + Math .round (rh / 2 * scale );
106+ rect .left = offsetX + w / 2.0f - Math .round (rw / 2.0f * scale );
107+ rect .right = offsetX + w / 2.0f + Math .round (rw / 2.0f * scale );
108+ rect .top = offsetY + h / 2.0f - Math .round (rh / 2.0f * scale );
109+ rect .bottom = offsetY + h / 2.0f + Math .round (rh / 2.0f * scale );
99110
100- borderRect . set ( rect );
101- borderRect . inset ( dp2px (1 ), dp2px (1 ) );
111+ path . reset ( );
112+ path . addRoundRect ( rect , dp2px (10 ), dp2px (10 ), Path . Direction . CCW );
102113 }
103114
104115 public void setMinPadding (int left , int top , int right , int bottom ) {
@@ -111,21 +122,26 @@ public void setMinPadding(int left, int top, int right, int bottom) {
111122 @ Override
112123 protected void onDraw (Canvas canvas ) {
113124 canvas .save ();
114- canvas .clipRect ( rect , Region .Op .DIFFERENCE );
115- canvas .drawColor (borderColor );
125+ canvas .clipPath ( path , Region .Op .DIFFERENCE );
126+ canvas .drawColor (backgroundColor );
116127 canvas .restore ();
117-
118- canvas .drawRect (borderRect , rectPaint );
119128 }
120129
121130 public Rect getIndicatorRect () {
122- return new Rect (rect );
131+ return new Rect (Math .round (rect .left ),
132+ Math .round (rect .top ),
133+ Math .round (rect .right ),
134+ Math .round (rect .bottom ));
135+ }
136+
137+ public void setStyle (Style style ) {
138+ this .style = style ;
139+ update ();
123140 }
124141
125142 public void setOffset (int offsetX , int offsetY ) {
126143 this .offsetX = offsetX ;
127144 this .offsetY = offsetY ;
128-
129145 update ();
130146 }
131147
0 commit comments