五指抓算法

这段代码实现了对多指触摸事件的处理,包括ACTION_POINTER_DOWN和ACTION_POINTER_UP事件。当检测到三指同时下滑或五指捏合时,触发特定操作,如播放视频或启动新的Activity。
摘要由CSDN通过智能技术生成

private float downX[] = new float[5];

private float downY[] = new float[5];

private float upX[] = new float[5];

private float upY[] = new float[5];

private float mathDownX[] = new float[4];

private float mathDownY[] = new float[4];

private float mathUpX[] = new float[4];

private float mathUpY[] = new float[4];

private int mPointCount;

@Override

public boolean onTouchEvent(MotionEvent event) {

mPointCount = event.getPointerCount();

int action = event.getAction() & MotionEvent.ACTION_MASK;

switch (action) {

case MotionEvent.ACTION_POINTER_DOWN:

if (mPointCount == 3) {

for (int i = 0; i < mPointCount; i++) {

int pointerIndex = event.getPointerId(i);

if (pointerIndex < 0) {

pointerIndex = 0;

} else if (pointerIndex >= mPointCount) {

pointerIndex = mPointCount - 1;

}

downX[i] = event.getX(pointerIndex);

downY[i] = event.getY(pointerIndex);

}

}

if (mPointCount == 5) {

for (int i = 0; i < mPointCount; i++) {

int pointerIndex = event.getPointerId(i);

if (pointerIndex < 0) {

pointerIndex = 0;

} else if (pointerIndex >= mPointCount) {

pointerIndex = mPointCount - 1;

}

downX[i] = event.getX(pointerIndex);

downY[i] = event.getY(pointerIndex);

}

}

break;

case MotionEvent.ACTION_POINTER_UP:

if (mPointCount == 3) {

for (int i = 0; i < mPointCount; i++) {

int pointerIndex = event.getPointerId(i);

if (pointerIndex < 0) {

pointerIndex = 0;

} else if (pointerIndex >= mPointCount) {

pointerIndex = mPointCount - 1;

}

upX[i] = event.getX(pointerIndex);

upY[i] = event.getY(pointerIndex);

}

}

if (mPointCount == 5) {

for (int i = 0; i < mPointCount; i++) {

int pointerIndex = event.getPointerId(i);

if (pointerIndex < 0) {

pointerIndex = 0;

} else if (pointerIndex >= mPointCount) {

pointerIndex = mPointCount - 1;

}

upX[i] = event.getX(pointerIndex);

upY[i] = event.getY(pointerIndex);

}

}

Log.i(TAG, "onTouchEvent up mPointCount " + mPointCount);

int result = 0;

if (mPointCount == 3) {

for (int i = 0; i < mPointCount; i++) {

float offsetY = upY[i] - downY[i];

if (offsetY > 0) {

result += 1;

}

}

Log.i(TAG, "onTouchEvent 三指下滑 " + result);

if (result >= 3) {

playLauncherVideo();

}

} else if (mPointCount == 5) {

for (int i = 0; i < mPointCount - 1; i++) {

mathDownX[i] = Math.abs(downX[i + 1] - downX[i]);

mathDownY[i] = Math.abs(downY[i + 1] - downY[i]);

mathUpX[i] = Math.abs(upX[i + 1] - upX[i]);

mathUpY[i] = Math.abs(upY[i + 1] - upY[i]);

if (mathUpX[i] - mathDownX[i] < 0 || mathUpY[i] - mathDownY[i] < 0) {

result += 1;

}

}

Log.i(TAG, "onTouchEvent 五指捏 " + result);

if (result >= 4) {

Intent intent = new Intent();

intent.setComponent(new ComponentName("com.tinnove.facerecognition", "com.tinnove.facerecognition.BlackScreenActivity"));

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

}

}

break;

}

return super.onTouchEvent(event);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值