获取自定义视图
问题描述:
我想画一个自定义视图中十字线的正确尺寸,获取自定义视图
@Override
public void onWindowFocusChanged(boolean hasWindowFocus)
{ super.onWindowFocusChanged(hasWindowFocus);
top=this.getTop()+(this.getTop()/10);
bottom=this.getBottom()-(this.getBottom()/10);
left=this.getLeft()+(this.getLeft()/10);
right=this.getRight()-(this.getRight()/10);
Log.e("graph",getTop()+":"+top+","+this.getBottom()+":"+bottom+","+this.getLeft()+":"+left+","+this.getRight()+":"+right);
}
我使用这个代码来获取上,下,左,视右坐标和调整第十部分作为填充。
的onDraw方法内,
canvas.drawLine(left, (bottom - top)/2, right, (bottom - top)/2, paint);
canvas.drawLine((right-left)/2,top,(right-left)/2,bottom,paint);
这两种方法来绘制该水平居中和垂直居中的线。我正在手机屏幕上显示此图。
我想有水平居中和垂直居中行,我该怎么办?
答
顶部,底部,左侧和右侧属性不描述视图的尺寸。他们描述视图在其容器内的位置。
相反,在视图对象上使用getWidth()和getHeight(),而不是使用该信息来查找尺寸和绘图。
答
顶部,底部,都是坐标相对于IST容器视图的位置, 我认为你需要像画线:
canvas.drawLine(this.getLeft(), (this.getTop() + this.getBottom())/2, this.getRight(), (this.getTop() + this.getBottom())/2, paint);
canvas.drawLine((this.getLeft()+this.getRight())/2, this.getTop(), (this.getLeft()+this.getRight())/2, this.getBottom(), paint);
在http://developer.android.com/ reference/android/view/View.html,它写成“例如,调用getRight()类似于下面的计算:getLeft()+ getWidth()”所以我不thisnk它会有所作为。 –