安卓实现双指滑动图片放大缩小,单指滑动图片移动,双击图片放大,再一次双击图片复原
时间: 2023-09-29 12:09:52 浏览: 99
实现双指滑动图片放大缩小,单指滑动图片移动,双击图片放大,再一次双击图片复原的效果,可以通过以下步骤实现:
1. 在布局文件中添加一个 ImageView,设置其初识状态为居中显示一张图片。
2. 在代码中获取该 ImageView,并设置其可缩放、可拖动。
3. 通过 GestureDetector 监听用户手势操作,判断用户是单指还是双指操作,并做出相应的处理。
4. 对于双指操作,可以通过 ScaleGestureDetector 监听缩放比例的变化,从而实现图片的放大缩小。
5. 对于单指操作,可以通过手势监听器中的 onTouchEvent 方法来处理图片的移动。
6. 对于双击操作,可以通过 GestureDetector 中的 onDoubleTap 方法来实现图片的放大和复原。
以下是一个简单的实现代码示例:
```
public class MainActivity extends AppCompatActivity {
private ImageView mImageView;
private float mScaleFactor = 1.0f;
private float mLastTouchX, mLastTouchY;
private int mActivePointerId = -1;
private ScaleGestureDetector mScaleGestureDetector;
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = findViewById(R.id.imageView);
mScaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());
mGestureDetector = new GestureDetector(this, new GestureListener());
// 设置图片可缩放、可拖动
mImageView.setScaleType(ImageView.ScaleType.MATRIX);
mImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleGestureDetector.onTouchEvent(event);
mGestureDetector.onTouchEvent(event);
final int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = event.getX();
final float y = event.getY();
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = event.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = event.findPointerIndex(mActivePointerId);
final float x = event.getX(pointerIndex);
final float y = event.getY(pointerIndex);
if (!mScaleGestureDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mImageView.postTranslate(dx, dy);
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = -1;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = -1;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = event.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = event.getX(newPointerIndex);
mLastTouchY = event.getY(newPointerIndex);
mActivePointerId = event.getPointerId(newPointerIndex);
}
break;
}
}
mImageView.invalidate();
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, 10.0f));
mImageView.setScaleX(mScaleFactor);
mImageView.setScaleY(mScaleFactor);
return true;
}
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDoubleTap(MotionEvent e) {
if (mScaleFactor == 1.0f) {
mScaleFactor = 2.0f;
} else {
mScaleFactor = 1.0f;
}
mImageView.setScaleX(mScaleFactor);
mImageView.setScaleY(mScaleFactor);
return true;
}
}
}
```
阅读全文