Android画布区域不是全屏。如何设置画布区域?

huangapple go评论79阅读模式
英文:

Android Canvas Area is not full screen. How to set canvas area?

问题

你好。
我在Android画布上绘制了一个矩形覆盖层。

    canvas.drawRect(0, 0, 1000, 1000, paint);

这是我的代码。我设置起始点为(0,0),即(左,上)坐标。
但是(0,0)不位于全屏的左上角端点。
无论宽度和高度如何,画布也无法覆盖整个屏幕。

我该如何解决这个问题?
英文:

Android画布区域不是全屏。如何设置画布区域?

Hello.
I draw a rectangle overlay on Android Canvas.

canvas.drawRect(0, 0, 1000, 1000, paint);

That is my code. I set start point is (0,0) that is (left, top) coordinate.
But (0,0) is not on a left-top end point of full screen.
No matter how width and height are big, canvas cannot cover full screen also.

How can I solve this problem?

答案1

得分: 1

如果您想要全屏查看,请使用setContentView(canvas)调用。这将将完整的画布视图设置为布局。

英文:

If you want a fullscreen view, use the setContentView(canvas) call. That sets the full canvas view as the layout.

答案2

得分: 0

你的问题在于,默认情况下,XML 使用一个称为 Titlebar 的东西,可以在这里找到答案。除了移除 Titlebar 外,你还可以将活动设置为全屏,通过使用这个解决方案来移除状态栏。

确保你的活动从 Activity 而不是 ActionBarActivity 进行导入。

另外,在设置位置或大小时最好避免使用实际数字,特别是当目标是覆盖整个屏幕时。你可以通过使用 getWidth()getHeight() 来获取屏幕的宽度和高度:

canvas.drawRect(0, 0, getWidth(), getHeight());

请务必仅在调用 onDraw 方法后或其中使用这些方法,否则它们的值将等于 0,因为画布尚未初始化。

英文:

Your issue is that by default, XML uses a thing called Titlebar, as answered here. Along with removing the Titlebar, you can set your activity to full-screen and also remove the status-bar by using this solution.

Make sure that your activity imports from Activity rather than ActionBarActivity.

Also, it is better to avoid using actual numbers when setting a position or a size, and especially when the goal is to cover the entire screen. You can get the width and height of the screen by using getWidth() and getHeight() accordingly:

canvas.drawRect(0, 0, getWidth(), getHeight());

Make sure to use these methods only in or after the onDraw method was called, else their values will be equal to 0 since the canvas was not yet initialized.

huangapple
  • 本文由 发表于 2020年3月15日 19:57:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/60692609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定