首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得一个完整的屏幕截图,如果你有一个很长的活动与滚动条

如何获得一个完整的屏幕截图,如果你有一个很长的活动与滚动条
EN

Stack Overflow用户
提问于 2017-01-03 07:31:04
回答 3查看 106关注 0票数 0

如果您的活动内容太多,并且显示了一个很长的ui,比如包含一个ListView或WebView,

,我们如何获得包含长图像中的全部内容的屏幕截图?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-01-03 07:36:07

我认为这个链接中的答案可以帮助你:

How to convert all content in a scrollview to a bitmap?

我在上面的链接中混合了两个答案,为您编写了一段优化的代码:

代码语言:javascript
复制
private void takeScreenShot() 
{
View u = ((Activity) mContext).findViewById(R.id.scroll);

HorizontalScrollView z = (HorizontalScrollView) ((Activity) mContext).findViewById(R.id.scroll);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();

Bitmap b = getBitmapFromView(u,totalHeight,totalWidth);             

//Save bitmap
String extr = Environment.getExternalStorageDirectory()+"/Folder/";
String fileName = "report.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(mContext.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 getBitmapFromView(View view, int totalHeight, int totalWidth) {

int height = Math.min(MAX_HEIGHT, totalHeight);
float percent = height / (float)totalHeight;

Bitmap canvasBitmap = Bitmap.createBitmap((int)(totalWidth*percent),(int)(totalHeight*percent), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(canvasBitmap);

Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
   bgDrawable.draw(canvas);
else
   canvas.drawColor(Color.WHITE);

canvas.save();
canvas.scale(percent, percent);
view.draw(canvas);
canvas.restore();

return canvasBitmap;
}
票数 1
EN

Stack Overflow用户

发布于 2017-01-03 07:44:06

尝试使用这个,将您的视图传递给这个函数,

代码语言:javascript
复制
public Bitmap getBitmapFromView(View view) {
        // Define a bitmap with the same size as the view
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
        // Bind a canvas to it
        Canvas canvas = new Canvas(returnedBitmap);
        // Get the view's background
        Drawable bgDrawable = view.getBackground();
        if (bgDrawable != null)
            // has background drawable, then draw it on the canvas
            bgDrawable.draw(canvas);
        else
            // does not have background drawable, then draw white background on
            // the canvas
            canvas.drawColor(Color.WHITE);
        // draw the view on the canvas
        view.draw(canvas);
        // return the bitmap
        return returnedBitmap;
    }

这个函数将返回一个位图,您可以以您想要的方式使用它。

票数 1
EN

Stack Overflow用户

发布于 2017-01-03 07:40:12

您可以轻松地将布局转换为“位图图像”。

代码语言:javascript
复制
protected Bitmap ConvertToBitmap(LinearLayout layout) {

    layout.setDrawingCacheEnabled(true);

    layout.buildDrawingCache();

    Bitmap bitmap = layout.getDrawingCache();

    return bitmap;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41438494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档