Canvas drawing in home screen widgets

My desired result:

Widget text (Home screen widget) with custom font

My problems:

  • You cannot use a custom font in text form in widgets (since TextView does not have direct access to widgets, we must use them with remoteviews.)

So, I was thinking of using imageview and drawing text on a bitmap using canvas, and then setting this bitmap on the image. So far, everything works, but only two things.

I dont know,

  • How to make my text set vertically and horizontally in the center? (Vertically and horizontally means to be in the center both vertically and horizontally)

  • How to make my text fill the entire space of the bitmap?

I use the following code to display some text in widgets.

Bitmap bitmap = Bitmap.createBitmap(200, 200, Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);

Paint paint = new Paint();

paint.setColor(Color.YELLOW);
canvas.drawPaint(paint);

paint.setColor(Color.GREEN);
paint.setStyle(Style.FILL);

canvas.drawText("Test", 50, 50, paint);

and the next is the conclusion. enter image description here

+3
source share
3 answers

1- Setting the runtime of fonts in text form in widgets

I dont know that either

2- Setting the text in the center and filling the container

Please see the following code:

 float size = 1.0f;

 Bitmap bitmap = Bitmap.createBitmap(200, 200, Config.ARGB_8888);

 Canvas canvas = new Canvas(bitmap);

 Paint paint = new Paint();

 paint.setColor(Color.YELLOW);
 canvas.drawPaint(paint);

 paint.setColor(Color.GREEN);
 paint.setStyle(Style.FILL);

 Rect rect = new Rect();
 paint.getTextBounds("Test", 0, 4, rect);

 float width = 1.0f;

 while (width<200 && rect.height()<200)
 {
  size++;
  paint.setTextSize(size);

  width = paint.measureText("Test");
  paint.getTextBounds("Test", 0, 4, rect);

 }

 size--;
 paint.setTextSize(size);

 canvas.drawText("Test", 100-width/2, 100+rect.height()/2, paint);

I tried to attach a screenshot, but it will not allow me to add, as I am a new user. (rookie: P)

Thank,

+1
source
  • Unable to use custom font in text form in widgets

    TextView tv=(TextView)findViewById(R.id.custom);
    Typeface face=Typeface.createFromAsset(getAssets(),"fonts/Verdana.ttf");
    tv.setTypeface(face);
    

    if you want to use the same in the canvas then set the face for drawing obj that uses u.

  • How to make my text set vertically and horizontally in the center?

  • In the canvas as a drawing text for paint obj. then you can install paint obj

    mPaint.setTextAlign(Align.CENTER);
    

. , ur , .

, SAM

  • canvas.drawText( "SAM", 50, 50, paint);

  •        canvas.drawText("S", 50, 50, paint);
           canvas.drawText("A", 50, 51, paint);
           canvas.drawText("M", 50, 52, paint);
    

    . , mPaint.setTextAlign(Align.CENTER), . , ve 100 widht, 20px 50px, , 20 50 px. 100 .

  • ?

, Canvas .

, u x y, .

0

Hello, why can't you use custom font in TextView: I think you can just subclass TextView and add this three-story text view designer

  if (!isInEditMode()) {
              Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/ds_digib.ttf");
              setTypeface(tf);
    }

To set the text vertically and horizontally

see link

enter image description here

Hope this helps you solve your problem.

0
source

All Articles