Skip to main content
edited tags
Link
Kara
  • 6.2k
  • 16
  • 54
  • 58
Source Link
Andre Yonadam
  • 1k
  • 1
  • 15
  • 30

Android view doesn't respond to draw

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