android在画布上绘图。访问onDraw外部的画布

时间:2014-06-04 07:41:33

标签: android android-canvas zooming pinchzoom android-bitmap

为了访问onDraw()之外的画布,我这样做:

 private Canvas canvas;


 public MyView(Context context, Paint paint) {
     super(context);

     canvas = new Canvas();
 }

 @Override
 protected final void onDraw(Canvas canvas) {
     canvas.save();

     this.canvas = canvas;
}


然后我用它:

 private class myScaleListener extends SimpleOnScaleGestureListener {

     @Override
     public boolean onScale(ScaleGestureDetector detector) {
        canvas.setMatrix(matrix);
     }
 }


有更好的方法吗?

当我从canvas.setMatrix(matrix)致电onDraw()时,图纸正在转移。

提前致谢!!!

1 个答案:

答案 0 :(得分:0)

我认为如果你换掉一些东西,它看起来会更好:

private Canvas canvas;

public MyView(Context context, Paint paint) {
    super(context);
    canvas = null;
}

@Override
protected final void onDraw(Canvas canvas) {
    this.canvas = canvas;
    canvas.save();
}

private class myScaleListener extends SimpleOnScaleGestureListener {<br/>

  @Override
  public boolean onScale(ScaleGestureDetector detector) {
    if(canvas != null) {
       canvas.setMatrix(matrix);
    }
  }
}

看看它是否有效,我认为应该。