温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

onTouchEvent事件中调用onFling方法

发布时间:2020-06-22 11:31:35 来源:网络 阅读:563 作者:truesea 栏目:开发技术

日志表现出: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.


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI