Recreate an object without deleting the last one

徘徊边缘 提交于 2019-12-11 14:10:13

问题


I'm creating an app that allows user to draw on the screen. My code actually creates a Path and next creates a line. But this when the user takes up his finger of the screen and goes to another part of the screen it make a line as it happen in the next video. http://youtu.be/CBv1wtUC2g4

The following code is the code of the drawview.

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

Also I think that maybe what i want I need is an array that contains objects (paths and lines) but i don't know how to do it. Using this method i think that i'm will be able to go back deleting the last object can be this possible?


回答1:


You need to create a different "Path" each time the user uplifts the finger. I've changed your code to make that. Try it to see if it's 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;
        }
    }
}


来源:https://stackoverflow.com/questions/15987703/recreate-an-object-without-deleting-the-last-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!