doum
6 天以前 2b287056e2f59518888d05a1bbc7e5a55fbd84d5
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
 */
package com.example.datalibrary.gatecamera;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Region;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.TextureView;
import android.widget.FrameLayout;
 
/**
 * 基于 系统TextureView实现的预览View。
 *
 * @Time: 2019/1/28
 * @Author: v_chaixiaogang
 */
public class AutoTexturePreviewView extends FrameLayout {
 
    public TextureView textureView;
 
    private int videoWidth = 0;
    private int videoHeight = 0;
 
 
    public static int previewWidth = 0;
    private int previewHeight = 0;
    private static int scale = 2;
 
    public static float circleRadius;
    public static float circleX;
    public static float circleY;
    public boolean isDraw = false;
    private boolean mIsRegister;   // 注册
 
    private float[] pointXY = new float[3];
 
    public AutoTexturePreviewView(Context context) {
        super(context);
        init();
    }
 
    public AutoTexturePreviewView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
 
    public AutoTexturePreviewView(Context context, AttributeSet attrs,
                                  int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
 
 
    private Handler handler = new Handler(Looper.getMainLooper());
 
    private void init() {
        setWillNotDraw(false);
        textureView = new TextureView(getContext());
        addView(textureView);
    }
 
    public void setIsRegister(boolean isRegister) {
        mIsRegister = isRegister;
        invalidate();
    }
 
    public boolean getIsRegister() {
        return mIsRegister;
    }
 
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
 
        previewWidth = getWidth();
        previewHeight = getHeight();
 
        if (videoWidth == 0 || videoHeight == 0 || previewWidth == 0 || previewHeight == 0) {
            return;
        }
 
        if (previewWidth * videoHeight > previewHeight * videoWidth) {
            int scaledChildHeight = videoHeight * previewWidth / videoWidth;
            textureView.layout(0, (previewHeight - scaledChildHeight) / scale,
                    previewWidth, (previewHeight + scaledChildHeight) / scale);
        } else {
            int scaledChildWidth = videoWidth * previewHeight / videoHeight;
            textureView.layout((previewWidth - scaledChildWidth) / scale, 0,
                    (previewWidth + scaledChildWidth) / scale, previewHeight);
 
        }
 
 
    }
 
    public TextureView getTextureView() {
        return textureView;
    }
 
    public int getPreviewWidth() {
        return previewWidth;
    }
 
    public int getPreviewHeight() {
        return previewHeight;
    }
 
    public void setPreviewSize(int width, int height) {
        if (this.videoWidth == width && this.videoHeight == height) {
            return;
        }
        this.videoWidth = width;
        this.videoHeight = height;
        handler.post(new Runnable() {
            @Override
            public void run() {
                requestLayout();
            }
        });
 
    }
 
 
    @Override
    protected void onDraw(Canvas canvas) {
        if (isDraw) {
            Path path = new Path();
            // 设置裁剪的圆心坐标,半径
            path.addCircle(getWidth() / 2, getHeight() / 2, getWidth() / 3, Path.Direction.CCW);
            // 裁剪画布,并设置其填充方式
            canvas.clipPath(path, Region.Op.REPLACE);
 
            // 圆的半径
            circleRadius = getWidth() / 3;
            // 圆心的X坐标
            circleX = (getRight() - getLeft()) / 2;
            // 圆心的Y坐标
            circleY = (getBottom() - getTop()) / 2;
        }
 
        if (mIsRegister) {
            Path path = new Path();
            // 设置裁剪的圆心坐标,半径
            path.addCircle(getWidth() / 2, getHeight() / 2, getWidth() / 3, Path.Direction.CCW);
            // 裁剪画布,并设置其填充方式
            canvas.clipPath(path, Region.Op.REPLACE);
            // 圆的半径
            circleRadius = getWidth() / 3;
            // 圆心的X坐标
            circleX = (getRight() - getLeft()) / 2;
            // 圆心的Y坐标
            circleY = (getBottom() - getTop()) / 2;
        }
        super.onDraw(canvas);
    }
}