尝试在Android中以编程方式模拟滑动 [英] Trying to simulate swipe programmatically in android

查看:100
本文介绍了尝试在Android中以编程方式模拟滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击按钮时,我希望我的应用模拟一次轻扫触摸事件(向上/向下/向左/向右),然后TextView将向下/向上滚动.

I want my app to simulate a swipe touch event (to up/down/left/right) when I click a button, then a TextView will scroll down/up.

我曾尝试使用运动事件,但在分别调度ACTION_DOWN,ACTION_MOVE和ACTION_UP的3个运动事件后,没有任何反应.

I have tried to use Motion Event, but nothing happen after I dispatch 3 Motion Event of ACTION_DOWN, ACTION_MOVE and ACTION_UP respectively.

可以模拟滑动事件吗?

public void simulation(View view){
    swipe(Direction.Bot);
}
public enum Direction {
    Top, Bot, Left, Right;
}
protected void swipe(Direction direction) {
    Point size = new Point();
    this.getWindowManager().getDefaultDisplay().getSize(size);
    int height = size.y; // height will be at top of the screen
    int width = size.x; // width will be rightmost location of the screen
    float xStart = size.x-50;
    float xEnd = size.x-50;
    float yStart = size.y-50;
    float yEnd = size.y-50;
    long downTime = SystemClock.uptimeMillis();

    if(direction == Direction.Top || direction == Direction.Bot){
        yStart = ((direction == Direction.Top) ? 50 : (height - 50));
        yEnd = ((direction == Direction.Top) ? (height - 50) : 50);
    }else {
        xStart = ((direction == Direction.Left) ? (width - 50) : 50); // true: xStart = w-10; else: = 10
        xEnd = ((direction == Direction.Left) ? 50 : (width - 50)); // true: xEnd = 10; else: = w-10
    }
    findViewById(R.id.my_id).dispatchTouchEvent(MotionEvent.obtain(downTime, SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN, xStart/2, yStart/2, 0));
    System.out.println("ACTION_DOWN");
    findViewById(R.id.my_id).dispatchTouchEvent(MotionEvent.obtain(downTime, SystemClock.uptimeMillis() + 500, MotionEvent.ACTION_MOVE, xEnd / 2, yEnd / 2, 0));
    System.out.println("ACTION_MOVE");
    findViewById(R.id.my_id).dispatchTouchEvent(MotionEvent.obtain(downTime, SystemClock.uptimeMillis() + 1000, MotionEvent.ACTION_UP, xEnd / 2, yEnd / 2, 0));
    System.out.println("ACTION_UP");

}

推荐答案

我能够以编程方式在滚动活动演示中模拟fling事件.

这是我尝试的模拟逃避事件的示例,并且有效.

This is an example of an emulating fling event I was trying and it worked.

蓝色虚线是我所模拟的猛击事件:

Blue dotted line is the fling event I have emulated:

 class ScrollingActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_scrolling)
    setSupportActionBar(toolbar)
    fab.setOnClickListener { view ->
        Thread(Runnable {
            try {

                fling(500f ,900f ,530f ,20f, 5);
               // emulateMptionEvent()

            } catch (e: Exception) {
            }
        }).start()
    }
}

/** * Simulate touching a specific location and dragging to a new location.
 *
 * @param fromX X coordinate of the initial touch, in screen coordinates
 * @param toX Xcoordinate of the drag destination, in screen coordinates
 * @param fromY X coordinate of the initial touch, in screen coordinates
 * @param toY Y coordinate of the drag destination, in screen coordinates
 * @param stepCount How many move steps to include in the drag
 */
fun fling(
    fromX: Float, toX: Float, fromY: Float,
    toY: Float, stepCount: Int
) {

    val inst = Instrumentation()

    val downTime = SystemClock.uptimeMillis()
    var eventTime = SystemClock.uptimeMillis()

    var y = fromY
    var x = fromX

    val yStep = (toY - fromY) / stepCount
    val xStep = (toX - fromX) / stepCount

    var event = MotionEvent.obtain(
        downTime, eventTime,
        MotionEvent.ACTION_DOWN, fromX, fromY, 0
    )
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
        event.source = InputDevice.SOURCE_TOUCHSCREEN
    }
    inst.sendPointerSync(event)



    for (i in 0 until stepCount) {
        y += yStep
        x += xStep
        eventTime = SystemClock.uptimeMillis()
        event = MotionEvent.obtain(
            downTime, eventTime + stepCount,
            MotionEvent.ACTION_MOVE, x, y, 0
        )
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
            event.source = InputDevice.SOURCE_TOUCHSCREEN
        }
        inst.sendPointerSync(event)
    }

    eventTime = SystemClock.uptimeMillis() + stepCount.toLong() + 2
    event = MotionEvent.obtain(
        downTime, eventTime,
        MotionEvent.ACTION_UP, toX, toY, 0
    )

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
        event.source = InputDevice.SOURCE_TOUCHSCREEN
    }
    inst.sendPointerSync(event)
}
}

希望对别人有帮助

这篇关于尝试在Android中以编程方式模拟滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆