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

时间:2014-08-29 04:32:19

标签: java android uiscrollview screenshot

我尝试将我的ScrollView转换为bitmap,我的ScrollView内容会溢出屏幕(因此滚动成为可能),在Ask here之后我可以在ScrollView中捕获我的活动,但是我遇到了一个问题,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;
    }

所以我的代码有问题所以我无法根据自己的需要显示bitmap吗?

1 个答案:

答案 0 :(得分:2)

您需要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>

只有在加载屏幕并绘制布局时才应尝试加载位图,不要从活动的onCreate方法执行此操作。