android绘制矩形单元问题

发布于 2024-11-27 18:30:11 字数 1007 浏览 3 评论 0原文

我试图跟踪扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生未歇 2024-12-04 18:30:11

事实证明, getGlobalVisibleRect 要么在 3.0 之前就被破坏了,要么没有返回我所期望的结果。我发现我可以这样做。

TextView tv = (TextView)findViewById(R.id.textView1);
Rect bounds = new Rect(tv.getLeft(), tv.getTop(), tv.getRight(), tv.getBottom()); 

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.

TextView tv = (TextView)findViewById(R.id.textView1);
Rect bounds = new Rect(tv.getLeft(), tv.getTop(), tv.getRight(), tv.getBottom()); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文