【问题标题】:drawText on a lower right position右下角的drawText
【发布时间】:2014-08-27 12:21:22
【问题描述】:

如何在屏幕右下方绘制文字?

这是我在 drawtext 上的代码。
编辑

Rect textBounds = new Rect();
    if(this != null) {
        timeCounter.start();
        int time= timeCounter.getTimeSeconds();
        String timetxt= String.format("%02d:%02d", time/60, time%60);
        timer.getTextBounds(timetxt, 0, text.length(), textBounds);
        canvas.drawText(timetxt, -textBounds.left, -textBounds.top, timer);
        }

我只是遵循一些教程,但它不起作用。有人可以帮我解决这个逻辑吗?提前致谢!

【问题讨论】:

  • 你只需要通过 canvas 或者你也可以使用 RelativeLayout 吗?
  • 参见 Paint.getTextBounds 方法
  • 我已经设置了 getTextBounds 先生 @pskink 但我现在的问题在于 drawtext 参数。
  • 为什么你测量text却绘制timetxt
  • 文本出现在屏幕的左上角。我怎么可能在右下角找到它?

标签: android eclipse drawtext


【解决方案1】:

我希望您尝试在视图 ondraw 回调中执行此操作。

如果是这样,您可能需要考虑两件事。

  • 使用 simpleDateFormat 而不是自己格式化字符串。它更容易。
  • 您在视图之外绘图。

sind 左上角为 0,0 textbounds.left 或 textbounds.right > 0 的任何值至少会部分隐藏您的文本。

如果你想把它画在右下角,你需要调用它:

canvas.drawText(timetxt, this.getWidth()-textBounds.left, this.getHeight()-textBounds.top, timer);

如果您没有在 onDrawCallback 中执行此操作,请将其移入一个;)

【讨论】:

  • is 也不起作用。 @DanielBo 先生,我现在什至看不到计时器
  • 你以前能看到计时器吗?你能发布更多代码吗,因为这看起来很奇怪
  • 应该是 this.getWidth()-textBounds.right 和 this.getHeight()-textBounds.bottom
  • 哦,是的,对不起。我的错。但你明白我的意思:)
猜你喜欢
  • 2013-12-22
  • 2018-03-01
  • 1970-01-01
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-18
  • 2023-03-16
相关资源
最近更新 更多