wxr
2020-09-09 c3e1b733fc45bd9f0b88bfb560cfa87a270b079b
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
using System;
using Android.Content;
using Android.Graphics;
using Android.Util;
using Android.Views;
using Android.Widget;
 
namespace GateWay.Droid
{
    public class ZXingOverlayView: View
    {
        float screenRate;
        float lineWidth;
        float textSize;
        float left;
        float width;
        float height;
        float top;
        Paint paint;
        public ZXingOverlayView(Context context) : base(context)
        {
            InitData(context);
        }
 
        public ZXingOverlayView(Context context, IAttributeSet attrs) : base(context, attrs)
        {
            InitData(context);
        }
 
        public override void Draw(Canvas canvas)
        {
            left = canvas.Width * 0.125f;
            width = canvas.Width * 0.75f;
            height = canvas.Width * 0.75f;
            top = (canvas.Height - height) / 2;
 
            paint = new Paint { AntiAlias = true, };
            paint.SetStyle(Paint.Style.FillAndStroke);
 
            AddScreenFillRect(canvas);
            AddCenterClearRect(canvas);
            AddWhiteCanvas(canvas);
            AddCornerLineWithCanvas(canvas);
            //AddTextWithCanvas(canvas);
        }
 
        private void InitData(Context context)
        {
            screenRate = 15 * context.Resources.DisplayMetrics.Density;
            lineWidth = 2 * context.Resources.DisplayMetrics.Density;
            textSize = 16 * context.Resources.DisplayMetrics.Density;
 
    
        }
 
        //添加屏幕半透明填充色
        private void AddScreenFillRect(Canvas canvas)
        {
            paint.Color = Color.Black;
            paint.Alpha = 128;
 
            canvas.DrawRect(0, 0, canvas.Width, top, paint);
            canvas.DrawRect(0, top, left, top + width, paint);
            canvas.DrawRect(width + left, top, canvas.Width, top + width, paint);
            canvas.DrawRect(0, top + width, canvas.Width, canvas.Height, paint);
        }
        //添加中心全透明填充色
        private void AddCenterClearRect(Canvas canvas)
        {
            paint.Color = Color.Transparent;
 
            canvas.DrawRect(left, top, left + width, top + width, paint);
        }
        //添加白色方框
        private void AddWhiteCanvas(Canvas canvas)
        {
            paint.Color = Color.Rgb(255, 255, 255);
 
            canvas.DrawRect(left, top, left + width, top + 2, paint);
            canvas.DrawRect(left, top, left + 2, top + height, paint);
            canvas.DrawRect(left + width, top, left + width + 2, top + height, paint);
            canvas.DrawRect(left, top + height, left + width, top + height + 2, paint);
        }
        //添加四个角的蓝色
        private void AddCornerLineWithCanvas(Canvas canvas)
        {
            paint.Color = Color.Rgb(22, 118, 188);
 
            canvas.DrawRect(left, top, left + screenRate, top + lineWidth, paint);
            canvas.DrawRect(left, top, left + lineWidth, top + screenRate, paint);
            canvas.DrawRect(left + width - screenRate, top, left + width, top + lineWidth, paint);
            canvas.DrawRect(left + width, top, left + width + lineWidth, top + screenRate, paint);
            canvas.DrawRect(left, top + width, left + screenRate, top + width + lineWidth, paint);
            canvas.DrawRect(left, top + width - screenRate, left + lineWidth, top + width, paint);
            canvas.DrawRect(left + width - screenRate, top + width, left + width + lineWidth, top + width + lineWidth, paint);
            canvas.DrawRect(left + width, top + width - screenRate, left + width + lineWidth, top + width + lineWidth, paint);
        }
        //添加文字提醒
        private void AddTextWithCanvas(Canvas canvas)
        {
            paint.Color = Color.Rgb(255, 255, 255);
            paint.SetTypeface(Typeface.DefaultBold);
            paint.TextSize = textSize;
            string text = "BottomText";
 
            float textWidth = paint.MeasureText(text);
 
            canvas.DrawText(text, (canvas.Width - textWidth) / 2, top + width + textSize, paint);
 
 
            //canvas.DrawText("返回", 50, 100, paint);
 
        }
 
 
 
    }
}