drawRect的单元问题的android单元、问题、drawRect、android

2023-09-07 14:27:33 作者:不哭不闹不炫耀

我想跟踪边界矩形为孩子的TextView A类扩展的LinearLayout 我才能得到的TextView的相对于其父边框使用 View.getGlobalVisibleRect(矩形)。它的伟大工程上的一些设备,但显然是某种装置问题对其他手机怎么回事。对我所看到的简单的例子:

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.

但问题是我不知道,如果矩形返回已在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());

但这些都不似乎是箱顶被转换为它应该是。我将非常AP preciate任何意见或建议。

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 或者是坏了pre-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());