Take whole “Screenshot” of a scrollable View

2020-02-07 12:36发布

问题:

In my Fragment's layout i have a ScrollView with a LinearLayout inside

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <!-- Other views -->

    </LinearLayout>
</ScrollView>

So i need to create and share a picture of entire content of scrollview. All solutions i've tried take screenshot only of the visible area, and not of entire scrollview content. How can i do?

回答1:

I hope this is work for you.. source here. this is not technically a screenshot code. but this code convert the whole layout view into bitmap

Bitmap bitmap = getBitmapFromView(scrollview, scrollview.getChildAt(0).getHeight(), scrollview.getChildAt(0).getWidth());

//create bitmap from the ScrollView 
private Bitmap getBitmapFromView(View view, int height, int width) {
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null)
        bgDrawable.draw(canvas);
    else
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);
    return bitmap;
}


回答2:

call below method getYourLayout() where you want to take snap of your layout. In this i attach layout in one dialog & take snapshop of root layout whithout showing dialog to user. All thing happens in background.

 private void getYourLayout() {
    try {
        Dialog fb_event_info = new Dialog(YourActivity.this);
        fb_event_info.requestWindowFeature(Window.FEATURE_NO_TITLE);
        fb_event_info.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        fb_event_info.setContentView(R.layout.yourXmlLayoutFile);

        final LinearLayout lnr_fb_info = (LinearLayout) fb_event_info.findViewById(R.id.container);
        TextView tv_fb_event_name = (TextView) fb_event_info.findViewById(R.id.tv_fb_event_name);

        tv_fb_event_name.setTypeface(Global.setCubanoFont(EventDetailActivity.this));
        tv_fb_event_name.setText(tv_event_name.getText().toString());

        lnr_fb_info.setDrawingCacheEnabled(true);

        lnr_fb_info.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        lnr_fb_info.layout(0, 0, lnr_fb_info.getMeasuredWidth(), lnr_fb_info.getMeasuredHeight());

        lnr_fb_info.buildDrawingCache(true);
        bitmap = Bitmap.createBitmap(lnr_fb_info.getDrawingCache());


        saveImage(bitmap);

    } catch (Exception e) {
    }
}

This Function is for Saving your Bitmap as file.

  private void saveImage(Bitmap bitmap) {
    try {
        Log.e("----------in---", "saveImage....: ");
        if (!rootFile.exists())
            rootFile.mkdirs();
        long time = System.currentTimeMillis();
        fname = "mynight-" + time + ".png";
        rootFile = new File(rootFile, fname);
        Log.e("----------in---", "saveImage...1.: ");
        try {
            FileOutputStream Fout = new FileOutputStream(rootFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, Fout);
            sendShareFb();
            Fout.flush();
            Fout.close();

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

Below function is for share your image on facebook.

 private void sendShareFb() {
    try {
        Log.e("----------in---", "sendShareFb....: ");
        Intent fbIntent = new Intent(Intent.ACTION_SEND);

        File imageFile = new File(rootFile.toString());
        fbIntent.putExtra(Intent.EXTRA_TEXT, "Share..");
        fbIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
        fbIntent.setType("image/jpeg/png");
        PackageManager pm = getPackageManager();
        List<ResolveInfo> lract = pm.queryIntentActivities(fbIntent, PackageManager.MATCH_DEFAULT_ONLY);
        boolean resolved = false;
        for (ResolveInfo ri : lract) {
            if (ri.activityInfo.name.toLowerCase().contains("facebook")) {
                fbIntent.setClassName(ri.activityInfo.packageName, ri.activityInfo.name);
                resolved = true;
                break;
            }
        }
        if (!resolved) {
            Toast.makeText(EventDetailActivity.this, "Vous ne semblez pas avoir Facebook installé sur cet appareil", Toast.LENGTH_SHORT).show();
        }

        startActivity(resolved ? fbIntent : Intent.createChooser(fbIntent, "Choose one"));
    } catch (final ActivityNotFoundException e) {
        e.printStackTrace();

    }
}

Sure that this will Help you. Because this solution has fixed my problem many time.