Simulate onFling() programmatically instead of detecting it (Android)
Asked Answered
N

2

1

This is my first post on StackOverflow! I have a background service running and I was wondering if I could actually simulate a horizontal fling touch screen gesture instead of just detecting that it was been called.

I can figure out how to capture this event, but I want to actually simulate a touch fling horizontal gesture instead of waiting for one.

Thanks beforehand!

Naif answered 29/4, 2011 at 19:46 Comment(0)
F
3

There is an API for simulating touch events. Some people report limited success simulating a fling using the TouchUtil API.

Frazier answered 2/5, 2011 at 17:51 Comment(1)
TouchUtil API is meant for testing purposes. It has no guaranteed success in a real-world implementation of the app.Cacoepy
L
1

Simulating Fling event:

/**
 * 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)
    }

Usage

 fab.setOnClickListener { view ->
            Thread(Runnable {
                try {

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

                } catch (e: Exception) {
                }
            }).start()
        }
Lajoie answered 30/10, 2019 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.