New file |
| | |
| | | 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); |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | | } |