展会信息港展会大全

Android触摸屏多点手势识别
来源:互联网   发布日期:2015-09-29 10:27:19   浏览:2734次  

导读:google 提供的API中,有个类,大家都很熟悉,GestureDetector。使用它,我们可以识别用户通常会用的手势。但是,这个类不支持多点触摸(可能 google认为没有人会在几个手指都在屏幕上的时候,使用手势吧~),不过......

google 提供的API中,有个类,大家都很熟悉,GestureDetector。使用它,我们可以识别用户通常会用的手势。但是,这个类不支持多点触摸(可能 google认为没有人会在几个手指都在屏幕上的时候,使用手势吧~),不过,最近和朋友们一起做的一个App,的确用到了多点手势(主要是 onScroll和onFling两个手势),所以,我就把这个类拓展了一下,来实现让多个控件各自跟着一跟手指实现拖动和滑动的效果。

顺便说一下,大家应该都知道,在Android3.0以后,Android的触摸事件的分配机制和以前的版本是有区别的。从3.0开始,用户在不同控件上操作产生的touch消息不会相互干扰,touch消息会被分派到不同控件上的touchListener中处理。而

在以前的版本中,所有的touch消息,都会被分排到第一个碰到屏幕的手指所操作的控件的touchListener中处理,也就是说,会出现这样一个矛盾的现象:

在界面上有A,B,C三个控件,然后,当你先用食指按住A,跟着又用中指和无名指(嘛,别的手指也行,不用在意中指还是无名指)按住B,C。当中指和无名指移动的时候,B和C都无法接收到这个ACTION_MOVE消息,而接收到消息的却是A。而在3.0以上版本中,并不存在这个问题。

使用以下的这个类,可以实现从2.2到3.2平台上手势识别的兼容。

[代码] [Java]代码

001package com.finger.utils;

002import java.util.ArrayList;

003import java.util.List;

004import android.content.Context;

005import android.os.Handler;

006import android.os.Message;

007import android.view.MotionEvent;

008import android.view.VelocityTracker;

009import android.view.ViewConfiguration;

010public class MultiTouchGestureDetector {

011 @SuppressWarnings("unused")

012 private static final String MYTAG = "Ray";

013 public static final String CLASS_NAME ="MultiTouchGestureDetector";

014 /**

015 * 事件信息类

016 * 用来记录一个手势

017 */

018 private class EventInfo {

019 private MultiMotionEvent mCurrentDownEvent; //当前的down事件

020 private MultiMotionEvent mPreviousUpEvent; //上一次up事件

021 private boolean mStillDown; //当前手指是否还在屏幕上

022 private boolean mInLongPress; //当前事件是否属于长按手势

023 private boolean mAlwaysInTapRegion; //是否当前手指仅在小范围内移动,当手指仅在小范围内移动时,视为手指未曾移动过,不会触发onScroll手势

024 private boolean mAlwaysInBiggerTapRegion; //是否当前手指在较大范围内移动,仅当此值为true时,双击手势才能成立

025 private boolean mIsDoubleTapping; //当前手势,是否为双击手势

026 private float mLastMotionY; //最后一次事件的X坐标

027 private float mLastMotionX; //最后一次事件的Y坐标

028 private EventInfo(MotionEvent e) {

029 this(new MultiMotionEvent(e));

030 }

031 private EventInfo(MultiMotionEvent me) {

032 mCurrentDownEvent = me;

033 mStillDown = true;

034 mInLongPress = false;

035 mAlwaysInTapRegion = true;

036 mAlwaysInBiggerTapRegion = true;

037 mIsDoubleTapping = false;

038 }

039 //释放MotionEven对象,使系统能够继续使用它们

040 public void recycle() {

041 if (mCurrentDownEvent != null) {

042 mCurrentDownEvent.recycle();

043 mCurrentDownEvent = null;

044 }

045 if (mPreviousUpEvent != null) {

046 mPreviousUpEvent.recycle();

047 mPreviousUpEvent = null;

048 }

049 }

050 @Override

051 public void finalize() {

052 this.recycle();

053 }

054 }

055 /**

056 * 多点事件类

057 * 将一个多点事件拆分为多个单点事件,并方便获得事件的绝对坐标

058 *

绝对坐标用以在界面中找到触点所在的控件

059 * @author ray-ni

060 */

061 public class MultiMotionEvent {

062 private MotionEvent mEvent;

063 private int mIndex;

064 private MultiMotionEvent(MotionEvent e) {

065 mEvent = e;

066 mIndex = (e.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT; //等效于 mEvent.getActionIndex();

067 }

068 private MultiMotionEvent(MotionEvent e, int idx) {

069 mEvent = e;

070 mIndex = idx;

071 }

072 // 行为

073 public int getAction() {

074 int action = mEvent.getAction() & MotionEvent.ACTION_MASK; //等效于 mEvent.getActionMasked();

075 switch (action) {

076 case MotionEvent.ACTION_POINTER_DOWN:

077 action = MotionEvent.ACTION_DOWN;

078 break;

079 case MotionEvent.ACTION_POINTER_UP:

080 action = MotionEvent.ACTION_UP;

081 break;

082 }

083 return action;

084 }

085 // 返回X的绝对坐标

086 public float getX() {

087 return mEvent.getX(mIndex) + mEvent.getRawX() - mEvent.getX();

088 }

089 // 返回Y的绝对坐标

090 public float getY() {

091 return mEvent.getY(mIndex) + mEvent.getRawY() - mEvent.getY();

092 }

093 // 事件发生的时间

094 public long getEventTime() {

095 return mEvent.getEventTime();

096 }

097 // 事件序号

098 public int getIndex() {

099 return mIndex;

100 }

101

102 // 事件ID

103 public int getId() {

104 return mEvent.getPointerId(mIndex);

105 }

106

107 // 释放事件对象,使系统能够继续使用

108 public void recycle() {

109 if (mEvent != null) {

110 mEvent.recycle();

111 mEvent = null;

112 }

113 }

114 }

115 // 多点手势监听器

116 public interface MultiTouchGestureListener {

117 // 手指触碰到屏幕,由一个 ACTION_DOWN触发

118 boolean onDown(MultiMotionEvent e);

119 // 确定一个press事件,强调手指按下的一段时间(TAP_TIMEOUT)内,手指未曾移动或抬起

120 void onShowPress(MultiMotionEvent e);

121 // 手指点击屏幕后离开,由 ACTION_UP引发,可以简单的理解为单击事件,即手指点击时间不长(未构成长按事件),也不曾移动过

122 boolean onSingleTapUp(MultiMotionEvent e);

123 // 长按,手指点下后一段时间(DOUBLE_TAP_TIMEOUT)内,不曾抬起或移动

124 void onLongPress(MultiMotionEvent e);

125 // 拖动,由ACTION_MOVE触发,手指地按下后,在屏幕上移动

126 boolean onScroll(MultiMotionEvent e1, MultiMotionEvent e2,float distanceX, float distanceY);

127 // 滑动,由ACTION_UP触发,手指按下并移动一段距离后,抬起时触发

128 boolean onFling(MultiMotionEvent e1, MultiMotionEvent e2,float velocityX, float velocityY);

129 }

130 // 多点双击监听器

131 public interface MultiTouchDoubleTapListener {

132 // 单击事件确认,强调第一个单击事件发生后,一段时间内,未发生第二次单击事件,即确定不会触发双击事件

133 boolean onSingleTapConfirmed(MultiMotionEvent e);

134 // 双击事件, 由ACTION_DOWN触发,从第一次单击事件的DOWN事件开始的一段时间(DOUBLE_TAP_TIMEOUT)内结束(即手指),

135 // 并且在第一次单击事件的UP时间开始后的一段时间内(DOUBLE_TAP_TIMEOUT)发生第二次单击事件,

136 // 除此之外两者坐标间距小于定值(DOUBLE_TAP_SLAP)时,则触发双击事件

137 boolean onDoubleTap(MultiMotionEvent e);

138 // 双击事件,与onDoubleTap事件不同之处在于,构成双击的第二次点击的ACTION_DOWN,ACTION_MOVE和ACTION_UP都会触发该事件

139 boolean onDoubleTapEvent(MultiMotionEvent e);

140 }

141 // 事件信息队列,队列的下标与MotionEvent的pointId对应

142 private static List sEventInfos = newArrayList(10);

143 // 双击判断队列,这个队列中的元素等待双击超时的判断结果

144 private static List sEventForDoubleTap = newArrayList(5);

145 // 指定大点击区域的大小(这个比较拗口),这个值主要用于帮助判断双击是否成立

146 private int mBiggerTouchSlopSquare = 20 * 20;

147 // 判断是否构成onScroll手势,当手指在这个范围内移动时,不触发onScroll手势

148 private int mTouchSlopSquare;

149 // 判断是否构成双击,只有两次点击的距离小于该值,才能构成双击手势

150 private int mDoubleTapSlopSquare;

151 // 最小滑动速度

152 private int mMinimumFlingVelocity;

153 // 最大滑动速度

154 private int mMaximumFlingVelocity;

155

156 // 长按阀值,当手指按下后,在该阀值的时间内,未移动超过mTouchSlopSquare的距离并未抬起,则长按手势触发

157 private static final int LONGPRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout();

158 // showPress手势的触发阀值,当手指按下后,在该阀值的时间内,未移动超过mTouchSlopSquare的距离并未抬起,则showPress手势触发

159 private static final int TAP_TIMEOUT = ViewConfiguration.getTapTimeout();

160 // 双击超时阀值,仅在两次双击事件的间隔(第一次单击的UP事件和第二次单击的DOWN事件)小于此阀值,双击事件才能成立

161 private static final int DOUBLE_TAP_TIMEOUT = ViewConfiguration.getDoubleTapTimeout();

162 // 双击区域阀值,仅在两次双击事件的距离小于此阀值,双击事件才能成立

163 private static final int DOUBLE_TAP_SLAP = 64;

164

165 // GestureHandler所处理的Message的what属性可能为以下 常量:

166 // showPress手势

167 private static final int SHOW_PRESS = 1;

168 // 长按手势

169 private static final int LONG_PRESS = 2;

170 // SingleTapConfirmed手势

171 private static final int TAP_SINGLE = 3;

172 // 双击手势

173 private static final int TAP_DOUBLE = 4;

174

175 // 手势处理器

176 private final GestureHandler mHandler;

177 // 手势监听器

178 private final MultiTouchGestureListener mListener;

179 // 双击监听器

180 private MultiTouchDoubleTapListener mDoubleTapListener;

181

182 // 长按允许阀值

183 private boolean mIsLongpressEnabled;

184 // 速度追踪器

185 private VelocityTracker mVelocityTracker;

186

187 private class GestureHandler extends Handler {

188 GestureHandler() {

189 super();

190 }

191 GestureHandler(Handler handler) {

192 super(handler.getLooper());

193 }

194 @Override

195 public void handleMessage(Message msg) {

196 int idx = (Integer) msg.obj;

197 switch (msg.what) {

198 case SHOW_PRESS: {

199 if (idx >= sEventInfos.size()) {

200// Log.w(MYTAG, CLASS_NAME + ":handleMessage, msg.what = SHOW_PRESS, idx=" + idx + ", while sEventInfos.size()="

201// + sEventInfos.size());

202 break;

203 }

204 EventInfo info = sEventInfos.get(idx);

205 if (info == null) {

206// Log.e(MYTAG, CLASS_NAME + ":handleMessage, msg.what = SHOW_PRESS, idx=" + idx + ", Info = null");

207 break;

208 }

209 // 触发手势监听器的onShowPress事件

210 mListener.onShowPress(info.mCurrentDownEvent);

211 break;

212 }

213 case LONG_PRESS: {

214 // Log.d(MYTAG, CLASS_NAME + ":trigger LONG_PRESS");

215

216

217 if (idx >= sEventInfos.size()) {

218// Log.w(MYTAG, CLASS_NAME + ":handleMessage, msg.what = LONG_PRESS, idx=" + idx + ", while sEventInfos.size()="

219// + sEventInfos.size());

220 break;

221 }

222 EventInfo info = sEventInfos.get(idx);

223 if (info == null) {

224// Log.e(MYTAG, CLASS_NAME + ":handleMessage, msg.what = LONG_PRESS, idx=" + idx + ", Info = null");

225 break;

226 }

227 dispatchLongPress(info, idx);

228 break;

229 }

230 case TAP_SINGLE: {

231 // Log.d(MYTAG, CLASS_NAME + ":trriger TAP_SINGLE");

232 // If the user's finger is still down, do not count it as a tap

233 if (idx >= sEventInfos.size()) {

234// Log.e(MYTAG, CLASS_NAME + ":handleMessage, msg.what = TAP_SINGLE, idx=" + idx + ", while sEventInfos.size()="

235// + sEventInfos.size());

236 break;

237 }

238 EventInfo info = sEventInfos.get(idx);

239 if (info == null) {

240// Log.e(MYTAG, CLASS_NAME + ":handleMessage, msg.what = TAP_SINGLE, idx=" + idx + ", Info = null");

241 break;

242 }

243 if (mDoubleTapListener != null && !info.mStillDown) {//手指在双击超时的阀值内未离开屏幕进行第二次单击事件,则确定单击事件成立(不再触发双击事件)

244 mDoubleTapListener.onSingleTapConfirmed(info.mCurrentDownEvent);

245 }

246 break;

247 }

248 case TAP_DOUBLE: {

249 if (idx >= sEventForDoubleTap.size()) {

250// Log.w(MYTAG, CLASS_NAME + ":handleMessage, msg.what = TAP_DOUBLE, idx=" + idx + ", while sEventForDoubleTap.size()="

251// + sEventForDoubleTap.size());

252 break;

253 }

254 EventInfo info = sEventForDoubleTap.get(idx);

255 if (info == null) {

256// Log.w(MYTAG, CLASS_NAME + ":handleMessage, msg.what = TAP_DOUBLE, idx=" + idx + ", Info = null");

257 break;

258 }

259 sEventForDoubleTap.set(idx, null);// 这个没什么好做的,就是把队列中对应的元素清除而已

260 break;

261 }

262 default:

263 throw new RuntimeException("Unknown message " + msg);// never

264 }

265 }

266 }

267 /**

268 * 触发长按事件

269 * @param info

270 * @param idx

271 */

272 private void dispatchLongPress(EventInfo info, int idx) {

273 mHandler.removeMessages(TAP_SINGLE, idx);//移除单击事件确认

274 info.mInLongPress = true;

275 mListener.onLongPress(info.mCurrentDownEvent);

276 }

277

278 /**

279 * 构造器1

280 * @param context

281 * @param listener

282 */

283 public MultiTouchGestureDetector(Context context, MultiTouchGestureListener listener) {

284 this(context, listener, null);

285 }

286 /**

287 * 构造器2

288 * @param context

289 * @param listener

290 * @param handler

291 */

292 public MultiTouchGestureDetector(Context context, MultiTouchGestureListener listener, Handler handler) {

293 if (handler != null) {

294 mHandler = new GestureHandler(handler);

295 } else {

296 mHandler = new GestureHandler();

297 }

298 mListener = listener;

299 if (listener instanceof MultiTouchDoubleTapListener) {

300 setOnDoubleTapListener((MultiTouchDoubleTapListener) listener);

301 }

302 init(context);

303 }

304 /**

305 * 初始化识别器

306 * @param context

307 */

308 private void init(Context context) {

309 if (mListener == null) {

310 throw new NullPointerException("OnGestureListener must not be null");

311 }

312 mIsLongpressEnabled = true;

313 int touchSlop, doubleTapSlop;

314 if (context == null) {

315 touchSlop = ViewConfiguration.getTouchSlop();

316 doubleTapSlop = DOUBLE_TAP_SLAP;

317 mMinimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity();

318 mMaximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity();

319 } else {//允许识别器在App中,使用偏好的设定

320 final ViewConfiguration configuration = ViewConfiguration.get(context);

321 touchSlop = configuration.getScaledTouchSlop();

322 doubleTapSlop = configuration.getScaledDoubleTapSlop();

323 mMinimumFlingVelocity = configuration.getScaledMinimumFlingVelocity();

324 mMaximumFlingVelocity = configuration.getScaledMaximumFlingVelocity();

325 }

326 mTouchSlopSquare = touchSlop * touchSlop / 16;

327 mDoubleTapSlopSquare = doubleTapSlop * doubleTapSlop;

328 }

329 /**

330 * 设置双击监听器

331 * @param onDoubleTapListener

332 */

333 public void setOnDoubleTapListener(MultiTouchDoubleTapListener onDoubleTapListener) {

334 mDoubleTapListener = onDoubleTapListener;

335 }

336 /**

337 * 设置是否允许长按

338 * @param isLongpressEnabled

339 */

340 public void setIsLongpressEnabled(boolean isLongpressEnabled) {

341 mIsLongpressEnabled = isLongpressEnabled;

342 }

343 /**

344 * 判断是否允许长按

345 * @return

346 */

347 public boolean isLongpressEnabled() {

348 return mIsLongpressEnabled;

349 }

350 /**

351 * 判断当前事件是否为双击事件

352 *

通过遍历sEventForDoubleTap来匹配是否存在能够构成双击事件的单击事件

353 * @param e

354 * @return

355 */

356 private EventInfo checkForDoubleTap(MultiMotionEvent e) {

357 if (sEventForDoubleTap.isEmpty()) {

358// Log.e(MYTAG, CLASS_NAME + ":checkForDoubleTap(), sEventForDoubleTap is empty !");

359 return null;

360 }

361 for (int i = 0; i < sEventForDoubleTap.size(); i++) {

362 EventInfo info = sEventForDoubleTap.get(i);

363 if (info != null && isConsideredDoubleTap(info, e)) {

364 sEventForDoubleTap.set(i, null);// 这个单击事件已经被消耗了,所以置为null

365 mHandler.removeMessages(TAP_DOUBLE, i);// 移除Handler内的为处理消息

366 return info;

367 }

368 }

369 return null;

370 }

371 /**

372 * 判断当前按下事件是否能和指定的单击事件构成双击事件

373 *

374 * @param info

375 * @param secondDown

376 * @return

377 */

378 private boolean isConsideredDoubleTap(EventInfo info, MultiMotionEvent secondDown) {

379 if (!info.mAlwaysInBiggerTapRegion) { //如多第一次单击事件有过较大距离的移动,则无法构成双击事件

380 return false;

381 }

382 if (secondDown.getEventTime() - info.mPreviousUpEvent.getEventTime() > DOUBLE_TAP_TIMEOUT) {

383 //如果第一次单击的UP时间和第二次单击的down时间时间间隔大于DOUBLE_TAP_TIMEOUT,也无法构成双击事件

384 return false;

385 }

386 int deltaX = (int) info.mCurrentDownEvent.getX() - (int) secondDown.getX();

387 int deltaY = (int) info.mCurrentDownEvent.getY() - (int) secondDown.getY();

388 return (deltaX * deltaX + deltaY * deltaY < mDoubleTapSlopSquare);//最后判断两次单击事件的距离

389

390 }

391

392

393 /**

394 * 将事件信息放入双击判断队列,并返回序号

395 *

396 * @param info

397 * @return

398 */

399 private int addIntoTheMinIndex(EventInfo info) {

400 for (int i = 0; i < sEventForDoubleTap.size(); i++) {

401 if (sEventForDoubleTap.get(i) == null) {

402 sEventForDoubleTap.set(i, info);

403 return i;

404 }

405 }

406 sEventForDoubleTap.add(info);

407 return sEventForDoubleTap.size() - 1;

408 }

409

410 /**

411 * 从事件信息队列中移除指定序号的事件

412 *

413 * @param idx

414 */

415 private void removeEventFromList(int id) {

416 if (id > sEventInfos.size() || id < 0) {

417// Log.e(MYTAG, CLASS_NAME + ".removeEventFromList(), id=" + id + ", while sEventInfos.size() =" + sEventInfos.size());

418 return;

419 }

420 sEventInfos.set(id, null);

421 }

422

423 /**

424 * 向事件队列中添加新信息

425 *

426 * @param e

427 */

428 private void addEventIntoList(EventInfo info) {

429 int id = info.mCurrentDownEvent.getId();

430 if (id < sEventInfos.size()) {

431// if (sEventInfos.get(id) != null)

432// Log.e(MYTAG, CLASS_NAME + ".addEventIntoList, info(" + id + ") has not set to null !");

433 sEventInfos.set(info.mCurrentDownEvent.getId(), info);

434 } else if (id == sEventInfos.size()) {

435 sEventInfos.add(info);

436 } else {

437// Log.e(MYTAG, CLASS_NAME + ".addEventIntoList, invalidata id !");

438 }

439 }

440

441 public boolean onTouchEvent(MotionEvent ev) {

442 if (mVelocityTracker == null) {

443 mVelocityTracker = VelocityTracker.obtain();

444 }

445 mVelocityTracker.addMovement(ev);//把所有事件都添加到速度追踪器,为计算速度做准备

446 boolean handled = false;

447 final int action = ev.getAction(); //获取Action

448// int idx = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;//获取触摸事件的序号

449 int idx = ev.getPointerId(ev.getActionIndex());//获取触摸事件的id

450 switch (action & MotionEvent.ACTION_MASK) {

451 case MotionEvent.ACTION_DOWN:

452 case MotionEvent.ACTION_POINTER_DOWN: {

453 EventInfo info = new EventInfo(MotionEvent.obtain(ev));

454 this.addEventIntoList(info);//将手势信息保存到队列中

455 if (mDoubleTapListener != null) {//如果双击监听器不为null

456 if (mHandler.hasMessages(TAP_DOUBLE)) {

457 MultiMotionEvent e = new MultiMotionEvent(ev);

458 EventInfo origInfo = checkForDoubleTap(e);//检查是否构成双击事件

459 if (origInfo != null) {

460 info.mIsDoubleTapping = true;

461 handled |= mDoubleTapListener.onDoubleTap(origInfo.mCurrentDownEvent);

462 handled |= mDoubleTapListener.onDoubleTapEvent(e);

463 }

464 }

465 if (!info.mIsDoubleTapping) {//当前事件不构成双击事件,那么发送延迟消息以判断onSingleTapConfirmed事件

466 mHandler.sendMessageDelayed(mHandler.obtainMessage(TAP_SINGLE, idx), DOUBLE_TAP_TIMEOUT);

467 // Log.d(MYTAG, CLASS_NAME + ": add TAP_SINGLE");

468 }

469 }

470 // 记录X坐标和Y坐标

471 info.mLastMotionX = info.mCurrentDownEvent.getX();

472 info.mLastMotionY = info.mCurrentDownEvent.getY();

473

474 if (mIsLongpressEnabled) {//允许长按

475 mHandler.removeMessages(LONG_PRESS, idx);

476 mHandler.sendMessageAtTime(mHandler.obtainMessage(LONG_PRESS, idx), info.mCurrentDownEvent.getEventTime() + TAP_TIMEOUT

477 + LONGPRESS_TIMEOUT);//延时消息以触发长按手势

478 // Log.d(MYTAG, CLASS_NAME +

479 // ":add LONG_PRESS to handler for idx " + idx);

480 }

481 mHandler.sendMessageAtTime(mHandler.obtainMessage(SHOW_PRESS, idx), info.mCurrentDownEvent.getEventTime() + TAP_TIMEOUT);// 延时消息,触发showPress手势

482 handled |= mListener.onDown(info.mCurrentDownEvent);//触发onDown()

483 break;

484 }

485 case MotionEvent.ACTION_UP:

486 case MotionEvent.ACTION_POINTER_UP: {

487 MultiMotionEvent currentUpEvent = newMultiMotionEvent(ev);

488 if (idx >= sEventInfos.size()) {

489// Log.e(MYTAG, CLASS_NAME + ":ACTION_POINTER_UP, idx=" + idx + ", while sEventInfos.size()=" + sEventInfos.size());

490 break;

491 }

492 EventInfo info = sEventInfos.get(currentUpEvent.getId());

493 if (info == null) {

494// Log.e(MYTAG, CLASS_NAME + ":ACTION_POINTER_UP, idx=" + idx + ", Info = null");

495 break;

496 }

497 info.mStillDown = false;

498 if (info.mIsDoubleTapping) { //处于双击状态,则触发onDoubleTapEvent事件

499 handled |= mDoubleTapListener.onDoubleTapEvent(currentUpEvent);

500 } else if (info.mInLongPress) {//处于长按状态

501 mHandler.removeMessages(TAP_SINGLE, idx);//可以无视这行代码

502 info.mInLongPress = false;

503 } else if (info.mAlwaysInTapRegion) {//尚未移动过

504 if (mHandler.hasMessages(TAP_SINGLE, idx)) {//还在双击的时间阀值内,所以要为双击判断做额外处理

505 mHandler.removeMessages(TAP_SINGLE, idx);

506 info.mPreviousUpEvent = newMultiMotionEvent(MotionEvent.obtain(ev));

507 int index = this.addIntoTheMinIndex(info);// 把当前事件放入队列,等待双击的判断

508 mHandler.sendMessageAtTime(mHandler.obtainMessage(TAP_DOUBLE, index), info.mCurrentDownEvent.getEventTime()

509 + DOUBLE_TAP_TIMEOUT); // 将双击超时判断添加到Handler

510 // Log.d(MYTAG, CLASS_NAME + ": add TAP_DOUBLE");

511 }

512 handled = mListener.onSingleTapUp(currentUpEvent);//触发onSingleTapUp事件

513 } else {

514 // A fling must travel the minimum tap distance

515 final VelocityTracker velocityTracker = mVelocityTracker;

516 velocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);//计算1秒钟内的滑动速度

517 //获取X和Y方向的速度

518 final float velocityX = velocityTracker.getXVelocity(idx);

519 final float velocityY = velocityTracker.getYVelocity(idx);

520 // Log.i(MYTAG, CLASS_NAME + ":ACTION_POINTER_UP, idx=" + idx +

521 // ", vx=" + velocityX + ", vy=" + velocityY);

522 // 触发滑动事件

523 if ((Math.abs(velocityY) > mMinimumFlingVelocity) || (Math.abs(velocityX) > mMinimumFlingVelocity)) {

524 handled = mListener.onFling(info.mCurrentDownEvent, currentUpEvent, velocityX, velocityY);

525 }

526 }

527 // Hold the event we obtained above - listeners may have changed the

528 // original.

529 if (action == MotionEvent.ACTION_UP) { //释放速度追踪器

530 mVelocityTracker.recycle();

531 mVelocityTracker = null;

532 // Log.w(MYTAG, CLASS_NAME +

533 // ":ACTION_POINTER_UP, mVelocityTracker.recycle()");

534 }

535

536 info.mIsDoubleTapping = false;

537 // Log.d(MYTAG, CLASS_NAME + "remove LONG_PRESS");

538 // 移除showPress和长按消息

539 mHandler.removeMessages(SHOW_PRESS, idx);

540 mHandler.removeMessages(LONG_PRESS, idx);

541 removeEventFromList(currentUpEvent.getId());//手指离开,则从队列中删除手势信息

542 break;

543 }

544 case MotionEvent.ACTION_MOVE:

545 for (int rIdx = 0; rIdx < ev.getPointerCount(); rIdx++) {//因为无法确定当前发生移动的是哪个手指,所以遍历处理所有手指

546 MultiMotionEvent e = new MultiMotionEvent(ev, rIdx);

547 if (e.getId() >= sEventInfos.size()) {

548// Log.e(MYTAG, CLASS_NAME + ":ACTION_MOVE, idx=" + rIdx + ", while sEventInfos.size()=" + sEventInfos.size());

549 break;

550 }

551 EventInfo info = sEventInfos.get(e.getId());

552 if (info == null) {

553// Log.e(MYTAG, CLASS_NAME + ":ACTION_MOVE, idx=" + rIdx + ", Info = null");

554 break;

555 }

556 if (info.mInLongPress) { //长按,则不处理move事件

557 break;

558 }

559 //当前坐标

560 float x = e.getX();

561 float y = e.getY();

562 //距离上次事件移动的位置

563 final float scrollX = x - info.mLastMotionX;

564 final float scrollY = y - info.mLastMotionY;

565 if (info.mIsDoubleTapping) {//双击事件

566 handled |= mDoubleTapListener.onDoubleTapEvent(e);

567 } else if (info.mAlwaysInTapRegion) {//该手势尚未移动过(移动的距离小于mTouchSlopSquare,视为未移动过)

568 // 计算从落下到当前事件,移动的距离

569 final int deltaX = (int) (x - info.mCurrentDownEvent.getX());

570 final int deltaY = (int) (y - info.mCurrentDownEvent.getY());

571 // Log.d(MYTAG, CLASS_NAME + "deltaX="+deltaX+";deltaY=" +

572 // deltaX +"mTouchSlopSquare=" + mTouchSlopSquare);

573 int distance = (deltaX * deltaX) + (deltaY * deltaY);

574 if (distance > mTouchSlopSquare) { // 移动距离超过mTouchSlopSquare

575 handled = mListener.onScroll(info.mCurrentDownEvent, e, scrollX, scrollY);

576 info.mLastMotionX = e.getX();

577 info.mLastMotionY = e.getY();

578 info.mAlwaysInTapRegion = false;

579 // Log.d(MYTAG, CLASS_NAME +

580 // ":remove LONG_PRESS for idx" + rIdx +

581 // ",mTouchSlopSquare("+mTouchSlopSquare+"), distance("+distance+")");

582 // 清除onSingleTapConform,showPress,longPress三种消息

583 int id = e.getId();

584 mHandler.removeMessages(TAP_SINGLE, id);

585 mHandler.removeMessages(SHOW_PRESS, id);

586 mHandler.removeMessages(LONG_PRESS, id);

587 }

588 if (distance > mBiggerTouchSlopSquare) {//移动距离大于mBiggerTouchSlopSquare,则无法构成双击事件

589 info.mAlwaysInBiggerTapRegion = false;

590 }

591 } else if ((Math.abs(scrollX) >= 1) || (Math.abs(scrollY) >= 1)) {//之前已经移动过了

592 handled = mListener.onScroll(info.mCurrentDownEvent, e, scrollX, scrollY);

593 info.mLastMotionX = x;

594 info.mLastMotionY = y;

595 }

596 }

597 break;

598 case MotionEvent.ACTION_CANCEL:

599 cancel();//清理

600 }

601 return handled;

602 }

603 // 清理所有队列

604 private void cancel() {

605 mHandler.removeMessages(SHOW_PRESS);

606 mHandler.removeMessages(LONG_PRESS);

607 mHandler.removeMessages(TAP_SINGLE);

608 mVelocityTracker.recycle();

609 mVelocityTracker = null;

610 sEventInfos.clear();

611 sEventForDoubleTap.clear();

612 }

613

614}

赞助本站

人工智能实验室

相关热词: 多点手势识别

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港