开发者

Text Rendered over bitmap

开发者 https://www.devze.com 2023-01-20 17:57 出处:网络
I \'d like to know how can I render a text o开发者_StackOverflow中文版ver a 48x48 bitmap in my android application, considering sometimes the text exceeds the bitmap width .In such cases I need to ren

I 'd like to know how can I render a text o开发者_StackOverflow中文版ver 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, Here's the code I used:

    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;  


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消