如何在Python中使用ADB实现放大缩小?

问题描述 投票:0回答:1

我正在尝试创建一个可以放大和缩小游戏的Python应用程序,但我无法实现它。我使用 MEmu 播放器作为模拟器,但我创建的放大和缩小功能不起作用。我创建的滑动和点击功能可以正常工作,但我不知道如何执行缩放功能。运行代码时,它向右滑动,3秒后向左滑动。

这是我的代码:

def zoom(zoom_in=True, duration=300, distance=100):
    # get screen size
    size_output = subprocess.check_output(["adb", "shell", "wm", "size"]).decode().strip()
    width, height = map(int, size_output.split()[-1].split('x'))
    
    # calculate start and end points
    center_x, center_y = width // 2, height // 2
    half_distance = distance // 2
    
    if zoom_in:
        start_x1, start_y1 = center_x - half_distance, center_y - half_distance
        start_x2, start_y2 = center_x + half_distance, center_y + half_distance
        end_x1, end_y1 = center_x - distance, center_y - distance
        end_x2, end_y2 = center_x + distance, center_y + distance
    else:
        start_x1, start_y1 = center_x - distance, center_y - distance
        start_x2, start_y2 = center_x + distance, center_y + distance
        end_x1, end_y1 = center_x - half_distance, center_y - half_distance
        end_x2, end_y2 = center_x + half_distance, center_y + half_distance
    
    subprocess.run([
        "adb", "shell", 
        f"input swipe {start_x1} {start_y1} {end_x1} {end_y1} {duration} && " 
        f"input swipe {start_x2} {start_y2} {end_x2} {end_y2} {duration}"
    ], shell=True)

    time.sleep(duration / 1000 + 0.1)

这是我的滑动功能:

def swipe_on_screen(start_x, start_y, end_x, end_y, duration=1000):
    try:
        subprocess.run(["adb","shell",f"input swipe {str(start_x)} {str(start_y)} {str(end_x)} {str(end_y)} {str(duration)}"],shell=True)
    except Exception as e:
        print(f"Error swiping on screen: {e}")

有人遇到过这种情况并找到解决办法吗?

python android-emulator adb
1个回答
0
投票

假设您想要通过捏合来放大和缩小地图

#! /usr/bin/env python3

import time
from com.dtmilano.android.viewclient import ViewClient

helper = ViewClient.view_client_helper()
oid = helper.ui_device.find_object(ui_selector='[email protected]:id/map_frame').oid
helper.ui_object.pinch_out(oid=oid, percentage=1000, steps=50)
time.sleep(5)
helper.ui_object.pinch_in(oid=oid, percentage=1000, steps=50)
© www.soinside.com 2019 - 2024. All rights reserved.