View too large to fit into drawing cache when calling getDrawingCache()

前端 未结 2 622
孤独总比滥情好
孤独总比滥情好 2021-01-03 03:16

I\'m trying to take a screenshot of the contents of a LinearLayout. The layout contains a scrollview that can be of variable height/width. This code works fine when the la

相关标签:
2条回答
  • 2021-01-03 03:43

    Here's what ended up working for me to capture a large off-screen view in it's entirety:

    public static Bitmap loadBitmapFromView(View v) 
    {
        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    
        Canvas c = new Canvas(b);
        v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
        v.draw(c);
        return b;
    }
    

    It's similar to the answer here, except that I use the view's width/height to create the bitmap

    0 讨论(0)
  • 2021-01-03 03:57

    As the original question is talking about a ScrollView I just though I would add the following for anyone that was having problems getting the full height. To get the height of the ScrollView, the v.getHeight doesn't work, you need to use:

    v.getChildAt(0).getHeight()
    

    so the full thing would be:

    ScrollView scrollView = (ScrollView) result.findViewById(R.id.ScrlView);
    
    loadBitmapFromView(scrollView);
    
    public static Bitmap loadBitmapFromView(View v) 
    {
    int height = v.getChildAt(0).getHeight()
    int width = v.getWidth()
    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;
    }
    

    Just using v.getHeight just gets the screen height, not the full height of the ScrollView.

    0 讨论(0)
提交回复
热议问题