Update canvas

I created a view in this view, I want to display this field as after receiving a pixel where the user touches the screen, but the onDraw method does not update the canvas when the screen is touched. Can you guys help me?

public class PlayView extends View { float width,height; float touchatX, touchatY; boolean isanyBox; public void init() { isanyBox = false; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { // TODO Auto-generated method stub width = w/6f; height = h/6f; super.onSizeChanged(w, h, oldw, oldh); } public PlayView(Context context) { super(context); setFocusable(true); setFocusableInTouchMode(true); init(); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub isanyBox = true; touchatX = (event.getX()/6)*6; touchatY = (event.getY()/6)*6; return super.onTouchEvent(event); } public void onDraw(Canvas canvas) { Paint lineColor = new Paint(); lineColor.setColor(Color.BLACK); //Box property Paint boxColor = new Paint(); boxColor.setColor(Color.BLUE); //Draw horizontal lines for(int i=0; i<6; i++) { canvas.drawLine(0, i*height, getWidth(), i*height, lineColor); } //Draw vertical lines for(int j=0; j<6; j++) { canvas.drawLine(j*width, 0, j*width, getHeight(), lineColor); } // if(isanyBox) // { canvas.drawRect(touchatX+1, touchatY+2, touchatX+width-1, touchatY+height-2, boxColor); // } } } 
+4
source share
1 answer

Put invalidate (); in onTouchEvent,

 @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub isanyBox = true; touchatX = (event.getX() / 6) * 6; touchatY = (event.getY() / 6) * 6; invalidate(); return super.onTouchEvent(event); } 
+2
source

All Articles