从相机拍摄屏幕快照不会显示全屏android

时间:2018-07-17 09:44:28

标签: android android-layout

我的活动布局是这样的:-

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RecordVideo">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/camview"
    android:layout_height="fill_parent">

    <FrameLayout
        android:id="@+id/camera_preview"
        android:adjustViewBounds="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <SurfaceView
        android:id="@+id/sview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <ImageView
        android:id="@+id/ImgLogo1"
        android:layout_width="80dp"
        android:src="@drawable/rotating_earth"
        android:layout_alignParentLeft="true"
        android:layout_height="80dp" />

    <ImageView
        android:id="@+id/ImgLogo2"
        android:layout_width="80dp"
        android:src="@mipmap/ic_launcher"
        android:layout_alignParentRight="true"
        android:layout_height="80dp" />

    <ImageButton
        android:id="@+id/button_capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="?android:attr/selectableItemBackground"
        android:src="@mipmap/img_record_green"
        android:layout_gravity="center" />

</RelativeLayout>

我正在视频捕获期间拍摄屏幕快照,并在这样的帧布局中设置jpeg图像:-

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap bMap = BitmapFactory.decodeFile(tmpFile.toString(), options);
Bitmap BbMap = Bitmap.createScaledBitmap(bMap, size.x, size.y,true);

preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.setBackground(new BitmapDrawable(BbMap));

代码正在捕获屏幕快照,但是ImgLogo1(右侧)和ImgLogo2(左侧)的垂直空间为黑色。我想将相机的屏幕截图以及ImgLogo1和ImgLogo2捕获为全屏截图。 谁能告诉我如何实现这一目标。

第一个屏幕截图是: enter image description here

第二个屏幕截图是:- enter image description here

第一个屏幕截图应与第二个屏幕截图一样宽,但是这2个imageview不允许其扩展。

1 个答案:

答案 0 :(得分:1)

这将有助于捕获整个屏幕的屏幕截图

   try {
                getWindow().getDecorView().findViewById(android.R.id.content).setDrawingCacheEnabled(true);
                Bitmap backBitmap = getWindow().getDecorView().findViewById(android.R.id.content).getDrawingCache();
                Bitmap bmOverlay = Bitmap.createBitmap(
                        backBitmap.getWidth(), backBitmap.getHeight(),
                        backBitmap.getConfig());
                Canvas canvas = new Canvas(bmOverlay);
                canvas.drawBitmap(backBitmap, 0, 0, null);
                try {
                    FileOutputStream fos = new FileOutputStream(new File(Environment
                            .getExternalStorageDirectory().toString(), "SCREEN"
                            + System.currentTimeMillis() + ".png"));


                    // Write the string to the file
                    bmOverlay.compress(Bitmap.CompressFormat.JPEG, 90, fos);
                    fos.flush();
                    fos.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    Log.d("ImageCapture", "FileNotFoundException");
                    Log.d("ImageCapture", e.getMessage());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.d("ImageCapture", "IOException");
                    Log.d("ImageCapture", e.getMessage());
                }


            } catch (Exception e) {
                e.printStackTrace();
            }