вторник, 1 апреля 2014 г.

Widgets and custom fonts


As you could know widgets use certain amount of the views and use it indirectly. There is no possibility set up custom font to some view. The object RemoveView in widgets doesn’t have such ability.

In this case you can use such method:
-create textview dinamically and set up custom font.
- add textview to canvas
- create bitmap from this canvas
- display it
The original idea is placed here: http://habrahabr.ru/post/117672/

private Bitmap convertToImg(String text, Context context)
   {
       Bitmap btmText = Bitmap.createBitmap(
400, 100, Bitmap.Config.ARGB_4444);
       Canvas cnvText =
new Canvas(btmText);
       
       Typeface tf = Typeface.createFromAsset(context.getAssets(),
"benegraphic.regular.ttf");
       
       Paint paint =
new Paint();
       paint.setAntiAlias(
true);
       paint.setSubpixelText(
true);
       paint.setTypeface(tf);
       paint.setColor(Color.WHITE);
       paint.setTextSize(
50);
       
       cnvText.drawText(text,
150, 50, paint);
       
return btmText;
   }

I have got such widget:widget_custom_font.png

Комментариев нет:

Отправить комментарий