【问题标题】:How to take screenshot of a layout having a camera preview and text view?如何截取具有相机预览和文本视图的布局的屏幕截图?
【发布时间】:2021-07-12 11:29:09
【问题描述】:

i want to take screenshot of camera and map and address text view only, whenever i try to take screenshot its only saving map and text area but the camera area gets all white screen only这是我的 xml 布局,我想用 id rl_photo 截取相对布局的屏幕截图,但是在截取布局后,它只获取地图和 textview 而不是相机图像,相机图像全白屏。

<?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>

【问题讨论】:

    标签: android screenshot surfaceview


    【解决方案1】:

    截图

    1. 在 FrameLayout 上方添加图像视图
    2. 在图像视图中捕获 ss 设置相机位图并使 Imageview 可见 时,完成了一个过程以使图像视图 不可见
    3. 对于 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() );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-05
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多