imageview 四角触摸放大旋转可拖拽 不能超出屏幕
时间: 2023-10-01 07:07:21 浏览: 184
你可以使用Android提供的`Matrix`类实现对`ImageView`的缩放、旋转和拖拽操作。以下是实现的大致步骤:
1. 继承`ImageView`,重写`onDraw()`方法,在此方法中使用`Matrix`类来实现对图片的变换。
2. 在`onTouchEvent()`方法中处理触摸事件,分别对应不同的操作(缩放、旋转、拖拽)。
3. 实现边界检测,确保图片不会超出屏幕。
下面是一个简单的示例代码,可以实现对`ImageView`的缩放、旋转和拖拽操作,并且确保图片不会超出屏幕。
```java
public class TouchImageView extends ImageView {
private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;
private static final int ROTATE = 3;
private int mode = NONE;
private PointF last = new PointF();
private PointF mid = new PointF();
private float oldDist = 1f;
private float oldRotation = 0;
private boolean canDrag = false;
private RectF bounds = new RectF();
public TouchImageView(Context context) {
super(context);
init();
}
public TouchImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TouchImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setScaleType(ScaleType.MATRIX);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
last.set(event.getX(), event.getY());
mode = DRAG;
canDrag = true;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
oldRotation = rotation(event);
midPoint(mid, event);
mode = ZOOM;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
float dx = event.getX() - last.x;
float dy = event.getY() - last.y;
if (canDrag) {
Matrix matrix = getImageMatrix();
matrix.postTranslate(dx, dy);
setImageMatrix(matrix);
}
last.set(event.getX(), event.getY());
} else if (mode == ZOOM && event.getPointerCount() == 2) {
float newDist = spacing(event);
float newRotation = rotation(event);
float scale = newDist / oldDist;
Matrix matrix = getImageMatrix();
matrix.postScale(scale, scale, mid.x, mid.y);
matrix.postRotate(newRotation - oldRotation, mid.x, mid.y);
setImageMatrix(matrix);
oldDist = newDist;
oldRotation = newRotation;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
canDrag = false;
break;
}
// 边界检测
Matrix matrix = getImageMatrix();
matrix.mapRect(bounds, new RectF(0, 0, getWidth(), getHeight()));
float dx = 0, dy = 0;
if (bounds.left > 0) {
dx = -bounds.left;
} else if (bounds.right < getWidth()) {
dx = getWidth() - bounds.right;
}
if (bounds.top > 0) {
dy = -bounds.top;
} else if (bounds.bottom < getHeight()) {
dy = getHeight() - bounds.bottom;
}
if (dx != 0 || dy != 0) {
matrix.postTranslate(dx, dy);
setImageMatrix(matrix);
}
return true;
}
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
private float rotation(MotionEvent event) {
double delta_x = (event.getX(0) - event.getX(1));
double delta_y = (event.getY(0) - event.getY(1));
double radians = Math.atan2(delta_y, delta_x);
return (float) Math.toDegrees(radians);
}
}
```
在使用时,只需要将`ImageView`替换为`TouchImageView`,即可实现对图片的缩放、旋转和拖拽操作,并且确保图片不会超出屏幕。
阅读全文