سؤال

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;
 }
}
هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

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)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top