为什么onDraw方法中的canvas方法没有nullpointerexception?

时间:2018-12-26 15:36:19

标签: android canvas

我正在尝试使用onDraw方法绘制矩形,因为Canvas对象具有参数。使用画布对象从onDraw方法调用drawRect方法时,如何初始化画布对象。

我尝试在onDraw方法中为Canvas类初始化新对象。但是输出显示为空白屏幕。

  protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(200,20,300,400,paint);

    }
}

Canvas对象画布如何初始化?如果不需要,在什么情况下会发生nullpointerexception?

1 个答案:

答案 0 :(得分:1)

您的视图负责创建片段,如果要在视图上绘制任何内容,则必须使用视图提供的画布实例View's canvas cannot be null

如果您检查android的源代码,则将发现一个名为mCanvas的变量,您可以在onDraw()方法调用中进行操作。

如果您想知道View如何实例化画布check here

如果您要操纵画布,只需操纵Canvas实例onDraw()

 privat Paint paint;

 //call this method in constructor of your View
 void setPaint(){
      this.paint = new Paint();
      this.paint.setColor(Color.BLACK);
      this.paint.setStyle(Style.STROKE);
      this.paint.setStrokeWidth(10.0F);
      this.paint.setAntiAlias(true);
      this.paint.setDither(true);
    }
 //should be called from constructor(anywhere else before invalidating View) setWillNotDraw(false) then only onDraw of View will be called.
 protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(200,20,300,400,paint);
    }
}

这是android实例化画布的方式:Source Code

        Canvas canvas;
        if (attachInfo != null) {
            canvas = attachInfo.mCanvas;
            if (canvas == null) {
                canvas = new Canvas();
            }
            canvas.setBitmap(bitmap);
            // Temporarily clobber the cached Canvas in case one of our children
            // is also using a drawing cache. Without this, the children would
            // steal the canvas by attaching their own bitmap to it and bad, bad
            // thing would happen (invisible views, corrupted drawings, etc.)
            attachInfo.mCanvas = null;
        } else {
            // This case should hopefully never or seldom happen
            canvas = new Canvas(bitmap);
        }
        if (clear) {
            bitmap.eraseColor(drawingCacheBackgroundColor);
        }
        computeScroll();
        final int restoreCount = canvas.save();
        if (autoScale && scalingRequired) {
            final float scale = attachInfo.mApplicationScale;
            canvas.scale(scale, scale);
        }
        canvas.translate(-mScrollX, -mScrollY);
        mPrivateFlags |= PFLAG_DRAWN;
        if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
                mLayerType != LAYER_TYPE_NONE) {
            mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
        }
        // Fast path for layouts with no backgrounds
        if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
            mPrivateFlags &= ~PFLAG_DIRTY_MASK;
            dispatchDraw(canvas);
            drawAutofilledHighlight(canvas);
            if (mOverlay != null && !mOverlay.isEmpty()) {
                mOverlay.getOverlayView().draw(canvas);
            }
        } else {
            draw(canvas);
        }
        canvas.restoreToCount(restoreCount);
        canvas.setBitmap(null);
        if (attachInfo != null) {
            // Restore the cached Canvas for our siblings
            attachInfo.mCanvas = canvas;
        }