安卓手机如何截长屏,安卓最新手机截长屏的软件

首页 > 经验 > 作者:YD1662022-10-26 22:50:20

点击测试校验代码如下

bool _hitTest(RenderView? root, RenderViewportBase? result) {
if (root == || result == ) {
return false;
}
Size rootSize = size(root, Size.zero);
HitTestResult hitResult = HitTestResult;
root.hitTest(hitResult, position: Offset(rootSize.width/2, rootSize.height/2));
for (HitTestEntry entry in hitResult.path) {
if (entry.target == result) {
return true;
}
}


/**
* 处理如下 case
* RenderPointerListener 2749d135
RenderSemanticsAnnotations 1cd639bf
RenderIgnorePointer 7e33fff
RenderShrinkWrappingViewport 1167ca33
*/
RenderPointerListener? pointerListenerParent;
AbstractNode? parent = result.parent;
const int lookUpLimit = 5;
int lookupCount = 0;
while (parent != &&
lookupCount < lookUpLimit &&
parent.runtimeType.toString != '_RenderTheatre') {
lookupCount ;
if (parent is RenderPointerListener) {
pointerListenerParent = parent;
}
parent = parent.parent;
}
if (pointerListenerParent != ) {
for (HitTestEntry entry in hitResult.path) {
if (entry.target == pointerListenerParent) {
return true;
}
}
}
return false;
}

异步 Channel 通信方案

安卓手机如何截长屏,安卓最新手机截长屏的软件(13)

Flutter channel 通信方案如上图所示,其中 EventChannel 和 MethodChannel 运行在 Java 主线程,同 Dart Platform Isolate,而 Dart 层事件处理逻辑在 UI Isolate,为此并不在同一线程。可以发现,Java → Dart → Java 发生了 2 次线程切换。使用小米 K50 测试性能,从 EventChannel 发送事件 到 MethodChannel 接收返回值,记录耗时。可见,首次 canScrollVertically (由截屏广播触发)需要递归查找滚动组件,耗时为 10-30ms,之后耗时均在 5ms 以内。

08-08 16:15:56.060 11079 11079 E longscreenshot: canScrollVertically use_time=25
08-08 16:15:56.278 11079 11079 E longscreenshot: canScrollVertically use_time=2
08-08 16:16:05.342 11079 11079 E longscreenshot: canScrollVertically use_time=10
08-08 16:16:05.562 11079 11079 E longscreenshot: canScrollVertically use_time=1

为保证在异步调用的情况下,MIUI ContentPort 下发命令均能获取到最新值,这里做以下特殊处理

  1. 1. 截屏广播提前计算 canScrollVerticallly 并缓存结果

  2. 2. MIUI ContentPort 调用 canScrollVerticallly 直接返回最新缓存值,异步触发计算

  3. 3. MIUI ContentPort 调用 scrollBy 后,及时更新 canScrollVerticallly 和 getScrollY 缓存值

同步 FFI 通信方案

异步调用方案,在高端机且 App 任务队列无阻塞情况下,能正确且准确运行,但在低端机和 App 任务较重时,可能存在返回 ContentPort 数据非最新的情况,为此我们考虑使用 FFI 同步通信的方案。

安卓手机如何截长屏,安卓最新手机截长屏的软件(14)

以上同步方案,一次同步调用性能分析,基本在 5ms 以内:

安卓手机如何截长屏,安卓最新手机截长屏的软件(15)

关键实现代码如下:

@Keep
public class NativeLongScreenshotJni implements Serializable {
static {
System.loadLibrary("flutter_longscreenshot");
}

public static native void nativeCanScrollVertically(int direction,
boolean startScreenshot,
int callbackId);
public static native void nativeGetScrollY(int screenWidth, int callbackId);
public static native void nativeScrollBy(int screenWidth, int x, int y);

public static boolean canScrollVertically(final int direction,
final boolean startScreenshot) {
FlutterLongScreenshotCallbacks.AwaitCallback callback =
FlutterLongScreenshotCallbacks.newCallback;
nativeCanScrollVertically(direction, startScreenshot, callback.id);
int result = callback.waitCallback.getResult;
return result == 1;
}

public static int getScrollY(final int screenWidth) {
FlutterLongScreenshotCallbacks.AwaitCallback callback =
FlutterLongScreenshotCallbacks.newCallback;
nativeGetScrollY(screenWidth, callback.id);
// waitCallback 同步等待 C 调用 FlutterLongScreenshotCallbacks.handleDartCall
int result = callback.waitCallback.getResult;
return result;
}

public static void scrollBy(int screenWidth, int x, int y) {
nativeScrollBy(screenWidth, x, y);
}
}


@Keep
public class FlutterLongScreenshotCallbacks implements Serializable {

public static AwaitCallback newCallback {
AwaitCallback callback = new AwaitCallback;
CALLBACKS.put(callback.id, callback);
return callback;
}

// C DART_EXPORT void resultCallback(int callbackId, int result) 反射调用
public static void handleDartCall(int id, int result) {
AwaitCallback callback = CALLBACKS.get(id);
if (callback != ) {
CALLBACKS.remove(id);
callback.release(result);
}
}

private static final SparseArray<AwaitCallback> CALLBACKS = new SparseArray<>;

@Keep
public static class AwaitCallback {
public static final int RESULT_ERR = -1;
private final CountDownLatch mLatch = new CountDownLatch(1);
private int mResult = RESULT_ERR;

public int id {
return hashCode;
}

public AwaitCallback waitCallback {
try {
mLatch.await(100, TimeUnit.MILLISECONDS);
} catch (Throwable e) {
e.printStackTrace;
}
return this;
}

public void release(int result) {
mResult = result;
mLatch.countDown;
}

public int getResult {
return mResult;
}
}
}

void setDartInt(Dart_CObject& dartObj, int value) {
dartObj.type = Dart_CObject_kInt32;
dartObj.value.as_int32 = value;
}

JNIEXPORT void JNICALL
nativeCanScrollVertically(
JNIEnv *env, jclass cls,
jint direction, jboolean startScreenshot, jint callbackId) {
Dart_CObject* dart_args[4];

Dart_CObject dart_arg0;
Dart_CObject dart_arg1;
Dart_CObject dart_arg2;
Dart_CObject dart_arg3;

setDartString(dart_arg0, strdup("canScrollVertically"));
setDartInt(dart_arg1, direction);
setDartBool(dart_arg2, startScreenshot);
setDartLong(dart_arg3, callbackId);

dart_args[0] = &dart_arg0;
dart_args[1] = &dart_arg1;
dart_args[2] = &dart_arg2;
dart_args[3] = &dart_arg3;

Dart_CObject dart_object;
dart_object.type = Dart_CObject_kArray;
dart_object.value.as_array.length = 4;
dart_object.value.as_array.values = dart_args;

Dart_PostCObject_DL(send_port_, &dart_object);
}

// getScrollY 和 scrollBy 实现类似

DART_EXPORT void resultCallback(int callbackId, int result) {
JNIEnv *env = _getEnv;
if (env != ptr) {
auto cls = _findClass(env, jCallbackClassName);
jmethodID handleDartCallMethod = ptr;
if (cls != ptr) {
// 调用 java 代码 FlutterLongScreenshotCallbacks.handleDartCall(int id, int result)
handleDartCallMethod = env->GetStaticMethodID(cls,
"handleDartCall", "(II)V");
}
if (cls != ptr && handleDartCallMethod != ptr) {
env->CallStaticVoidMethod(cls, handleDartCallMethod,
callbackId, result);
} else {
print("resultCallback. find method handleDartCall is ptr");
}
}
}

class NativeLongScreenshot extends Object {
...

late final NativeLongScreenshotLibrary _nativeLibrary;
late final ReceivePort _receivePort;
late final StreamSubscription _subscription;

NativeLongScreenshot {
...
_nativeLibrary = initLibrary;
_receivePort = ReceivePort;

var nativeInited = _nativeLibrary.initializeApi(
ffi.NativeApi.initializeApiDLData
);
assert(nativeInited == 0, 'DART_API_DL_MAJOR_VERSION != 2');
_subscription = _receivePort.listen(_handleNativeMessage);
_nativeLibrary.registerSendPort(_receivePort.sendPort.nativePort);
}

void _handleNativeMessage(dynamic inArgs) {
List<dynamic> args = inArgs;
String method = args[0];

switch (method) {
case 'canScrollVertically': {
int direction = args[1];
bool startScreenshot = args[2];
int callbackId = args[3];

final bool canScroll = canScrollVertically(direction, startScreenshot);
int result = canScroll ? 1 : 0;
_nativeLibrary.resultCallback(callbackId, result);
} break;
case 'getScrollY': {
int nativeScreenWidth = args[1];
int callbackId = args[2];
int result = getScrollY(nativeScreenWidth);
_nativeLibrary.resultCallback(callbackId, result);
} break;
case 'scrollBy': {
int nativeScreenWidth = args[1];
int nativeX = args[2];
int nativeY = args[3];
scrollBy(nativeY, nativeScreenWidth);
} break;
}
}
}
总结

完成国内主要机型适配,现在线上几乎不再有用户反馈 Flutter 页面不支持长截屏。闲鱼 Android 用户已经能用系统长截屏能力,分享自己喜欢的商品、圈子内容,卖家能使用一张图片推广自己的全部商品,买家能帮助家里不会用 App 的老人找商品。面对系统功能适配,业务 App 侧也并不是完全束手无策。通过以下过程便有可能找到解决之道:

安卓手机如何截长屏,安卓最新手机截长屏的软件(16)

上一页12345下一页

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.