First option
The uiautomator by xiaocong. This library specifically supports two point gestures - see examples taken from the GitHub repo:
d(text="Settings").gesture((sx1, sy1), (sx2, sy2)) \
.to((ex1, ey1), (ex2, ey2))
Moving from one point to another with two points.
Most importantly there is a specific pinch.in()
and pinch.out()
function. See:
d(text="Settings").pinch.In(percent=100, steps=10)
d(text="Settings").pinch.Out()
I can highly recommend this python wrapper due to it's detailed documentation as well as the functionality.
First off, install the uiautomator library
:
pip install uiautomator
Little code snippet which I used for testing (this only works for Android 4.3 and on-going; "pinch can not be set until Android 4.3"):
from uiautomator import device as d
#Replace the variables, marked by the percentage signs
d = Device('%serial_number%', adb_server_host='%server%', adb_server_port=%port%)
#[...]
#Zooming in - arbitrary example values for a "small zoom" in; from the edges to the center
d(text="Settings").pinch.In(percent=30, steps=10)
At this point I want to mention a note from the GitHub repo regarding %server%
:
Although adb supports -a
option since SDK 4.3, but now it has a bug on it. The only way to start adb server listenning on all interfaces instead of localhost, is adb -a -P 5037 fork-server server &
For further reading on this subject, I highly suggest a question from the Android Stackexchange (https://android.stackexchange.com/questions/52911/adb-start-server-and-listen-on-all-interfaces).
Second option
Doing my research I found another StackOverflow question which is highly related to your post: Fire a pinch in/out command to Android phone using adb - Even though this thread isn't specifically for Python, I'd highly suggest taking a look at it. User @Saurabh Meshram described in a detailed manner how to use getevent
and sendevent
in order to achieve a pinch in/out motion via adb. (Further sources: Check out this blogpost as well)
Though if I understood your initial question correct, you already tried this approach:
I have tried recording events
Still for the sake of completeness the second option should be mentioned. Anyhow, on to the third option:
Third option
Here comes a "tricky" solution. The python_api of the android_touch repo doesn't specifically support a pinch and zoom action per se. Though with the TouchActionBuilder
you're allowed to construct complex actions as a pinch and zoom.
Depending on the motion you want to have you can combine either the down()
or multidown()
option with move()
and multimove()
. To achieve this I'd highly suggest taking a look at the code as well as the example of a "complex action".
Further reading and sources