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

5
我试图将我的 ScrollView 转换为 bitmap,我的 ScrollView 的内容超出了屏幕(因此可以滚动),在 这里询问 后,我可以捕获 ScrollView 中的 Activity,但是我遇到了一个问题,用于保存屏幕截图的 Bitmap 只创建了最后一个 ScrollView 的视图,其余部分都是黑屏,如下所示: enter image description here 以下是我用于获取 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;
    }

那么,我的代码有什么问题,所以我无法按照想要的方式显示位图吗?

1个回答

4
你需要获取ScrollView的第一个子元素,并将其转换为位图。
示例:
<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>

你应该尝试在屏幕加载和布局绘制时才加载位图,不要从Activity的onCreate方法中加载。

当屏幕加载和布局绘制时,加载位图是什么意思?我只想问一下在ScrollView中如何获取第一个子项。 - Salman Khan
谢谢,我为此苦苦挣扎了一个小时。我没有意识到应该取scrollview的子元素。 - Amir Dora.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接