如何将滚动视图中的所有内容转换为位图? [英] How to convert all content in a scrollview to a bitmap?

查看:21
本文介绍了如何将滚动视图中的所有内容转换为位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码进行转换,但似乎只能获取显示屏幕中的内容,无法获取显示屏幕中的内容.

I use below code to convert, but it seems can only get the content in the display screen and can not get the content not in the display screen.

有没有办法让所有内容甚至脱离滚动?

Is there a way to get all the content even out of scroll?

Bitmap viewBitmap = Bitmap.createBitmap(mScrollView.getWidth(),mScrollView.getHeight(),Bitmap.Config.ARGB_8888);  
Canvas canvas = new Canvas(viewBitmap); 
mScrollView.draw(canvas);

推荐答案

我们可以使用如下所示的代码将 scrollView 的所有内容转换为位图图像

We can convert all the contents of a scrollView to a bitmap image using the code shown below

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 Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) {

    Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth,totalHeight , Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null)
        bgDrawable.draw(canvas);
    else
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);
    return returnedBitmap;
}

这篇关于如何将滚动视图中的所有内容转换为位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆