convert a textview, including those contents off the screen, to bitmap
I want to save(export) contents of MyView, which extends TextView, into a bitmap.
I followed the code: [this][1].
It works fine when the size of the text is small.
But when there are lots of texts, and some of the content is out of the screen, what I got is only what showed in the screen.
Then I add a "layout" in my code:
private class MyView extends TextView{
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public Bitmap export(){
Layout l = getLayout();
int width = l.getWidth() + getPaddingLeft() + getPaddingRight();
int height = l.getHeight() + getPaddingTop() + getPaddingBottom();
Bitmap viewBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(viewBitmap);
setCursorVisible(false);
layout(0, 0, width, height);
draw(canvas);
setCursorVisible(true);
return viewBitmap;
}
}
Now the strange t开发者_StackOverflowhing happened:
The first time I invoke "export"(I use an option key to do that), I got contents only on the screen.
When I invoke "export" again, I got complete contents, including those out of the screen.
Why?
How to "export" a view, including contents cannot be showed on the screen?
Thank you!
[1]: http://www.techjini.com/blog/2010/02/10/quicktip-how-to-convert-a-view-to-an-image-android/ this
I found out a simpler way: Put the TextView in a ScrollView. Now myTextView.draw(canvas) will draw all of the text.
I think you should be subtracting the padding from the width in the height instead of adding it. Adding it will give you an area larger than the screen.
I solved this issue this way(strange but works):
public Bitmap export(){
//...
LayoutParams lp = getLayoutParams();
int old_width = lp.width;
int old_height = lp.height;
int old_scroll_x = getScrollX();
int old_scroll_y = getScrollY();
lp.width = width;
lp.height = height;
layout(0, 0, width, height);
scrollTo(0, 0);
draw(canvas);
lp.width = old_width;
lp.height = old_height;
setLayoutParams(lp);
scrollTo(old_scroll_x, old_scroll_y);
//...
}
精彩评论