写一个ANDROID ACTIVITY 对多张图片进行旋转缩放移动代码 不要APPCOMATACTIVITY
时间: 2024-05-15 20:12:32 浏览: 114
Android多张图片缩放、移动、旋转
5星 · 资源好评率100%
以下是一个简单的Android Activity,用于对多张图片进行旋转、缩放和移动:
```java
public class MyImageActivity extends Activity implements View.OnTouchListener {
private static final int INVALID_POINTER_ID = -1;
private ArrayList<ImageView> mImageViews;
private int mActivePointerId = INVALID_POINTER_ID;
private float mLastTouchX, mLastTouchY;
private float mPosX, mPosY;
private float mScaleFactor = 1.f;
private float mRotationDegrees = 0.f;
private ScaleGestureDetector mScaleDetector;
private RotationGestureDetector mRotationDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 初始化ImageView数组
mImageViews = new ArrayList<>();
mImageViews.add(new ImageView(this));
mImageViews.add(new ImageView(this));
// 添加图片资源
mImageViews.get(0).setImageResource(R.drawable.image1);
mImageViews.get(1).setImageResource(R.drawable.image2);
// 将ImageView数组添加到布局中
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
for (ImageView imageView : mImageViews) {
imageView.setOnTouchListener(this);
layout.addView(imageView);
}
setContentView(layout);
// 初始化手势检测器
mScaleDetector = new ScaleGestureDetector(this, new ScaleListener());
mRotationDetector = new RotationGestureDetector(new RotationListener());
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// 获取触摸事件的坐标
final int action = MotionEventCompat.getActionMasked(motionEvent);
switch (action) {
case MotionEvent.ACTION_DOWN: {
final float x = motionEvent.getX();
final float y = motionEvent.getY();
// 保存初始触摸点的坐标
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = motionEvent.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = MotionEventCompat.findPointerIndex(motionEvent, mActivePointerId);
final float x = MotionEventCompat.getX(motionEvent, pointerIndex);
final float y = MotionEventCompat.getY(motionEvent, pointerIndex);
// 计算手指移动的距离
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
// 更新图片的位置
mPosX += dx;
mPosY += dy;
for (ImageView imageView : mImageViews) {
imageView.setTranslationX(mPosX);
imageView.setTranslationY(mPosY);
}
// 保存当前触摸点的坐标
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = MotionEventCompat.getActionIndex(motionEvent);
final int pointerId = MotionEventCompat.getPointerId(motionEvent, pointerIndex);
if (pointerId == mActivePointerId) {
// 如果当前手指抬起,则更新当前手指的ID
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = MotionEventCompat.getX(motionEvent, newPointerIndex);
mLastTouchY = MotionEventCompat.getY(motionEvent, newPointerIndex);
mActivePointerId = MotionEventCompat.getPointerId(motionEvent, newPointerIndex);
}
break;
}
}
// 使用手势检测器处理缩放和旋转事件
mScaleDetector.onTouchEvent(motionEvent);
mRotationDetector.onTouchEvent(motionEvent);
return true;
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
// 更新缩放比例
mScaleFactor *= detector.getScaleFactor();
// 限制缩放比例的范围
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
// 应用缩放比例
for (ImageView imageView : mImageViews) {
imageView.setScaleX(mScaleFactor);
imageView.setScaleY(mScaleFactor);
}
return true;
}
}
private class RotationListener extends RotationGestureDetector.SimpleOnRotationGestureListener {
@Override
public boolean onRotation(RotationGestureDetector detector) {
// 更新旋转角度
mRotationDegrees -= detector.getAngle();
// 应用旋转角度
for (ImageView imageView : mImageViews) {
imageView.setRotation(-mRotationDegrees);
}
return true;
}
}
}
```
需要注意的是,在布局文件中需要添加`android:orientation="vertical"`属性,以便将多张图片垂直排列。此外,还需要在`AndroidManifest.xml`文件中为该Activity添加以下权限:
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
```
阅读全文