初识matlab及图像处理之图片平移、旋转、水平镜像、垂直镜像、放大、缩小 (1) 实现双指滑动图片放大缩小,单指滑动图片移动,双击图片放大,再一次双击图片复原的效果,可以通过以下步骤实现: 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; } } } “`
2024最新激活全家桶教程,稳定运行到2099年,请移步至置顶文章:https://sigusoft.com/99576.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。 文章由激活谷谷主-小谷整理,转载请注明出处:https://sigusoft.com/90079.html