Android自定义View之getTextBounds()

在Android自定义View的过程中一定会用到Paint,而paint属性中有一个方法getTextBounds(String text,int start,int end,Rext bounds),它的中文解释是:返回一个包含中文的矩形边界,位置为(start,end)

英文解释:Return in bounds (allocated by the caller) the smallest rectangle that encloses all of the characters, with an implied origin at (0,0).由调用者返回在边界(分配)的最小矩形包含所有的字符,以隐含原点(0,0)。

Parameters

text    String:String to measure and return its bounds  要测量的字符串,并返回它的界限

start   int:Index of the first char in the string to measure  在这个字符串里的第一个字符的索引,它将作为测量的起点

end    int:1 past the last char in the string  measure

bounds    Rect:Returns the unioned bounds of all the text.返回包含文本在内,所形成的边界。

下面是第二个方法:

void getTextBounds (char[] text, 
                int index, 
                int count, 
                Rect bounds)
Return in bounds (allocated by the caller) the smallest rectangle that encloses all of the characters, with an implied origin at (0,0).由调用者返回在边界(分配)的最小矩形包含所有的字符,以隐含原点(0,0)。


Parameters
text    char: Array of chars to measure and return their unioned bounds一个包含字符的数组,测量并返回他们的联合边界
index    int: Index of the first char in the array to measure 在这个 数组 里的第一个(要测量的)字符的索引(你不一定要指定为0, 虽然那确实意味着从第一个开始),它将作为测量的起点
count    int: The number of chars, beginning at index, to measure字符的数量(比如5),测量将会 开始于上面指定的索引号,结束于 此数量值之后(若为5, 则上面指定的索引号+5)
bounds    Rect: Returns the unioned bounds of all the text. Must be allocated by the caller.返回包含所有文本在内,所形成的边界,(所以叫unioned bounds)。必须分配给调用者。

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,关于 Android 自定义进度条控件的实现,我可以为您提供一些思路和示例代码。 首先,我们需要继承自 View 或者 ProgressBar,然后在 onDraw 方法中实现自己的绘制逻辑。具体来说,需要绘制背景、进度条和文本等内容。 示例代码如下: ```java public class CustomProgressBar extends ProgressBar { private Paint mPaint; private String mText; private Rect mRect; public CustomProgressBar(Context context) { super(context); init(); } public CustomProgressBar(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CustomProgressBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { mPaint = new Paint(); mPaint.setAntiAlias(true); mRect = new Rect(); } @Override protected synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); // 绘制背景 mPaint.setColor(Color.GRAY); mPaint.setStyle(Paint.Style.FILL); canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint); // 绘制进度条 mPaint.setColor(Color.BLUE); mPaint.setStyle(Paint.Style.FILL); float progress = getProgress() * 1.0f / getMax(); canvas.drawRect(0, 0, getWidth() * progress, getHeight(), mPaint); // 绘制文本 mPaint.setColor(Color.WHITE); mPaint.setTextSize(24); mText = getProgress() + "%"; mPaint.getTextBounds(mText, 0, mText.length(), mRect); float x = getWidth() / 2f - mRect.centerX(); float y = getHeight() / 2f - mRect.centerY(); canvas.drawText(mText, x, y, mPaint); } } ``` 这个自定义控件实现了一个简单的水平进度条,包括了背景、进度条和文本三个部分。当然,您可以根据自己的需求进行更改和扩展。 希望这个示例代码能够帮助到您,如果您还有其他问题,欢迎继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值