我试过这个
@Override
protected void onDraw(Canvas canvas)
{
Log.e("TEST", "canvas width: " + canvas.getWidth() + "");
Log.e("TEST", "view width: " + this.getWidth() + "");
Log.e("TEST", "canvas height: " + canvas.getHeight() + "");
Log.e("TEST", "view height: " + this.getHeight() + "");
super.onDraw(canvas);
Log.e("TEST", "canvas width: " + canvas.getWidth() + "");
Log.e("TEST", "view width: " + this.getWidth() + "");
Log.e("TEST", "canvas height: " + canvas.getHeight() + "");
Log.e("TEST", "view height: " + this.getHeight() + "");
}
这是结果:
canvas width: 320
view width: 480
canvas height: 533
view height: 300
canvas width: 320
view width: 480
canvas height: 533
view height: 300
部分来自我的 main.xml
android:layout_width="480px"
android:layout_height="300px"
根据我的日志打印和我的部分 xml,视图大小是正确的。我的设备是 480x800,宽度和高度除以 Log 结果为 1.5(800/533=1.5 和 480/320=1.5),所以我似乎从整个屏幕上得到了画布。为什么会这样,为什么这么小,我应该怎么做才能以正常方式绘制?