Yo utilizo este código:
public class DrawView extends View implements OnTouchListener {
List<Point> points = new ArrayList<Point>();
Paint paint = new Paint();
private int Gx=0;
private int Gy=0;
public DrawView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setBackgroundColor(Color.WHITE);
this.setOnTouchListener(this);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(4);
paint.setAntiAlias(true);
}
@Override
public void onDraw(Canvas canvas) {
for (Point point : points) {
if ((Gx==0) && (Gy==0)) {
canvas.drawPoint(point.x, point.y, paint);
} else {
if ((point.x!=0) && (point.y!=0)) {
canvas.drawLine(Gx, Gy, point.x, point.y, paint);}
}
Gx = (int) point.x;
Gy = (int) point.y;
}
Gx = 0;
Gy = 0;
}
public boolean onTouch(View view, MotionEvent event) {
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_DOWN:
Gx = 0;
Gy = 0;
break;
case MotionEvent.ACTION_UP:
Gx = 0;
Gy = 0;
point.x = 0;
point.y = 0;
default:
}
points.add(point);
invalidate();
return true;
}
public void Nueva() {
points.clear();
try {
this.bringToFront();
this.clearFocus();
} catch( Exception e ) {
}
}
}
class Point {
float x, y;
@Override
public String toString() {
return x + ", " + y;
}
}
Con este código llamo a la función.
drawView = new DrawView(this);
setContentView(drawView);
drawView.requestFocus();
Espero que te sirva.
Contenido editorial recomendado
Con su consentimiento, aquí se cargan contenidos externos.
Al hacer clic en el botón anterior, acepta que se le muestren contenidos externos. En el proceso pueden transmitirse datos personales a terceros proveedores. Encontrará más información al respecto en nuestro Política de privacidad.