画布布局无法正常工作

时间:2012-10-20 14:37:05

标签: android canvas surfaceview ondraw

我通过使用OnDraw方法在画布上绘制矩形来创建地图。这在三星s2上完美运行但是当我在s3中安装它时画布看起来太小了......我该怎么办?

我使用Surface视图绘制矩形。

1 个答案:

答案 0 :(得分:0)

Galaxy S2是hdpi,S3是xhdpi,所以这可能是为什么矩形看起来要小一些。我建议覆盖onMeasure,然后根据画布的当前高度和宽度设置矩形的高度和宽度。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentViewWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentViewHeight = MeasureSpec.getSize(heightMeasureSpec);
// ... take into account the parent's size as needed ...
super.onMeasure(
    MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY), 
    MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY));

}

编辑:刚刚意识到你说过使用SurfaceView绘制矩形而不是View。在onDraw中,只需获取画布的当前高度和宽度,并根据这些尺寸绘制矩形,而不是硬编码矩形的固定尺寸。希望这有帮助