请为我再加上旋转和缩放功能的代码
时间: 2024-03-03 17:53:17 浏览: 68
matlab gui实现三维图形的旋转平移及缩放
5星 · 资源好评率100%
好的,以下是添加了旋转和缩放功能的代码:
```
package com.blog.demo11;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.blog.BaseActivity;
import com.blog.R;
import static android.view.MotionEvent.INVALID_POINTER_ID;
/**
* 可拖拽、旋转、缩放效果类。
*/
public class ViewDragActivity extends BaseActivity {
private static final String TAG = "ViewDragActivity";
private ImageView backgroundImage;
private ImageView image1;
private ImageView image2;
private int activePointerId = INVALID_POINTER_ID;
private float lastTouchX, lastTouchY;
private float lastRotation = 0;
private float lastScale = 1;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drag_layout);
backgroundImage = findViewById(R.id.background_image);
image1 = findViewById(R.id.image1);
image2 = findViewById(R.id.image2);
// Set touch listeners for each image
image1.setOnTouchListener(new MultiTouchListener());
image2.setOnTouchListener(new MultiTouchListener());
}
private class MultiTouchListener implements View.OnTouchListener {
private float initialTouchX, initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN: {
final int pointerIndex = event.getActionIndex();
final float x = event.getX(pointerIndex);
final float y = event.getY(pointerIndex);
initialTouchX = x;
initialTouchY = y;
lastTouchX = x;
lastTouchY = y;
activePointerId = event.getPointerId(pointerIndex);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = event.findPointerIndex(activePointerId);
final float x = event.getX(pointerIndex);
final float y = event.getY(pointerIndex);
final float dx = x - lastTouchX;
final float dy = y - lastTouchY;
if (event.getPointerCount() == 1) {
// Single finger drag
v.setX(v.getX() + dx);
v.setY(v.getY() + dy);
} else if (event.getPointerCount() == 2) {
// Two finger gesture (rotation and scaling)
final float rotation = rotation(event);
final float scale = scale(event);
v.setRotation(v.getRotation() + rotation - lastRotation);
v.setScaleX(v.getScaleX() * scale / lastScale);
v.setScaleY(v.getScaleY() * scale / lastScale);
v.setX(v.getX() + (scale - 1) * v.getWidth() / 2);
v.setY(v.getY() + (scale - 1) * v.getHeight() / 2);
lastRotation = rotation;
lastScale = scale;
}
lastTouchX = x;
lastTouchY = y;
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
activePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = event.getActionIndex();
final int pointerId = event.getPointerId(pointerIndex);
if (pointerId == activePointerId) {
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
lastTouchX = event.getX(newPointerIndex);
lastTouchY = event.getY(newPointerIndex);
activePointerId = event.getPointerId(newPointerIndex);
}
lastRotation = rotation(event);
lastScale = scale(event);
break;
}
}
return true;
}
private float rotation(MotionEvent event) {
double deltaX = (event.getX(0) - event.getX(1));
double deltaY = (event.getY(0) - event.getY(1));
double radians = Math.atan2(deltaY, deltaX);
return (float) Math.toDegrees(radians);
}
private float scale(MotionEvent event) {
float distance = distance(event);
float initialDistance = distance(event, 0, 1);
return distance / initialDistance;
}
private float distance(MotionEvent event) {
return distance(event, 0, 1);
}
private float distance(MotionEvent event, int index1, int index2) {
float dx = event.getX(index1) - event.getX(index2);
float dy = event.getY(index1) - event.getY(index2);
return (float) Math.sqrt(dx * dx + dy * dy);
}
}
}
```
在 MultiTouchListener 中添加了计算旋转角度和缩放比例的方法,分别为 rotation() 和 scale()。在 ACTION_MOVE 事件中,判断触摸点数量,如果是一个触摸点,则进行拖拽操作;如果是两个触摸点,则进行旋转和缩放操作。其中旋转角度和缩放比例的计算方法可以参考代码中的实现。
另外需要注意的是,在 ACTION_POINTER_UP 事件中需要重新计算 lastRotation 和 lastScale 的值,以便于下一次的缩放和旋转操作。
阅读全文