Recover an object without deleting the last

I am creating an application that allows the user to draw on the screen. My code actually creates a Path and then creates a string. But this, when the user lifts a finger on the screen and moves to another part of the screen, he draws a line, as happens in the next video. http://youtu.be/CBv1wtUC2g4

The following code is drawview code.

public class DrawView extends View implements OnTouchListener { private static final String TAG = "DrawView"; List<Point> points = new ArrayList<Point>(); Paint paint = new Paint(); int action; int draw=0; Boolean finger=true; public DrawView(Context context) { super(context); setFocusable(true); setFocusableInTouchMode(true); this.setOnTouchListener(this); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); paint.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub Path path = new Path(); boolean first = true; for(int i = 0; i < points.size(); i += 2){ Point point = points.get(i); if(first){ first = false; path.moveTo(point.x, point.y); } else if(i < points.size() - 1){ Point next = points.get(i + 1); path.quadTo(point.x, point.y, next.x, next.y); } else { path.lineTo(point.x, point.y); } } canvas.drawPath(path, paint); } public boolean onTouch(View view, MotionEvent event) { action = event.getAction(); Point point = new Point(); point.x = event.getX(); point.y = event.getY(); points.add(point); invalidate(); Log.d(TAG, "point: " + point); if (action == MotionEvent.ACTION_MOVE) { finger = true; } if (action == MotionEvent.ACTION_UP) { finger = false; } draw++; return true; } } class Point { float x, y; @Override public String toString() { return x + ", " + y; } } 

I also think that maybe I want it to be an array containing objects (paths and strings), but I don't know how to do it. Using this method, I think I can return to deleting the last object, maybe this is possible?

+1
source share
1 answer

You need to create a different "Path" every time the user raises his finger. I changed my code to do this. Try to find out what you need.

 import java.util.ArrayList; import java.util.List; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; public class DrawView extends View implements OnTouchListener { private static final String TAG = "DrawView"; private List<List<Point>> _paths = new ArrayList<List<Point>>(); private List<Point> _lastPath; private Paint _paint = new Paint(); private Path _path = new Path(); public DrawView(Context context) { super(context); setFocusable(true); setFocusableInTouchMode(true); setOnTouchListener(this); _paint.setColor(Color.BLACK); _paint.setStyle(Paint.Style.STROKE); _paint.setStrokeWidth(5); _paint.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { for (List<Point> pointsPath : _paths) { _path.reset(); boolean first = true; for (int i = 0; i < pointsPath.size(); i += 2) { Point point = pointsPath.get(i); if (first) { first = false; _path.moveTo(point.x, point.y); } else if (i < pointsPath.size() - 1) { Point next = pointsPath.get(i + 1); _path.quadTo(point.x, point.y, next.x, next.y); } else { _path.lineTo(point.x, point.y); } } canvas.drawPath(_path, _paint); } } public boolean onTouch(View view, MotionEvent event) { Point point = new Point(); point.x = event.getX(); point.y = event.getY(); Log.d(TAG, "point: " + point); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: _lastPath = new ArrayList<Point>(); _lastPath.add(point); _paths.add(_lastPath); break; case MotionEvent.ACTION_MOVE: _lastPath.add(point); break; } invalidate(); return true; } private class Point { float x, y; @Override public String toString() { return x + ", " + y; } } } 
+3
source

All Articles