我需要复制应用程序的屏幕才能在眼镜硬件上使用它。 我只有一个活动,因此从最简单的解决方案开始:
<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);
效果很好,但不适用于根布局中的视频或其他特定视图。
如何解决此问题或其他更好的解决方案?
答案 0 :(得分:0)
找到了解决方案。 将SurfaceView用作目标视图
<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);
}
这完全解决了任务,我们在屏幕上拥有的全部重复。 但是,欢迎对此任务提供更优雅的解决方案。