Question

In order to access canvas outside of onDraw() I do this:

 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;
}


And after that I use it:

 private class myScaleListener extends SimpleOnScaleGestureListener {

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


Is there a better way to do this?

When I call canvas.setMatrix(matrix) from onDraw() the drawing is being shifted.

Thanks in advance!!!

Was it helpful?

Solution

I think if you switch some things around, it'll look better:

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);
    }
  }
}

See if it works, I think it should.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top