【发布时间】:2014-05-31 20:23:52
【问题描述】:
我正在尝试弄清楚如何在 Android 中的 onDraw 方法中绘制一个 Square。
- 正方形必须位于画布的正中心 (不是屏幕)
- 正方形左右两侧的填充/间距应为 相等
- 正方形顶部和底部的填充/间距应该相等
- 正方形的大小应该比较大,大概是90%左右 画布宽度
这是我目前所拥有的。
//this.rect is an instance of Rect() which later gets called in the canvas.drawRect() method
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = this.getMeasuredWidth();
int height = this.getMeasuredHeight();
int padding = (width / 10);
this.size = width - padding;
this.rect.set(padding,padding,size,size);
}
上面的代码绘制了正方形,但我不知道如何让它在画布中居中。我也愿意使用不涉及使用 Rect 的另一种技术。
我需要为此Rect() 设置哪些属性,以便canvas.drawRect(rect,paint) 直接在画布中心绘制矩形?
【问题讨论】: