0

I'm currently experimenting with my first custom View in android and have been trying to draw points on a canvas via a onTouchEvent but have failed after several attempts. The View does detect my touches and successfully prints out a System.out.println message when touched however it still doesn't draw on the canvas.

After several various attempts this is what I came up with:

package com.techdigy.testapp; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class DrawingBoard extends View { Canvas canvas; Bitmap bmp; BitmapDrawable temp; public DrawingBoard(Context context, AttributeSet attributeSet) { super(context,attributeSet); // TODO Auto-generated constructor stub } protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); canvas = new Canvas(); } protected void onDraw(Canvas canvas) { //draw view } public boolean onTouchEvent(MotionEvent event) { //detect user touch float touchX = event.getX(); float touchY = event.getY(); Paint paint = new Paint(); System.out.println("test"); this.canvas.drawPoint(touchX, touchY, paint); temp = new BitmapDrawable(this.bmp); this.setBackground(this.temp); invalidate(); return true; } } 

1 Answer 1

1

try this...

public class DrawingBoard extends View { private Bitmap bmp; private float touchX; private float touchY; private Paint paint; public DrawingBoard(Context context, AttributeSet attributeSet) { super(context, attributeSet); paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.GREEN); paint.setStyle(Paint.Style.STROKE); } protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); setBackgroundDrawable(new BitmapDrawable(getResources(), bmp)); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawPoint(touchX, touchY, paint); } public boolean onTouchEvent(MotionEvent event) { // detect user touch touchX = event.getX(); touchY = event.getY(); System.out.println("test"); invalidate(); return true; } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help. This works, but only temporarily holds the value and when you click somewhere else it gets moved. Also any idea on what I did wrong in the original code?
you are drawing points on Canvas that you have created. this canvas has no relation with the View. when you want to draw anything, draw on the canvas that you get as a parameter in onDraw() method

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.