Java 在不删除最后一个对象的情况下重新创建对象
我正在创建一个允许用户在屏幕上绘图的应用程序。我的代码实际上创建了一条路径,然后创建了一条线。但是,当用户用手指触摸屏幕并转到屏幕的另一部分时,它会在下一个视频中画出一条线。 以下代码是drawview的代码Java 在不删除最后一个对象的情况下重新创建对象,java,android,android-canvas,ondraw,Java,Android,Android Canvas,Ondraw,我正在创建一个允许用户在屏幕上绘图的应用程序。我的代码实际上创建了一条路径,然后创建了一条线。但是,当用户用手指触摸屏幕并转到屏幕的另一部分时,它会在下一个视频中画出一条线。 以下代码是drawview的代码 public class DrawView extends View implements OnTouchListener { private static final String TAG = "DrawView"; List<Point> points = new Ar
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;
}
}
公共类DrawView扩展视图实现OnTouchListener{
私有静态最终字符串标记=“DrawView”;
列表点=新的ArrayList();
油漆=新油漆();
内联作用;
int-draw=0;
布尔指=真;
公共绘图视图(上下文){
超级(上下文);
设置聚焦(真);
setFocusableInTouchMode(真);
this.setOnTouchListener(this);
油漆。设置颜色(颜色。黑色);
绘制.设置样式(绘制.样式.笔划);
油漆。设置行程宽度(5);
paint.setAntiAlias(真);
}
@凌驾
受保护的void onDraw(画布){
//TODO自动生成的方法存根
路径路径=新路径();
布尔值优先=真;
对于(int i=0;i
另外,我想我需要的可能是一个包含对象(路径和线)的数组,但我不知道怎么做。使用此方法,我想我将能够返回删除最后一个对象。这可能吗?每次用户抬起手指时,您需要创建不同的“路径”。我已经更改了你的代码来实现这一点。试试看你是否需要它
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;
}
}
}
import java.util.ArrayList;
导入java.util.List;
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.graphics.Path;
导入android.util.Log;
导入android.view.MotionEvent;
导入android.view.view;
导入android.view.view.OnTouchListener;
公共类DrawView扩展了视图实现OnTouchListener{
私有静态最终字符串标记=“DrawView”;
私有列表_path=new ArrayList();
私有列表_lastPath;
私人油漆_油漆=新油漆();
私有路径_Path=新路径();
公共绘图视图(上下文){
超级(上下文);
设置聚焦(真);
setFocusableInTouchMode(真);
setOnTouchListener(这个);
_油漆。设置颜色(颜色。黑色);
_绘制.设置样式(绘制.样式.笔划);
_油漆。设置行程宽度(5);
_paint.setAntiAlias(真);
}
@凌驾
受保护的void onDraw(画布){
对于(列出点路径:_路径){
_path.reset();
布尔值优先=真;
对于(int i=0;i