xml—获取scrollview android的完整屏幕截图

jhiyze9q  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(339)

事实上,世界各地都有解决办法和问题的答案。所以,这是我迄今为止尝试过的!
滚动\u layout.xml

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginEnd="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="8dp"
    tools:layout_constraintLeft_creator="1"
    android:layout_marginBottom="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/txtAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ADDRESS"
            android:textSize="40px"
            android:textColor="@android:color/black"/>
        <EditText
            android:id="@+id/edtName"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Enter Address"
            android:text="lorem emisidjsi damsodks"/>
        <TextView
            android:id="@+id/txtPostal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="POSTAL CODE"
            android:textSize="40px"
            android:layout_marginTop="10dp"
            android:textColor="@android:color/black"/>
        <EditText
            android:id="@+id/edtPostal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Enter Postal Code"
            android:text="123456"/>

        <TextView
            android:id="@+id/txtCity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="CITY / GNR"
            android:textColor="@android:color/black"
            android:textSize="40px" />

        <EditText
            android:id="@+id/edtCity"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Loresm"
            android:hint="Enter City"/>
        <TextView
            android:id="@+id/txtBnr"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="BNR"
            android:textSize="40px"
            android:layout_marginTop="10dp"
            android:textColor="@android:color/black"/>
        <EditText
            android:id="@+id/edtBnr"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Enter BNR"
            android:text="1122"/>
        <TextView
            android:id="@+id/txtSize"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SIZE"
            android:textSize="40px"
            android:layout_marginTop="10dp"
            android:textColor="@android:color/black"/>
        <EditText
            android:id="@+id/edtSize"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="Enter Size"
            android:text="50 x 400 x 200"/>
        <TextView
            android:id="@+id/txtRental"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="RENTAL"
            android:textSize="40px"
            android:layout_marginTop="10dp"
            android:textColor="@android:color/black"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="3"
            android:layout_marginTop="5dp"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:buttonTint="@color/colorPrimary"/>
                <TextView
                    android:id="@+id/txtFull"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Full"
                    android:textSize="40px"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:buttonTint="@color/colorPrimary"/>
                <TextView
                    android:id="@+id/txtPartial"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Partial"
                    android:textSize="40px"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:buttonTint="@color/colorPrimary"/>
                <TextView
                    android:id="@+id/txtRentalNo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="No"
                    android:textSize="40px"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"/>

            </LinearLayout>

        </LinearLayout>

        <TextView
            android:id="@+id/txtBasement"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="BASEMENT"
            android:textSize="40px"
            android:layout_marginTop="10dp"
            android:textColor="@android:color/black"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="3"
            android:layout_marginTop="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:buttonTint="@color/colorPrimary" />
                <TextView
                    android:id="@+id/txtFurnished"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Furnished"
                    android:textSize="40px"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:buttonTint="@color/colorPrimary"/>
                <TextView
                    android:id="@+id/txtUnFurnished"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Un-Furnished"
                    android:textSize="40px"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:buttonTint="@color/colorPrimary"/>
                <TextView
                    android:id="@+id/txtBasementNo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="No"
                    android:textSize="40px"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"/>

            </LinearLayout>
        </LinearLayout>

        <TextView
            android:id="@+id/txtType"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TYPE"
            android:textSize="40px"
            android:layout_marginTop="10dp"
            android:textColor="@android:color/black"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="3"
            android:layout_marginTop="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:buttonTint="@color/colorPrimary"/>
                <TextView
                    android:id="@+id/txtDetached"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Detached"
                    android:textSize="40px"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:buttonTint="@color/colorPrimary"/>
                <TextView
                    android:id="@+id/txtSemiDetached"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Semi-Detached"
                    android:textSize="40px"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:buttonTint="@color/colorPrimary"/>
                <TextView
                    android:id="@+id/txtTownHouse"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Town-House"
                    android:textSize="40px"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"/>

            </LinearLayout>
        </LinearLayout>

        <android.support.v7.widget.AppCompatSpinner
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_marginTop="20dp"
            android:entries="@array/arr_add_buildings">
        </android.support.v7.widget.AppCompatSpinner>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="2"
            android:layout_marginTop="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical"
                android:paddingRight="20dp">
                <TextView
                    android:id="@+id/txtYear"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="YEAR"
                    android:textSize="40px"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"/>
                <EditText
                    android:id="@+id/edtYear"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:hint="Enter Year"
                    android:text="2011"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/txtBldSize"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="SIZE"
                    android:textSize="40px"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"/>
                <EditText
                    android:id="@+id/edtBdlSize"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:hint="Enter Size"
                    android:text="20 x 20"/>
            </LinearLayout>
        </LinearLayout>

        <android.support.v7.widget.AppCompatSpinner
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_marginTop="5dp"
            android:entries="@array/arr_deductible">
        </android.support.v7.widget.AppCompatSpinner>

        <TextView
            android:id="@+id/txtCoverage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="COVERAGE"
            android:textSize="40px"
            android:layout_marginTop="10dp"
            android:textColor="@android:color/black"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="2"
            android:layout_marginTop="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:buttonTint="@color/colorPrimary"/>
                <TextView
                    android:id="@+id/txtNormal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Normal"
                    android:textSize="40px"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:buttonTint="@color/colorPrimary"/>
                <TextView
                    android:id="@+id/txtSuper"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Super"
                    android:textSize="40px"
                    android:layout_marginTop="10dp"
                    android:textColor="@android:color/black"
                    android:textAlignment="center"/>

            </LinearLayout>

        </LinearLayout>

        <TextView
            android:id="@+id/txtComments"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="COMMENTS"
            android:textColor="@android:color/black"
            android:textSize="40px" />

        <EditText
            android:id="@+id/edtComments"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="white walls"
            android:hint="Enter Comments"/>

        <Button
            android:id="@+id/save"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:layout_marginTop="20dp"
            android:text="SAVE"
            android:textSize="50px"
            android:textColor="@android:color/white"
            android:backgroundTint="@color/colorPrimary" />

    </LinearLayout>

</ScrollView>

解决方案,我已经试过了!
方法1:

public Bitmap takeScreenshot() {
    View rootView = getWindow().getDecorView().findViewById(R.id.scrollView); //Instead of ScrollView I have taken Main view to but no luck
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
}

方法2:

public Bitmap takeScreenshot() {
    View rootView = getWindow().getDecorView().findViewById(R.id.constraintLayout); //Here also I have taken ScrollView too.
    Bitmap b = Bitmap.createBitmap(rootView.getWidth() , rootView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    rootView.layout(0, 0, rootView.getLayoutParams().width, rootView.getLayoutParams().height);
    rootView.draw(c);
        return b;
}

方法3:

public Bitmap takeScreenshot() {
    ScrollView iv = (ScrollView) findViewById(R.id.scrollView);
    Bitmap bitmap = Bitmap.createBitmap(
            iv.getChildAt(0).getWidth(),
            iv.getChildAt(0).getHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bitmap);
    iv.getChildAt(0).draw(c);
    return bitmap;
}

在所有上述方法中,我有“黑色”背景截图。除此之外,还有其他解决方案吗?或者我在截图时犯了什么错误吗?

6ioyuze2

6ioyuze21#

我希望这对你有用。。来源于此

Bitmap bitmap = getBitmapFromView(scrollview, scrollview.getChildAt(0).getHeight(), scrollview.getChildAt(0).getWidth());

//create bitmap from the ScrollView 
private Bitmap getBitmapFromView(View view, int height, int width) {
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null)
        bgDrawable.draw(canvas);
    else
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);
    return bitmap;
}

相关问题