重新创建一个对象,但不删除最后一个 [英] Recreate an object without deleting the last one
问题描述
我创建一个应用程序,允许用户在屏幕上绘制。我的code实际上创建了一个路径和明年创建一个行。但此,当用户占用他的手指在屏幕的,然后进到屏幕的另一部分它作出其发生在下一视频行。 http://youtu.be/CBv1wtUC2g4
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
下面code是drawView函数的code。
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?
推荐答案
您需要创建一个不同的路径每个用户隆起手指的时间。我更改了自己的code键使这一点。试试吧,看它是否是你所需要的。
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;
}
}
}
这篇关于重新创建一个对象,但不删除最后一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!