写一个UVCCamera的类,继续承接UVCCameraTextureView类上面扩展出一个类支持多点触摸支持放大缩小和旋转的类,并解决放大旋转中闪屏问题
时间: 2023-08-16 07:06:57 浏览: 70
以下是一个扩展了UVCCameraTextureView的类,支持多点触摸、放大缩小和旋转,并解决了闪屏问题:
```
public class MultiTouchUVCCameraTextureView extends UVCCameraTextureView implements ScaleGestureDetector.OnScaleGestureListener, RotationGestureDetector.OnRotationGestureListener {
private ScaleGestureDetector mScaleDetector;
private RotationGestureDetector mRotationDetector;
private PointF mLastTouchPoint1;
private PointF mLastTouchPoint2;
private float mLastDistance;
private float mLastRotation;
private float mScaleFactor = 1.0f;
private float mRotationDegree = 0.0f;
private boolean mIsScaling = false;
private boolean mIsRotating = false;
public MultiTouchUVCCameraTextureView(Context context, AttributeSet attrs) {
super(context, attrs);
mScaleDetector = new ScaleGestureDetector(context, this);
mRotationDetector = new RotationGestureDetector(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount() == 2) {
if (mIsRotating) {
mRotationDetector.onTouchEvent(event);
} else if (mIsScaling) {
mScaleDetector.onTouchEvent(event);
} else {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_POINTER_DOWN:
mLastTouchPoint1 = new PointF(event.getX(0), event.getY(0));
mLastTouchPoint2 = new PointF(event.getX(1), event.getY(1));
mLastDistance = distance(mLastTouchPoint1, mLastTouchPoint2);
mLastRotation = rotation(mLastTouchPoint1, mLastTouchPoint2);
break;
case MotionEvent.ACTION_MOVE:
PointF curTouchPoint1 = new PointF(event.getX(0), event.getY(0));
PointF curTouchPoint2 = new PointF(event.getX(1), event.getY(1));
float curDistance = distance(curTouchPoint1, curTouchPoint2);
float curRotation = rotation(curTouchPoint1, curTouchPoint2);
float deltaDistance = curDistance - mLastDistance;
float deltaRotation = curRotation - mLastRotation;
if (Math.abs(deltaRotation) > Math.abs(deltaDistance)) {
mIsRotating = true;
mRotationDetector.onTouchEvent(event);
} else {
mIsScaling = true;
mScaleDetector.onTouchEvent(event);
}
break;
case MotionEvent.ACTION_POINTER_UP:
mIsScaling = false;
mIsRotating = false;
break;
}
}
} else {
mIsScaling = false;
mIsRotating = false;
}
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 3.0f));
setScaleX(mScaleFactor);
setScaleY(mScaleFactor);
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
mLastScaleFactor = detector.getScaleFactor();
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {}
@Override
public boolean onRotation(RotationGestureDetector detector) {
mRotationDegree += detector.getAngleDelta();
setRotation(mRotationDegree);
return true;
}
private float distance(PointF p1, PointF p2) {
float dx = p1.x - p2.x;
float dy = p1.y - p2.y;
return (float) Math.sqrt(dx * dx + dy * dy);
}
private float rotation(PointF p1, PointF p2) {
float dx = p1.x - p2.x;
float dy = p1.y - p2.y;
return (float) Math.toDegrees(Math.atan2(dy, dx));
}
}
```
在这个类中,我们通过重写`onTouchEvent`方法来处理多点触摸事件,并根据手指的移动来判断是放大缩小还是旋转。在放大缩小和旋转的回调方法中,我们分别更新了`mScaleFactor`和`mRotationDegree`变量,并通过`setScaleX`、`setScaleY`和`setRotation`方法来更新视图的缩放和旋转状态。
此外,在这个类中我们还使用了`RotationGestureDetector`类来处理旋转手势,并解决了闪屏问题。
阅读全文