写一个UVCCamera的类,继承UVCCameraTextureView类上面扩展出一个类支持多点触碰支持放大缩小和旋转的类
时间: 2023-08-15 18:04:49 浏览: 168
下面是一个简单的示例代码,实现了继承自UVCCameraTextureView类的多点触碰支持放大缩小和旋转的UVCCamera类:
```java
public class UVCCamera extends UVCCameraTextureView {
private float mPrevX, mPrevY;
private float mScaleFactor = 1f;
private float mRotationDegree = 0f;
public UVCCamera(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mPrevX = event.getX(0);
mPrevY = event.getY(0);
break;
case MotionEvent.ACTION_MOVE:
if (event.getPointerCount() == 1) {
float x = event.getX(0);
float y = event.getY(0);
float dx = x - mPrevX;
float dy = y - mPrevY;
mRotationDegree += Math.atan2(dy, dx) * 180 / Math.PI;
setRotation(mRotationDegree);
mPrevX = x;
mPrevY = y;
} else if (event.getPointerCount() == 2) {
float x1 = event.getX(0);
float y1 = event.getY(0);
float x2 = event.getX(1);
float y2 = event.getY(1);
float distance = (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
if (distance > 10f) {
float scaleFactor = distance / mPrevDistance;
mScaleFactor *= scaleFactor;
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));
setScaleX(mScaleFactor);
setScaleY(mScaleFactor);
}
float angle = (float) Math.atan2(y2 - y1, x2 - x1) * 180 / (float) Math.PI;
setRotation(angle);
mPrevDistance = distance;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
}
```
这个类继承自UVCCameraTextureView,重写了onTouchEvent方法,实现了多点触碰支持放大缩小和旋转的功能。当手指只有一个触点时,根据手指移动的方向来旋转图像;当手指有两个触点时,根据两个触点之间的距离来缩放图像,并根据两个触点之间的角度来旋转图像。
阅读全文