本文介绍了拍摄ScrollView Android的完整屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的确,整个SO中都有解决方案和问题已得到回答.所以,这是到目前为止我尝试过的!

Indeed, there are solutions and questions has been answered all over the SO. So, here is what I have tried so far!

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

解决方案,到目前为止,我已经尝试过!

Solution, I have tried so far!

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();
}
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;
}
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;
}

在上述所有方法中,我都有黑色"背景屏幕截图.除了这些以外,还有其他解决方案吗?或者在截取屏幕截图时我犯了任何错误?

In all the above method, I am having "Black" background screenshot. Apart from these do anyone have other solutions or am I making any mistakes in taking Screenshot?

推荐答案

我希望这对您有用..源此处

I hope this is work for you.. source here

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;
}

这篇关于拍摄ScrollView Android的完整屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 21:39