tags:

views:

26

answers:

1

Hello,

I 'd like to know how can I render a text over a 48x48 bitmap in my android application, considering sometimes the text exceeds the bitmap width .In such cases I need to render only a part of the text followed by dots such that all fits the available width !

Thanks !

Update Edit : the code I use is :

Bitmap renderSurface = icon.createBitmap(); Canvas canvas = new Canvas(renderSurface);

Paint paint  = new Paint();
paint.setTextSize(10);
if(paint.measureText(nativeName)>canvas.getWidth())
    nativeName = getClippedString(paint,nativeName,canvas.getWidth() );

canvas.drawText(nativeName,33,0, paint);

return renderSurface;  
+3  A: 

You have to create another Bitmap (unless the original Bitmap is mutable) and then create a Canvas to draw on that Bitmap. Finally, just call drawText() on the Canvas. To know where to ellipsize your text, you can use the Paint's class text measurement methods.

Romain Guy
Could you show me in a snippet how to obtain an instance of the Paint class ?
rantravee
Oh !, silly question from me , please ignore it. However I obtain the following error that I can not resolve and don't know what it means : " immutable bitmap passed to canvas constructor"
rantravee
Read the Bitmap documentation. You must use one of the copy*() or create*() methods that returns a *mutable* Bitmap. Only mutable bitmaps can be drawn upon.
Romain Guy