整个"截图"可滚动视图的

时间:2017-05-23 08:13:42

标签: android screenshot android-scrollview

在我的片段布局中,我有一个带有LinearLayout的ScrollView

<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>

所以我需要创建并分享scrollview的整个内容的图片。我试过的所有解决方案都只截取可见区域的屏幕截图,而不是整个scrollview内容的屏幕截图。我该怎么办?

3 个答案:

答案 0 :(得分:6)

调用以下方法getYourLayout(),您可以在其中捕捉布局。在这个我附加布局在一个对话框&amp;获取根布局的snapshop,而不向用户显示对话框。一切都发生在后台。

 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) {
    }
}

此功能用于将位图保存为文件。

  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();
    }
}

以下功能是在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();

    }
}

当然这会帮助你。因为这个解决方案很多时候解决了我的问题。

答案 1 :(得分:5)

我希望这对你有用..来源here。这在技术上不是截图代码。但是这段代码将整个布局视图转换为位图

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 :(得分:0)

尝试一下: https://github.com/PGSSoft/scrollscreenshot

它应该是有用的。

描述: 制作可滚动屏幕内容的Android屏幕截图 - 由PGS Software SA提供给您

此工具可制作多个屏幕截图,每次拍摄之间自动滚动屏幕内容。默认情况下,状态栏和导航栏仅包含一次。