Android: Taking a ScreenShot for all view in Scrol

2019-08-12 18:20发布

问题:

I try to take a ScreenShot for all view in ScrollView, my ScrollView has content that spills out of the screen (hence scrolling made possible), how can I ensure that the screenshot includes the elements that are not visible on the screen?

I have been using this code:

scroll_pp=(ScrollView)polis.findViewById(R.id.scroll_pp);
int totalHeight = scroll_pp.getChildAt(0).getHeight();
int totalWidth = scroll_pp.getChildAt(0).getWidth();
 Bitmap b= MethodSupport.loadBitmapFromView(scroll_pp, 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) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        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.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
        v.draw(c);
        return b;
    }

The Image file created, but it only display the black screen, not the view as I wanted, so Is there something wrong with my code? Is there anyone can help me, please?

UPDATE

After follow @113408 's Answer, I got Error, this is my logcat :

 08-27 10:40:57.555: E/AndroidRuntime(4957): FATAL EXCEPTION: main
08-27 10:40:57.555: E/AndroidRuntime(4957): java.lang.NullPointerException
08-27 10:40:57.555: E/AndroidRuntime(4957):     at android.graphics.Bitmap.createBitmap(Bitmap.java:484)
08-27 10:40:57.555: E/AndroidRuntime(4957):     at id.co.ajsmsig.espaj.PemegangPolis.onClick(PemegangPolis.java:1546)
08-27 10:40:57.555: E/AndroidRuntime(4957):     at android.view.View.performClick(View.java:4222)
08-27 10:40:57.555: E/AndroidRuntime(4957):     at android.view.View$PerformClick.run(View.java:17337)
08-27 10:40:57.555: E/AndroidRuntime(4957):     at android.os.Handler.handleCallback(Handler.java:615)
08-27 10:40:57.555: E/AndroidRuntime(4957):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-27 10:40:57.555: E/AndroidRuntime(4957):     at android.os.Looper.loop(Looper.java:137)
08-27 10:40:57.555: E/AndroidRuntime(4957):     at android.app.ActivityThread.main(ActivityThread.java:4895)
08-27 10:40:57.555: E/AndroidRuntime(4957):     at java.lang.reflect.Method.invokeNative(Native Method)
08-27 10:40:57.555: E/AndroidRuntime(4957):     at java.lang.reflect.Method.invoke(Method.java:511)
08-27 10:40:57.555: E/AndroidRuntime(4957):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
08-27 10:40:57.555: E/AndroidRuntime(4957):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
08-27 10:40:57.555: E/AndroidRuntime(4957):     at dalvik.system.NativeStart.main(Native Method)

for note : 08-27 10:40:57.555: E/AndroidRuntime(4957): at id.co.ajsmsig.espaj.PemegangPolis.onClick(PemegangPolis.java:1546) is refer to Bitmap b = Bitmap.createBitmap(u.getDrawingCache());

回答1:

Try this:

Bitmap bitmap = Bitmap.createBitmap(scrollView.getChildAt(0).getWidth(), scrollView.getChildAt(0).getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
scrollView.getChildAt(0).draw(c);

You can get the screenshot any of the child layout of scrollview.



回答2:

You can use this code to take a screenshot of your full ScrollView the same code can be adapted to all kind of Views

Code :

        View u = findViewById(R.id.scroll_pp);
        u.setDrawingCacheEnabled(true);                                                
        ScrollView scroll_pp = (ScrollView) findViewById(R.id.scroll_pp);
        int totalHeight = scroll_pp.getChildAt(0).getHeight();
        int totalWidth = scroll_pp.getChildAt(0).getWidth();
        u.layout(0, 0, totalWidth, totalHeight);    
        u.buildDrawingCache(true);
        Bitmap b = Bitmap.createBitmap(u.getDrawingCache());             
        u.setDrawingCacheEnabled(false);

        //Save bitmap
        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(getContentResolver(), b, "Screen", "screen");
        }catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }