android绘制矩形单元问题
我试图跟踪扩展 LinearLayout
的类中子 TextView
的边界 Rect
我正在使用 View.getGlobalVisibleRect (Rect)
以便获取 TextView 相对于其父级的边界框。它在某些设备上运行良好,但在其他手机上显然存在某种单位问题。我所看到的简单示例:
//Extended LinearLayout's onDraw
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
TextView tv = (TextView)findViewById(R.id.textView1);
Rect bounds = new Rect();
tv.getGlobalVisibleRect(bounds);
canvas.drawRect(bounds, myPaint);
}
预期的是它应该在 TextView 电视下绘制一个框。它在我的 3.1 平板电脑上执行此操作,但在我的 1.6 和 2.3 手机上它会在 TextView 下方绘制框。看来我需要将边界框的值转换为不同类型的像素单位,以便获得我期望的一致结果。
问题是我不确定返回的 Rect 是否已经是 DIP 或标准像素。我尝试过同时执行以下操作:
bounds.top = TypedValue.complexToDimensionPixelSize(bounds.top, getContext().getResources().getDisplayMetrics());
和
bounds.top = TypedValue.COMPLEX_UNIT_DIP, bounds.top, getContext().getResources().getDisplayMetrics());
但这些似乎都没有转换盒子顶到它应该在的地方。我将非常感谢任何反馈或建议。
谢谢!
I am trying to keep track of the bounding Rect
for a child TextView
inside of a class extending LinearLayout
I am using View.getGlobalVisibleRect(Rect)
in order to get the TextView's bounding box relative to its parent. It works great on some devices, but there is obviously some kind of unit issue going on on other phones. Simple example of what I'm seeing:
//Extended LinearLayout's onDraw
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
TextView tv = (TextView)findViewById(R.id.textView1);
Rect bounds = new Rect();
tv.getGlobalVisibleRect(bounds);
canvas.drawRect(bounds, myPaint);
}
Whats expected is that it should draw a box under the the TextView tv. It does on my 3.1 tablet, but on my 1.6 and 2.3 phones it draw the box underneath of the TextView. It seems that I need to convert the values of the bounding box to a differnt kind of pixel unit in order to get the results I expect consistently.
Problem is I am not sure if the Rect returned is already in DIP or standard pixels. I have tried doing both:
bounds.top = TypedValue.complexToDimensionPixelSize(bounds.top, getContext().getResources().getDisplayMetrics());
and
bounds.top = TypedValue.COMPLEX_UNIT_DIP, bounds.top, getContext().getResources().getDisplayMetrics());
But neither of these seems to be converting the box top to where it should be. I would greatly appreciate any feedback or advice.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,
getGlobalVisibleRect
要么在 3.0 之前就被破坏了,要么没有返回我所期望的结果。我发现我可以这样做。Turns out that
getGlobalVisibleRect
is either broken pre-3.0 or doesn't return what I am expecting. I found that I could do this instead.