简体   繁体   中英

How to convert scrollview to bitmap?

  • If I want to print a ScrollView, how do I do it?
  • My temporary solution was to convert ScrollView to bitmaps and then split the bitmaps into several small images to draw on the Canvas, but they were often interrupted.
  • Is there a more effective solution? The previous example I used the view.draw() method.
<ScrollView 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"
    android:layout_alignParentBottom="true"
    android:fillViewport="true"
    android:fitsSystemWindows="true"
    android:orientation="vertical">
        
            

    <EditText
     android:id="@+id/txt_content"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:autoLink="web|email"
     android:ems="10"
     android:autoSizeTextType="uniform"
     android:gravity="top|start"
     android:inputType="textMultiLine"
     android:padding="6dp"
     android:textSize="17sp"
     app:layout_constraintTop_toTopOf="parent"
     tools:layout_editor_absoluteX="1dp" /> 
        
</ScrollView>


You can get the bitmap per ScrollView child.

for (int i=0; i<scrollView.getChildCount(); i++) {
    View v = scrollView.getChildAt(i);
    Bitmap b = loadBitmapFromview(v);
    //save bitmap or so
}

public static Bitmap loadBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
    Canvas c = new Canvas(b);
    v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    v.draw(c);
    return b;
}

Note that loadBitmapFromView(View v) and then saving the bitmap can be a time consuming process. So consider Threading in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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