android第三方手势,Android手势识别02——旋转、移动等使用第三方库

Android手势识别02——旋转、移动等使用第三方库

一、简要说明

通过前面一篇文章(Android手势识别01——基本手势的处理)的学习,我们知道了基本手势的使用方法。但是我们发现这里基本上手势的使用方法中,竟然没有旋转这种手势,是不是很不可思议呢?当然你也可以自己来写这种手势,其实有现成的手势库可以用了哦? 那么我们就来用用这个库

我们可以看看这个库的说明:

1.1 Containments

RotateGestureDetector

Helps finding out the rotation angle between the line of two fingers (with the

normal or previous finger positions)

MoveGestureDetector

Convenience gesture detector to keep it easy moving an object around with one

ore more fingers without losing the clean usage of the gesture detector pattern.

ShoveGestureDetector

Detects a vertical two-finger shove. (If you place two fingers on screen with less than a 20 degree angle between them,

this will detact movement on the Y-axis.)

ScaleGestureDetector (default Android)

This one is NOT in this framework, but is gesture detector that resides in the

Android API since API level 8 (2.2).

BaseGestureDetector (abstract)

Abstract class that every new gesture detector class should extend.

TwoFingerGestureDetector (abstract)

Abstract class that extends the BaseGestureDetector and that every new gesture

detector class for two finger operations should extend.

1.2 库的使用方式简要说明

这个库的使用方式和 Android原生库的使用方式一模一样。

1.创建监听的实例

2.创建手势探测器

3.重写onTouchEvent方法,这个这个方法内调用探测器的onTouchEvent()

二、RotateGestureDetector的使用

2.1 RotateGestureDetector和RotateGestureDetector.SimpleOnRotateGestureListener的简要说明

RotateGestureDetector 用处创建旋转手势的探测器。其中 getRotationDegreesDelta() 用户获取旋转的角度(单位:角度),值为一个相对值,相对与上一次旋转的角度值,即以上一次为起点。

RotateGestureDetector.SimpleOnRotateGestureListener 用于创建堂测器的监听。有三个事件

public boolean onRotate(RotateGestureDetector detector) {

return false;

}

public boolean onRotateBegin(RotateGestureDetector detector) {

return true;

}

public void onRotateEnd(RotateGestureDetector detector) {

// Do nothing, overridden implementation may be used

}

旋转开始:调用onRotateBegin,旋转过程中不停的调用onRotate,结束调用onRotateEnd

2.2 RotateGestureActivity.java

/** * Project: gesturedemo
* Create Date: 2017/4/26
* Author: qiwenming
* Description:
*/

public class RotateGestureActivity extends BaseActivity {

private View mView;

private RotateGestureDetector mRotateGestureDetector;

private float mRotationDegrees = 0.f;

private static final String TAG = "RotateGestureActivity";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_rotate_gesture);

mView = findViewById(R.id.rotate_view);

mRotateGestureDetector = new RotateGestureDetector(this,mSimpleOnRotateGestureListener);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

mRotateGestureDetector.onTouchEvent(event);

return super.onTouchEvent(event);

}

private RotateGestureDetector.SimpleOnRotateGestureListener mSimpleOnRotateGestureListener = new RotateGestureDetector.SimpleOnRotateGestureListener(){

@Override

public boolean onRotate(RotateGestureDetector detector) {

mRotationDegrees = -detector.getRotationDegreesDelta()+mRotationDegrees;

mRotationDegrees = mRotationDegrees % 360;

mView.setRotation(mRotationDegrees);

Log.i(TAG, "onRotate: "+detector.getRotationDegreesDelta()+"----"+mRotationDegrees);

return true;

}

};

}

2.3 旋转图示

0818b9ca8b590ca3270a3433284dd417.png

三、MoveGestureDetector的使用

3.1 MoveGestureDetector和MoveGestureDetector.SimpleOnMoveGestureListener的简要说明

MoveGestureDetector 移动的探测器。其中getFocusDelta()获取移动的值,这个值是相对于按下的按个点。

MoveGestureDetector.SimpleOnMoveGestureListener 移动的监听。

3.2 MoveGestureActivity.java

MoveGestureActivity

/** * Project: YuantaiApplication
* Create Date: 2017/4/27
* Author: qiwenming
* Description:
* 移动 */

public class MoveGestureActivity extends BaseActivity {

private View mView;

private MoveGestureDetector mMoveGestureDetector;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_move_gesture);

mView = findViewById(R.id.move_view);

mMoveGestureDetector = new MoveGestureDetector(this,mSimpleOnMoveGestureListener);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

mMoveGestureDetector.onTouchEvent(event);

return super.onTouchEvent(event);

}

private float mOffsetX = 0;

private float mOffsetY = 0;

private float mLastX = 0;

private float mLastY = 0;

private MoveGestureDetector.SimpleOnMoveGestureListener mSimpleOnMoveGestureListener = new MoveGestureDetector.SimpleOnMoveGestureListener(){

@Override

public boolean onMove(MoveGestureDetector detector) {

mOffsetX = detector.getFocusDelta().x+mLastX;

mOffsetY = detector.getFocusDelta().y+mLastY;

mView.setTranslationX(mOffsetX);

mView.setTranslationY(mOffsetY);

return super.onMove(detector);

}

@Override

public void onMoveEnd(MoveGestureDetector detector) {

mLastX = mOffsetX;

mLastY = mOffsetY;

super.onMoveEnd(detector);

}

};

}

3.3 移动图示

0818b9ca8b590ca3270a3433284dd417.png

四、源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值