我正在试图找出如何在我的onDraw方法中绘制一个正方形。
到目前为止我的情况是这样的。
//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)?
编辑:我想要达到的目标
发布于 2014-05-31 11:47:30
假设width是画布的宽度,我猜你错过了两次减法填充。
this.size = width - padding * 2;编辑
由于我们在这里讨论的是一个矩形,您需要对代码进行更多的更改,并计算不同的顶部和左侧填充。
int width = this.getMeasuredWidth();
int height = this.getMeasuredHeight();
int paddingLeft = (width / 10);
size = width - paddingLeft * 2;
int paddingTop = (height - size) / 2;
this.rect.set(paddingLeft,paddingTop,size,size);编辑2
也许一种更清晰的方法可以开始计算你的方格大小。
size = width * 0.9f;
int paddingLeft = (width - size) / 2;
int paddingTop = (height - size) / 2;
this.rect.set(paddingLeft,paddingTop,size,size);https://stackoverflow.com/questions/23969643
复制相似问题