提兵百万西湖上,立马吴山第一峰!这篇文章主要讲述Android 手势识别—缩放相关的知识,希望能为你提供帮助。
上一篇讲解了手势识别中的点击和双击事件的识别,用到的是GestureDetector类和GestureDetectorCompat类,用于监听用户触摸屏幕中的简单动作。
缩放
基本用法如下,可以通过缩放手势缩放一个View
文章图片
文章图片
public class ScaleGestureActivity extends BaseActivity {private View mView; private ScaleGestureDetector mScaleGestureDetector; private static final String TAG = "ScaleGestureActivity"; private float mScaleFactor = 1.0f; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scale_gesture); mView = findViewById(R.id.scale_view); mScaleGestureDetector = new ScaleGestureDetector(this,mScaleGestureListener); }@Override public boolean onTouchEvent(MotionEvent event) { mScaleGestureDetector.onTouchEvent(event); return super.onTouchEvent(event); }private ScaleGestureDetector.OnScaleGestureListener mScaleGestureListener = new ScaleGestureDetector.OnScaleGestureListener() { @Override public boolean onScale(ScaleGestureDetector scaleGestureDetector) { mScaleFactor*= scaleGestureDetector.getScaleFactor(); mView.setScaleX(mScaleFactor); mView.setScaleY(mScaleFactor); Log.i(TAG, "onScale: "+ scaleGestureDetector.getScaleFactor()+"----"+mScaleFactor); return true; }@Override public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) { return true; }@Override public void onScaleEnd(ScaleGestureDetector scaleGestureDetector) {} }; }
View Code
ScaleGestureDetector 官方对该类的解释如下:
Detects scaling transformation gestures using the supplied该类的构造方法如下:MotionEvent
s. TheScaleGestureDetector.OnScaleGestureListener
callback will notify users when a particular gesture event has occurred. This class should only be used withMotionEvent
s reported via touch. To use this class:
- Create an instance of the
ScaleGestureDetector
for yourView
- In the
onTouchEvent(MotionEvent)
method ensure you callonTouchEvent(MotionEvent)
. The methods defined in your callback will be executed when the events occur.
public ScaleGestureDetector(Context context, ScaleGestureDetector.OnScaleGestureListener listener)由构造方法可知需要为一个缩放类设置一个缩放的监听接口,具体实现见代码中监听类接口的实现,下面具体介绍几个函数的含义:
- public boolean onScale(ScaleGestureDetector detector);
public boolean onScale(ScaleGestureDetector detector) { System.out.println(detector.getScaleFactor()); if(detector.getScaleFactor()< 2){ return false; } mMatrix.preScale(detector.getScaleFactor(), detector.getScaleFactor()); setImageMatrix(mMatrix); return true; }
2.public boolean onScaleBegin(ScaleGestureDetector detector)
缩放开始时会被调用。返回值表示该detector是否处理后继的缩放事件。返回false时,不会执行后续的onScale()。
3. public void onScaleEnd(ScaleGestureDetector scaleGestureDetector)
缩放结束时的回调
http://blog.csdn.net/qiwenmingshiwo/article/details/70850406
https://github.com/Almeros/android-gesture-detectors
【Android 手势识别—缩放】https://github.com/Almeros/android-gesture-detectors-example/blob/master/app/src/main/java/com/almeros/android/multitouch/example/TouchActivity.java
推荐阅读
- 博客园app for xamarin android
- 安卓开发——定位管理
- android user用户版本提高adb权限
- android user 版本如何默认adb调试为打开
- spring-boot application.properties设置最全指南
- Android系统设置Android adb 开关的方法
- 关于如何在Visual Studio上仿真调试安卓的U3D应用
- android SDK安装配置
- 张高兴的 Xamarin.Forms 开发笔记(Android 快捷方式 Shortcut 应用)