将 ScrollView 中的所有内容转换为位图?

Men*_*nma 5 java android screenshot uiscrollview

我尝试将我的内容转换ScrollViewbitmap,我的ScrollView内容溢出屏幕(因此可以滚动),在我在这里询问之后我可以在 中捕获我的活动ScrollView,但是我遇到了一个问题,Bitmap用于保存屏幕截图的不是创建所有查看,只有最后一个ScrollView,其余的都是黑屏,如下图: 在此处输入图片说明

这是我为以下所有视图截取屏幕截图的代码ScrollView

 scroll_pp=(ScrollView)polis.findViewById(R.id.scroll_pp);
   utktest=polis.findViewById(R.id.scroll_pp);             
   int totalHeight = scroll_pp.getChildAt(0).getHeight();
   int totalWidth = scroll_pp.getChildAt(0).getWidth();
   Bitmap b= MethodSupport.loadBitmapFromView(utktest, totalWidth, totalHeight);

 String extr = Environment.getExternalStorageDirectory().toString() +   File.separator + "Folder";
                    String fileName = "Test.jpg";
                    File myPath = new File(extr, fileName);
                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(myPath);
                        b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                        fos.flush();
                        fos.close();
                        MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), b, "Screen", "screen");
                    }catch (FileNotFoundException e) {

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

                        e.printStackTrace();
                    }
public static Bitmap loadBitmapFromView(View v, int width, int height) {
        Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);                
        Canvas c = new Canvas(b);
        v.draw(c);
        return b;
    }
Run Code Online (Sandbox Code Playgroud)

那么我的代码有问题吗,所以我无法bitmap根据我想要的显示输入?

Meh*_*sar 4

你需要get First children of ScrollViewconvert it to Bitmap.

例子:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

    <RelativeLayout
        android:id="@+id/rlChildContainer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

    </RelativeLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

您应该尝试仅在加载屏幕并绘制布局时加载位图,不要在 Activity 的 onCreate 方法中执行此操作。