【问题标题】:How to determine the TextView width in a Widget?如何确定 Widget 中的 TextView 宽度?
【发布时间】:2012-12-27 11:25:56
【问题描述】:

当我查看解决方案时:

How to use a custom typeface in a widget

在上面的问题中,我在一个Widget中使用了自定义字体; 使用解决方案:

public Bitmap buildUpdate(String time) 
  {
    Bitmap myBitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_4444);
    Canvas myCanvas = new Canvas(myBitmap);
    Paint paint = new Paint();
    Typeface clock = Typeface.createFromAsset(this.getAssets(),"Clockopia.ttf");
    paint.setAntiAlias(true);
    paint.setSubpixelText(true);
    paint.setTypeface(clock);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTextSize(65);
    paint.setTextAlign(Align.CENTER);
    myCanvas.drawText(time, 80, 60, paint);
    return myBitmap;
  }

问题:如何根据字符数确定生成的位图的宽高?

【问题讨论】:

    标签: android


    【解决方案1】:

    有了这个:

    Rect rect = new Rect();
    paint.getTextBounds(time, 0, time.length(), rect); // time = your text
    int w = rect.width(); // width text
    int h = rect.height(); // height of text
    

    这会将文本的边界保存在 Rect 中,然后您可以测量 Rect。

    【讨论】:

    • 我需要增加宽高,这样字体才能正常显示-->int widthOffset = Utils.dip2px(context, 12); int heightOffset = Utils.dip2px(context, 3);位图 myBitmap = Bitmap.createBitmap(rect.width(), size, Bitmap.Config.ARGB_4444);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多