
如何在Android上只允许一次触摸?我不想支持多点触控,如果我的一个手指已经触摸屏幕,我希望我的应用程序放弃其他触摸.就像在iOS上使用独家触控一样.
另外,有没有办法设置允许的触摸次数?
谢谢!
编辑:
最低目标API = 8.
解决方法:
您必须跟踪第一次触摸的指针ID,并仅使用与该触摸相对应的触摸事件.这是The official Android blog “Making Sense of Multitouch “的好例子
private static final int INVALID_POINTER_ID = -1;// The ‘active pointer’ is the one currently moving our object.private int mActivePointerID = INVALID_POINTER_ID;// Existing code ...@OverrIDepublic boolean ontouchEvent(MotionEvent ev) { final int action = ev.getAction(); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); mLasttouchX = x; mLasttouchY = y; // Save the ID of this pointer mActivePointerID = ev.getPointerID(0); break; } case MotionEvent.ACTION_MOVE: { // Find the index of the active pointer and fetch its position final int pointerIndex = ev.findPointerIndex(mActivePointerID); final float x = ev.getX(pointerIndex); final float y = ev.getY(pointerIndex); final float dx = x - mLasttouchX; final float dy = y - mLasttouchY; mPosX += dx; mPosY += dy; mLasttouchX = x; mLasttouchY = y; invalIDate(); break; } case MotionEvent.ACTION_UP: { mActivePointerID = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_CANCEL: { mActivePointerID = INVALID_POINTER_ID; break; } case MotionEvent.ACTION_POINTER_UP: { // Extract the index of the pointer that left the touch sensor final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; final int pointerID = ev.getPointerID(pointerIndex); if (pointerID == mActivePointerID) { // This was our active pointer going up. Choose a new // active pointer and adjust accordingly. final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mLasttouchX = ev.getX(newPointerIndex); mLasttouchY = ev.getY(newPointerIndex); mActivePointerID = ev.getPointerID(newPointerIndex); } break; } } return true;} 总结 以上是内存溢出为你收集整理的Android:如何只允许一键?全部内容,希望文章能够帮你解决Android:如何只允许一键?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)