drawRect 单元问题 android [英] drawRect unit issues android

查看:30
本文介绍了drawRect 单元问题 android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试跟踪扩展 LinearLayout 的类中子 TextView 的边界 Rect 我正在使用 View.getGlobalVisibleRect(Rect) 以获取 TextView 相对于其父级的边界框.它在某些设备上运行良好,但在其他手机上显然存在某种单元问题.我所看到的简单示例:

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);
}

预期它应该在 TextView 电视下绘制一个框.它在我的 3.1 平板电脑上执行,但在我的 1.6 和 2.3 手机上,它在 TextView 下方绘制框.似乎我需要将边界框的值转换为不同类型的像素单位,以获得我期望的一致结果.

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.

问题是我不确定返回的 Rect 是否已经在 DIP 或标准像素中.我试过两者兼而有之:

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());

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.

谢谢!

推荐答案

结果证明 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.

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

这篇关于drawRect 单元问题 android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆