简体   繁体   English

截取特定布局的屏幕截图

[英]Taking screenshot of a particular layout

I am trying to take screenshot of a particular layout but the problem is,the screenshot is coming with the actionbar and also has a blue patch above it although at time of taking screenshot I hide the action bar but still it persists but it is working fine for fab button. 我正在尝试截取特定布局的屏幕截图,但问题是,该屏幕截图随操作栏一起提供,并且上方有一个蓝色补丁,尽管在截屏时我隐藏了操作栏,但它仍然存在,但可以正常工作用于fab按钮。

I am mentioning the code below- 我在下面提到的代码-

                    try {

                        actionBar.hide();
                        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + "Hi" + ".jpg";
                        fabSpeedDial.setVisibility(View.INVISIBLE);
                        // create bitmap screen capture
                        View v1 = getWindow().getDecorView().getRootView();
                        v1.setDrawingCacheEnabled(true);
                        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
                        v1.setDrawingCacheEnabled(false);

                        File imageFile = new File(mPath);

                        FileOutputStream outputStream = new FileOutputStream(imageFile);
                        int quality = 100;
                        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
                        outputStream.flush();
                        outputStream.close();
                        fabSpeedDial.setVisibility(View.VISIBLE);
                         actionBar.show();
                         return true;


                    } catch (Throwable e) {

                        e.printStackTrace();
                    }

Xml code- xml代码-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.ragnar.useless.MainActivity"

android:background="#ff4343"
android:id="@+id/l1">

<ImageView
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_width="180dp"
    android:id="@+id/img"
    android:layout_height="180dp"
    android:src="@drawable/panda"/>
<io.github.yavski.fabspeeddial.FabSpeedDial
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom|end"
app:fabGravity="bottom_end"
app:fabMenu="@menu/main_menu"
android:id="@+id/fabspeed"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
app:miniFabDrawableTint="?attr/colorPrimaryDark"
app:miniFabTitleTextColor="?attr/colorPrimaryDark"
>
</io.github.yavski.fabspeeddial.FabSpeedDial>


</RelativeLayout>

What I want is the screenshot of Relativelayout without getting the actionbar and the above bluepatch. 我想要的是Relativelayout的屏幕截图,但没有获取操作栏和上面的bluepatch。 ANy suggestions will be appreciated and also I have seen a similar question on stackoverflow but couldn't understand its answers so please explain what you are providing .THank you in advance. 可能会收到任何建议,并且我也曾在stackoverflow上看到过类似的问题,但无法理解其答案,因此请提前解释一下您所提供的。谢谢。

This is the problem: 这就是问题:

View v1 = getWindow().getDecorView().getRootView();

You are taking the screenshot of the root view. 您正在获取根视图的屏幕截图。 You have to place an ID on your RelativeLayout, which is the root of your activity layout, like this: 您必须在您的RelativeLayout上放置一个ID,这是您的活动布局的根,如下所示:

android:id="@+id/myview"

And then replace the first line I mentioned with: 然后将我提到的第一行替换为:

View v1 = findViewById(R.id.myview);

You will get the bitmap for the layout, without the actionbar and the above blue bar (which is the background of the status bar). 您将获得布局的位图,其中没有操作栏和上方的蓝色栏(这是状态栏的背景)。 Let me know if it works for you! 请让我知道这对你有没有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM