android非触摸平台,实现当前显示内容翻页功能

一. MotionEvent.ACTION_SCROLL鼠标滚轮方式

        使用此方式的前提是当前页面支持鼠标滚轮。

        像netflix主页面,只支持上下滚动,左右滚动不支持,他们左右滚动的控件不支持滚动,只支持拖曳,只能使用方式二。

        final MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[1];
        pointerProperties[0] = new MotionEvent.PointerProperties();
        pointerProperties[0].id = 0;
        final MotionEvent.PointerCoords[] coords = new MotionEvent.PointerCoords[1];
        coords[0] = new MotionEvent.PointerCoords();
        coords[0].setAxisValue(MotionEvent.AXIS_VSCROLL, -2);//底部的内容往上滚动
        //coords[0].setAxisValue(MotionEvent.AXIS_VSCROLL, 2);//顶部的内容往下滚动
        //coords[0].setAxisValue(MotionEvent.AXIS_HSCROLL, -2);//右边的内容往左滚动
        //coords[0].setAxisValue(MotionEvent.AXIS_HSCROLL, 2);//左边的内容往右滚动
        coords[0].x = 960;
        coords[0].y = 540;

        new Thread(new Runnable() {
            @Override
            public void run() {
                Instrumentation instrumentation = new Instrumentation();
                final long time = SystemClock.uptimeMillis();
                instrumentation.sendPointerSync(MotionEvent.obtain(time, time, MotionEvent.ACTION_SCROLL, 1,
                        pointerProperties, coords, 0, 0, 1.0f, 1.0f, 0, 0,
                        InputDevice.SOURCE_CLASS_POINTER, 0));
            }
        }).start();

二. 模拟鼠标拖曳事件

MotionEvent.ACTION_DOWN -> MotionEvent.ACTION_MOVE -> MotionEvent.ACTION_UP
        new Thread(new Runnable() {
            public void run() {
                try {
                    Instrumentation instrumentation = new Instrumentation();
                    long downTime = SystemClock.uptimeMillis();
                    //模拟底部的内容往上滚动。x, y为当前的坐标,可以取屏幕中心点位置。
                    instrumentation.sendPointerSync(MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0));
                    instrumentation.sendPointerSync(MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_MOVE, x, y, 0));
                    instrumentation.sendPointerSync(MotionEvent.obtain(downTime, downTime + 20, MotionEvent.ACTION_MOVE, x, y - 100, 0));
                    instrumentation.sendPointerSync(MotionEvent.obtain(downTime, downTime + 30, MotionEvent.ACTION_MOVE, x, y - 200, 0));
                    instrumentation.sendPointerSync(MotionEvent.obtain(downTime, downTime + 40, MotionEvent.ACTION_MOVE, x, 0, 0));//底部y坐标为0,最后移动到此处,上面的down事件才不会响应
                    instrumentation.sendPointerSync(MotionEvent.obtain(downTime, downTime + 60, MotionEvent.ACTION_UP,   x, 0, 0));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值