0

I am unable to get the canvas object in ontouch().Without the canvas I cannot draw a circle when touched.How can I draw any shape or image when touched

public class Board extends View implements View.OnTouchListener {

public Board(Context context) {
    super(context);
    Paint paint1 = new Paint();
    paint1.setTextSize(50);
    paint1.setColor(Color.WHITE);

    View view=this;
    view.setOnTouchListener(this);    
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawRGB(200, 100, 0);

}

@Override
public boolean onTouch(View v, MotionEvent event) {
    final int action = MotionEventCompat.getActionMasked(event);
    int pointer = MotionEventCompat.getActionIndex(event);


    if (action == MotionEvent.ACTION_DOWN) {

         canvas.drawCircle(70, 1100, 50, paint1);

    }

    return false;
}
Ganesh
  • 1
  • 2
  • `Canvas` is not a local or global object so that you can't access it from `onTouch()` event. Either create the Canvas instance on this method OR use Global. – Riad Jul 12 '16 at 07:22
  • Possible duplicate of [Draw Circle on touch](http://stackoverflow.com/questions/11796357/draw-circle-on-touch) – Bö macht Blau Jul 12 '16 at 07:31

1 Answers1

0

To draw on canvas wherever you touch, you need a path to keep track of your touch points and path. Using the path object you can draw on canvas. See this answer Draw on touch

Community
  • 1
  • 1
Sujith Niraikulathan
  • 2,111
  • 18
  • 21