panlili2024
2025-03-05 134209ad70f82051da3ce63471df0cc8f778e57d
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.hdl.sdk.sourceos.qrcode;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.View;
 
import androidx.annotation.Nullable;
import androidx.annotation.Px;
 
import java.util.concurrent.atomic.AtomicBoolean;
 
/**
 * Created by Tong on 2021/12/2.
 * 二维码View
 */
public class QrCodeView extends View {
 
    private static final int DEF_MARGIN = 0;
 
    private int mWidth;
    private int mHeight;
    private String mContent;
    private int mMargin = DEF_MARGIN;
 
    private Bitmap drawBitmap;
 
    private Canvas drawCanvas;
 
 
    private Paint paint;
 
    private final AtomicBoolean isDraw = new AtomicBoolean(false);
    private DrawThread mDrawThread;
 
    public QrCodeView(Context context) {
        this(context, null);
    }
 
    public QrCodeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public QrCodeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
 
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (drawBitmap != null) {
            canvas.drawBitmap(drawBitmap, getPaddingLeft(), getPaddingTop(), paint);
        }
    }
 
 
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHeight = h - getPaddingTop() - getPaddingBottom();
        mWidth = w - getPaddingStart() - getPaddingEnd();
 
        if (drawBitmap == null || (drawBitmap.isRecycled() || drawBitmap.getWidth() != mWidth || drawBitmap.getHeight() != mHeight)) {
            drawBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
            drawCanvas = new Canvas(drawBitmap);
            startDraw();
        }
    }
 
 
    public void setContent(String content) {
        setContent(content, DEF_MARGIN);
    }
 
    public void setContent(String content, @Px int margin) {
        this.mContent = content;
        startDraw();
        this.mMargin = margin;
    }
 
    public String getContent() {
        return mContent;
    }
 
    public void startDraw() {
        if (mContent == null || isDraw.get()) return;
        isDraw.set(true);
        mDrawThread = new DrawThread();
        mDrawThread.start();
    }
 
    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }
 
 
    private class DrawThread extends Thread {
        @Override
        public void run() {
            while (isDraw.get())
                try {
                    if (drawCanvas != null) {
 
                        drawCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                        int size = Math.min(mHeight, mWidth);
                        QRCodeUtils.createQRCode(drawBitmap, mContent, size, size, mMargin);
                        postInvalidate();
                        isDraw.set(false);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (isDraw.get()) {
                        try {
                            Thread.sleep(200L);
                        } catch (Exception ignore) {
 
                        }
                    }
                }
        }
    }
 
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (drawBitmap != null) {
            drawBitmap.recycle();
        }
        isDraw.set(false);
        if (mDrawThread != null) {
            mDrawThread.interrupt();
        }
    }
}