gpt4 book ai didi

java - 复制屏幕以在眼镜中使用。安卓

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

我需要复制应用程序的屏幕才能在眼镜硬件上使用它。我只有一项 Activity ,因此从最简单的解决方案开始:

<LinearLayout 
android:orientation="horizontal">

<FrameLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>

<FrameLayout
android:id="@+id/copy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />

</LinearLayout>

以及重复复制功能:

Bitmap b = Bitmap.createBitmap(root.getWidth(),
root.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
root.draw(c);
BitmapDrawable d = new BitmapDrawable(r, b);
copy.setBackground(d);

效果很好,但不适用于根布局内的视频或其他一些特定 View 。

如何解决这个问题,或者其他更好的解决方案?

最佳答案

找到了解决方案。使用SurfaceView作为目标 View

<SurfaceView
android:id="@+id/copy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />

并使用 MediaProjection:

private MediaProjectionManager mediaProjectionManager;
private MediaProjection mediaProjection;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) activity.getSystemService(
android.content.Context.MEDIA_PROJECTION_SERVICE);
Intent permissionIntent = mediaProjectionManager.createScreenCaptureIntent();
activity.startActivityForResult(permissionIntent, MEDIA_PROJECTION_REQUEST_CODE);
}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (MEDIA_PROJECTION_REQUEST_CODE == requestCode) {
if (resultCode == RESULT_OK) {
mediaProjectionManager = (MediaProjectionManager) getSystemService(
android.content.Context.MEDIA_PROJECTION_SERVICE);
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, intent);
startRecording(mediaProjection);
}
}
}

public void startRecording(MediaProjection mediaProjection) {
DisplayManager dm = (DisplayManager) activity.getSystemService(Context.DISPLAY_SERVICE);
Display defaultDisplay = null;
if (dm != null) {
defaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
}
if (defaultDisplay == null) {
return;
}

DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int screenDensity = metrics.densityDpi;

Surface inputSurface = copy.getHolder().getSurface();

mediaProjection.createVirtualDisplay("Recording Display", screenWidth,
screenHeight, screenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, inputSurface,
null, null);
}

这完全解决了任务,我们屏幕上的所有内容都是重复的。但欢迎更优雅的任务解决方案。

关于java - 复制屏幕以在眼镜中使用。安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55834252/

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