Android - UIAutomator如何在拖动时保持动量?
创始人
2024-08-12 08:00:52
0

在Android的UIAutomator中,可以使用GestureDescription类来实现拖动操作,并通过setStrokeDescription方法设置速度来模拟保持动量。

以下是一个示例代码,演示如何在拖动时保持动量:

import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.os.SystemClock;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.Direction;
import androidx.test.uiautomator.GestureDescription;
import androidx.test.uiautomator.UiAutomatorTestCase;
import androidx.test.uiautomator.UiDevice;

import java.util.ArrayList;
import java.util.List;

public class DragWithMomentum extends UiAutomatorTestCase {

    public void testDragWithMomentum() throws UiObjectNotFoundException {
        // 获取UiDevice实例
        UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        // 找到拖动的起始点和终点的坐标
        UiObject2 startObject = device.findObject(By.text("Start"));
        UiObject2 endObject = device.findObject(By.text("End"));
        Point startPoint = startObject.getVisibleCenter();
        Point endPoint = endObject.getVisibleCenter();

        // 创建一个Path对象,用于描述拖动的路径
        Path path = new Path();
        path.moveTo(startPoint.x, startPoint.y);
        path.lineTo(endPoint.x, endPoint.y);

        // 创建一个GestureDescription.Builder对象
        GestureDescription.Builder builder = new GestureDescription.Builder();
        builder.addStroke(new GestureDescription.StrokeDescription(path, 0, 500));

        // 设置拖动的速度,以模拟保持动量
        builder.setStrokeDescription(new GestureDescription.StrokeDescription(path, 0, 500)
                .setStrokeSlowDown(0.8f).setStrokeDuration(2000));

        // 创建GestureDescription对象
        GestureDescription gestureDescription = builder.build();

        // 执行拖动操作
        assertTrue(device.executeGesture(gestureDescription));
    }
}

在示例代码中,首先通过findObject方法找到拖动的起始点和终点的坐标。然后,创建一个Path对象来描述拖动的路径,并使用moveTolineTo方法设置起始点和终点。接下来,创建一个GestureDescription.Builder对象,并使用addStroke方法添加拖动路径。最后,使用setStrokeDescription方法设置拖动的速度,以模拟保持动量。最后,通过executeGesture方法执行拖动操作。

请注意,示例代码使用的是AndroidX库,如果您使用的是旧版的Support库,请相应地更改导入语句。

相关内容

热门资讯

避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
安装了Anaconda之后找不... 在安装Anaconda后,如果找不到Jupyter Notebook,可以尝试以下解决方法:检查环境...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...