您好,登錄后才能下訂單哦!
日志表現出:startFling()方法先后在onFling()和onTouch()調用。按照邏輯來說,在onFling()中調用了,即使在onTouch()方法中調用也不會再執行。
11-01 16:09:55.147: D/WatchTvFragment(1026): ++programTouchListener.onTouch++ 11-01 16:09:55.147: D/WatchTvFragment(1026): ++onFling++ 11-01 16:09:55.147: D/WatchTvFragment(1026): velocityX:139.54526, velocityY:225.4602 11-01 16:09:55.147: D/WatchTvFragment(1026): onFling() set bFlinging to true 11-01 16:09:55.147: D/WatchTvFragment(1026): onFling() invode startFling() 11-01 16:09:55.147: D/WatchTvFragment(1026): ++startFling++ 11-01 16:09:55.147: D/WatchTvFragment(1026): event.getAction():1 11-01 16:09:55.147: D/WatchTvFragment(1026): onTouch() set bFlinging to true 11-01 16:09:55.147: D/WatchTvFragment(1026): onTouch() invode startFling() 11-01 16:09:55.147: D/WatchTvFragment(1026): ++startFling++ 11-01 16:09:55.197: D/WatchTvFragment(1026): run() set bFlinging to false 11-01 16:09:55.247: D/WatchTvFragment(1026): run() set bFlinging to false
經過處理,startFling()方法不會既執行onFling()方法的調用又去執行onTouch()方法的調用。
在onTouch()方法中的相關代碼片段如下
case MotionEvent.ACTION_UP: Logger.i(TAG, "event.getAction():" + MotionEvent.ACTION_UP); programWidth = programListFragment.getView().getMeasuredWidth(); float velocityX = (programParams.leftMargin < programWidth / 2) ? -1000 : 1000; Logger.i(TAG, "onTouch() invode startFling()"); startFling(velocityX); break;
在onFling()方法中的相關代碼片段如下
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Logger.i(TAG, "++onFling++"); Logger.i(TAG, "velocityX:" + velocityX + ", velocityY:" + velocityY); Logger.i(TAG, "onFling() invode startFling()"); startFling(velocityX); return true; // 需要返回true }
方法startFling()的代碼片段如下
boolean bFlinging = false; void stopFling() { bFlinging = false; } synchronized void startFling(final float velocityX) { Logger.i(TAG, "++startFling++"); if (bFlinging) { Logger.i(TAG, "invalid operation."); return; } Logger.i(TAG, "flinging..."); bFlinging = true; programWidth = programListFragment.getView().getMeasuredWidth(); new Thread() { public void run() { int interval = 50; float xDistFling; int newLeftMargin; float decay = 0.98f; float v; // 速度從(+/-)1000px/s開始。 if (velocityX > 0 && velocityX < 1000) v = 1000; else if (velocityX < 0 && velocityX > -1000) v = -1000; else v = velocityX; // running表示while循環什么時候結束 boolean running = true; while (running) { v *= decay; xDistFling = v / 1000 * interval; newLeftMargin = programParams.leftMargin + (int) xDistFling; if (newLeftMargin <= 0) { newLeftMargin = 0; running = false; } else if (newLeftMargin >= channelWidth) { newLeftMargin = channelWidth; running = false; } programParams.leftMargin = newLeftMargin; try { Thread.sleep(interval); } catch (InterruptedException e) { e.printStackTrace(); } handler.sendEmptyMessage(0); } Logger.i(TAG, "flinging finished."); } }.start(); }
打印出來的日志表現是符合邏輯的,日志如下。
(1)onFling()中執行startFling()方法,在onTouch()中再執行被視為無效操作。
11-01 16:30:29.427: D/WatchTvFragment(2654): ++programTouchListener.onTouch++ 11-01 16:30:29.427: D/WatchTvFragment(2654): ++onFling++ 11-01 16:30:29.427: D/WatchTvFragment(2654): velocityX:-348.25256, velocityY:215.86748 11-01 16:30:29.427: D/WatchTvFragment(2654): onFling() invode startFling() 11-01 16:30:29.427: D/WatchTvFragment(2654): ++startFling++ 11-01 16:30:29.427: D/WatchTvFragment(2654): flinging... 11-01 16:30:29.427: D/WatchTvFragment(2654): event.getAction():1 11-01 16:30:29.427: D/WatchTvFragment(2654): onTouch() invode startFling() 11-01 16:30:29.427: D/WatchTvFragment(2654): ++startFling++ 11-01 16:30:29.427: D/WatchTvFragment(2654): invalid operation. 11-01 16:30:29.987: D/WatchTvFragment(2654): flinging finished.
(2)onFling()中沒有執行startFling()方法,在onTouch()中可以執行。
11-01 17:30:52.257: D/WatchTvFragment(8850): ++programTouchListener.onTouch++ 11-01 17:30:52.257: D/WatchTvFragment(8850): event.getAction():1 11-01 17:30:52.257: D/WatchTvFragment(8850): onTouch() invode startFling() 11-01 17:30:52.257: D/WatchTvFragment(8850): ++startFling++ 11-01 17:30:52.257: D/WatchTvFragment(8850): flinging... 11-01 17:30:52.307: D/WatchTvFragment(8850): flinging finished.
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。