gpt4 book ai didi

android - 使用 AccessibilityService 在屏幕上执行滑动

转载 作者:行者123 更新时间:2023-12-03 06:03:02 56 4
gpt4 key购买 nike

我想使用辅助功能服务在屏幕上执行滑动操作。我尝试过这个,但这只执行一次触摸。我知道这是可能的,因为当在设备上启用我的服务时,它说该服务可以执行滑动、触摸、捏合等操作。

Point position=new Point(100,10);
GestureDescription.Builder builder = new GestureDescription.Builder();
Path p = new Path();
p.moveTo(position.x, position.y);
builder.addStroke(new GestureDescription.StrokeDescription(p, 100L, 50L));
GestureDescription gesture = builder.build();
boolean isDispatched = dispatchGesture(gesture,gestureResultCallback,null);

最佳答案

我认为您有多个问题。你构建手势的方式有点偏差,并且你必须移动的像素数才能成为滑动,比你想象的要大!我会根据屏幕尺寸而不是特定的像素数来计算。我认为典型的滑动手势大约是屏幕的一半,从一侧到另一侧,就在中间高度。

我设置了一个愚蠢的小“onAccessibilityEvent”监听器,它在我的 Nexus 6 上在主屏幕 1 和主屏幕 2 之间来回弹跳。显然,您必须设置两个主屏幕才能看到它的运行情况。

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {

switch (event.getEventType()) {
case AccessibilityEvent.TYPE_ANNOUNCEMENT:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {

DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

int middleYValue = displayMetrics.heightPixels / 2;
final int leftSideOfScreen = displayMetrics.widthPixels / 4;
final int rightSizeOfScreen = leftSideOfScreen * 3;
GestureDescription.Builder gestureBuilder = new GestureDescription.Builder();
Path path = new Path();

if (event.getText() != null && event.getText().toString().contains("1")) {
//Swipe left
path.moveTo(rightSizeOfScreen, middleYValue);
path.lineTo(leftSideOfScreen, middleYValue);
} else {
//Swipe right
path.moveTo(leftSideOfScreen, middleYValue);
path.lineTo(rightSizeOfScreen, middleYValue);
}

gestureBuilder.addStroke(new GestureDescription.StrokeDescription(path, 100, 50));
dispatchGesture(gestureBuilder.build(), new GestureResultCallback() {
@Override
public void onCompleted(GestureDescription gestureDescription) {
Log.w("Gesture Completed");
super.onCompleted(gestureDescription);
}
}, null);
}

default: {
break;
}
}
}

同样重要的是辅助功能配置信息,请查看我的配置 xml 文件

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/accessibility_service_description"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagReportViewIds"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:settingsActivity="com.moba11y.basicaccessibilityservice.SettingsActivity"
android:canPerformGestures="true"
/>

编辑:

要支持向上或向下滑动,您只需更改路径参数。

final int height = displayMetrics.heightPixels;
final int top = height * .25;
final int mid = height * .5;
final int bottom = height * .75;
final int midX = displayMetrics.widthPixels / 2;

if(swipeUp) {
path.moveTo(midX, bottom);
path.lineTo(midX, top);
} else {
path.moveTo(midX, top);
path.lineTo(midX, bottom);
}

关于android - 使用 AccessibilityService 在屏幕上执行滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44420320/

56 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com