android - 如何截取具有相机预览和文本视图的布局的屏幕截图?
问题描述
我只想截取相机和地图和地址文本视图的屏幕截图,每当我尝试截取屏幕截图时,它只保存地图和文本区域,但相机区域只显示全白屏这是我的 xml 布局,我想截取id 为 rl_photo 的相对布局,但在截取布局截图后,它只获取地图和文本视图,而不是相机图像,相机图像得到全白屏。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@android:color/black"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Photo"
android:visibility="gone"
android:textStyle="bold"
android:id="@+id/tv_save"
android:background="@android:color/black"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:textColor="#ffffff"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_above="@+id/rl_action"
android:id="@+id/rl_photo"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="120dp"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:layout_height="120dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="gravity"
android:gravity="right"
android:layout_toRightOf="@+id/map"
android:layout_alignParentBottom="true"
android:background="#66000000"
android:layout_margin="10dp"
android:padding="10dp"
android:textColor="@android:color/white"
android:layout_alignParentRight="true"
android:id="@+id/tv_latlong"
android:text="xbdjhgjrejgtrjbtbjr"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/rl_action"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/button_capture"
android:layout_width="50dp"
android:layout_margin="10dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:src="@drawable/record"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="30dp"
android:src="@drawable/flip"
android:id="@+id/img_flip"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_height="30dp"/>
<ImageView
android:layout_width="30dp"
android:src="@drawable/gallery"
android:id="@+id/img_gallery"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:layout_height="30dp"/>
</RelativeLayout>
</RelativeLayout>
解决方案
对于截图
- 在 FrameLayout 的上方添加图像视图
- 在图像视图中捕获 ss 设置相机位图并使 Imageview可见时,完成了一个过程以使图像视图不可见
- 对于 SS,请遵循以下流程
XML 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<TextView
android:id="@+id/tv_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:background="@android:color/black"
android:text="Save Photo"
android:textColor="#ffffff"
android:textStyle="bold"
android:visibility="gone" />
<RelativeLayout
android:id="@+id/rl_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/rl_action">
<ImageView
android:id="@+id/ImageViewOFCamera"
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentBottom="true"
android:layout_margin="10dp" />
<TextView
android:id="@+id/tv_latlong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:layout_toRightOf="@+id/map"
android:background="#66000000"
android:gravity="right"
android:padding="10dp"
android:text="xbdjhgjrejgtrjbtbjr"
android:textAlignment="gravity"
android:textColor="@android:color/white" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<ImageView
android:id="@+id/button_capture"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:src="@drawable/record" />
<ImageView
android:id="@+id/img_flip"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:src="@drawable/flip" />
<ImageView
android:id="@+id/img_gallery"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:src="@drawable/gallery" />
</RelativeLayout>
</RelativeLayout>
用于 java 文件中的 ss 捕获
private Bitmap bitmap;
bitmap = ScreenshotUtil.getInstance().takeScreenshotForView( rl_photo);
public class ScreenshotUtil {
private static ScreenshotUtil mInstance;
private ScreenshotUtil() {
}
public static ScreenshotUtil getInstance() {
if (mInstance == null) {
synchronized (ScreenshotUtil.class) {
if (mInstance == null) {
mInstance = new ScreenshotUtil();
}
}
}
return mInstance;
}
public Bitmap takeScreenshotForView(View view) {
view.measure( MeasureSpec.makeMeasureSpec( view.getWidth(), MeasureSpec.EXACTLY ),
MeasureSpec.makeMeasureSpec( view.getHeight(), MeasureSpec.EXACTLY ) );
view.layout( (int) view.getX(), (int) view.getY(),
(int) view.getX() + view.getMeasuredWidth(),
(int) view.getY() + view.getMeasuredHeight() );
view.setDrawingCacheEnabled( true );
view.buildDrawingCache( true );
Bitmap bitmap = Bitmap.createBitmap( view.getDrawingCache() );
view.setDrawingCacheEnabled( false );
return bitmap;
}
public Bitmap takeScreenshotForScreen(Activity activity) {
return takeScreenshotForView( activity.getWindow().getDecorView().getRootView() );
}
}
推荐阅读
- java - DB2 根据给定输入重新编号表中的值
- postgresql - postgres:为什么 SELECT NULL <> '' 返回 FALSE?
- delphi - 为什么这个赋值会导致编译器错误?
- javascript - 为什么我的 *ngFor 对 ngrx 状态更改做出反应,但我的订阅方法只执行一次
- javascript - 如何在 JavaScript 中禁用当前月份日期和未来日期?
- asp.net-core - 身份服务器中一个客户端的多个实例抛出异常
- kubernetes - 有没有办法在 Rancher UI 中显示 CRD 资源?
- flutter - 在 Flutter 中使用 Text 小部件时,问号出现在文本之前而不是文本之后
- android-studio - 如何在 Android Studio 中更改 Flutter 存储库 URL
- php - 如何在 XPath 注释后获取下一个 HTML 元素?