Stretched bitmap in android
I'm working on a piece of code where i have to write text and images on a bitmap. Unfortunately i don't know the number of text items to be added in bitmap. To begin i create a bitmap by this
Bitmap bitMapBuffer = Bitmap.cr开发者_如何学GoeateBitmap(containerWidth,50, Bitmap.Config.ARGB_8888);
the problem is .. the above bitmap is fixed size.. so after a while whatever i write does not show up on bitmap. How can i make it work, i mean a kind of stretched bitmap type. I looked in BitmapDrawable
, but i cannot pass a drawable to a canvas like this
Canvas c1 = new Canvas(bitMapBuffer);
How can i handle this scenario?
You need to keep track of how big a bitmap you need. When you need to add another piece of text and this would take you over the limit of the current bitmap, you need to create a new one. Here's pseudocode for one way to do this:
Bitmap buffer = Bitmap.createBitmap(containerWidth, 50, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(buffer);
// ...
while (more_text_to_add()) {
if (bitmap_too_small()) {
Bitmap old = buffer;
buffer = Bitmmap.createBitmap(buffer.getWidth(),
buffer.getHeight + delta, Bitmap.Config.ARGB_8888);
c = new Canvas(buffer);
c.drawBitmap(old, 0, 0, null);
}
draw_more_text();
}
In this code, delta
is the added height you want to for the bitmap.
Have you considered NinePatch?
精彩评论