ANDROID:我的自定义视图没有完全绘制

发布于 2024-10-08 20:00:32 字数 1066 浏览 3 评论 0原文

我创建了一个扩展 View 的类,这就是 onDraw 方法。该程序创建了一个迷宫,为了获得适当的高度和宽度读数,我似乎需要在 onDraw 方法中调用它们(否则它只会返回 0)。这可能就是把一切搞砸的原因。但是,它会根据所绘制的视图部分中可见方块的间距获得正确的高度。

视图中似乎未绘制的部分大约是上下文菜单的大小,并且与备件不匹配。我找过其他有这个问题的人,似乎没有其他人有这个问题,据我所知,我没有做任何与他们特别不同的事情。如果我可以提供任何其他见解,请告诉我。

我还不能发布图片,因为我对整个堆栈溢出问题很陌生=( 因此,我试图尽可能地解释这一现象。

谢谢!

@Override
 public void onDraw(Canvas canvas) {
  if(firstRun){
  width = getMeasuredWidth();
  height = getMeasuredHeight();
  MazeMake();
  invalidate();
 }else
  for (int i = 0; i < c; i++)
   for (int j = 0; j < r; j++) {
    grid[i][j].paintSq(canvas);
   }
 }

@Override
 protected void onMeasure(int wMeasureSpec, int hMeasureSpec) {
  int measuredHeight = measure(hMeasureSpec);
  int measuredWidth = measure(wMeasureSpec);
  setMeasuredDimension(measuredHeight, measuredWidth);
 }

private int measure(int measureSpec) {
 int specMode = MeasureSpec.getMode(measureSpec);
 int specSize = MeasureSpec.getSize(measureSpec);
 if (specMode == MeasureSpec.UNSPECIFIED)
   return 500;
 else {
   return specSize;
 }
}

I have made a class that extends View and this is the onDraw method. The program creates a maze and to get an appropriate reading of the height and width, I appeared to need to call them in the onDraw method (otherwise it would just return 0 for both). This may be what is screwing everything up. However, it gets the correct height based on the spacing of the visible squares in the section of the view that is painted.

The section of the view that appears to be unpainted is about the size of the context menu and does not match up with spares. I have looked for other people having this problem and it appears nobody else is having this problem and as best I can tell, I am not doing anything particularly different from them. If there is any other insight I can provide, please let me know.

I can't post pictures yet because I'm new at this whole stack overflow thing =(
Thus I tried to explain the phenomenon as best as I could.

Thanks!

@Override
 public void onDraw(Canvas canvas) {
  if(firstRun){
  width = getMeasuredWidth();
  height = getMeasuredHeight();
  MazeMake();
  invalidate();
 }else
  for (int i = 0; i < c; i++)
   for (int j = 0; j < r; j++) {
    grid[i][j].paintSq(canvas);
   }
 }

@Override
 protected void onMeasure(int wMeasureSpec, int hMeasureSpec) {
  int measuredHeight = measure(hMeasureSpec);
  int measuredWidth = measure(wMeasureSpec);
  setMeasuredDimension(measuredHeight, measuredWidth);
 }

private int measure(int measureSpec) {
 int specMode = MeasureSpec.getMode(measureSpec);
 int specSize = MeasureSpec.getSize(measureSpec);
 if (specMode == MeasureSpec.UNSPECIFIED)
   return 500;
 else {
   return specSize;
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡墨 2024-10-15 20:00:32

也许重写“ onMeasure (int widthMeasureSpec, int heightMeasureSpec)”方法,并且不要忘记调用“setMeasuredDimension(int, int)”,如文档中所述

http://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas )

Maybe override the " onMeasure (int widthMeasureSpec, int heightMeasureSpec)" method and don't forget to call "setMeasuredDimension(int, int)" as described in the documentation

http://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas)

蓝色星空 2024-10-15 20:00:32

我得到了它。事实证明,我成功地在 setMeasuredDimension(int, int) 调用中切换了宽度和高度。我以为我这样做是恰当的,但经过几个小时的拔毛后,我发现事实并非如此。

I got it. As it turns out, I had managed to switch the width and height in the setMeasuredDimension(int, int) call. I thought I had done so appropriately, but after hours of pulling my hair out, determined that was not, in fact, the case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文