嗨,我是 android 开发的初学者。我正在开发一种类似油漆的应用程序。我已经在画布上画了一些点。现在我想通过手指在这些点之间画线。我不知道我该怎么做。我没有使用谷歌地图。
package com.example.point;
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.graphics.PointF;
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";
List<Point> points = new ArrayList<Point>();
List<Point> points1 = new ArrayList<Point>();
List<Point> points2 = new ArrayList<Point>();
Paint paint = new Paint();
PointF firstpoint=new PointF(100,100);
PointF secondpoint=new PointF(200,200);
Path path=new Path();
public DrawView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
}
@Override
public void onDraw(Canvas canvas) {
paint.setStrokeWidth(3);
canvas.drawPoint(100, 100, paint);
canvas.drawPoint(200, 200, paint);
canvas.drawPoint(200, 400, paint);
Point p=new Point();
Point p1=new Point();
//p1=points.get(1);
for(int i=points.size()-2;i<points.size();i++)
{
p=points.get(i);
//if(p1.x==100 && p1.y==100)
//canvas.drawLine(100, 100, p.x, p.y, paint);
// path.moveTo(100, 100);
//path.lineTo(200, 200);
//canvas.drawPath(path, paint);
}
}
public boolean onTouch(View view, MotionEvent event) {
/*if(event.getAction()==MotionEvent.ACTION_MOVE)
{
Point point1 = new Point();
point1.x = event.getX();
point1.y = event.getY();
points.add(point1);
invalidate();
Log.d(TAG, "point: " + point1);
}*/
//if(event.getX()==100 && event.getY()==100)
//{
if(event.getAction()==MotionEvent.ACTION_MOVE)
{
Point point1 = new Point();
point1.x = event.getX();
point1.y = event.getY();
points.add(point1);
invalidate();
Log.d(TAG, "point: " + point1);
//}
}
//return false;
return true;
/*switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
points.add(point);
invalidate();
Log.d(TAG, "point: " + point);
return true;
case MotionEvent.ACTION_MOVE:
Point point1 = new Point();
point1.x = event.getX();
point1.y = event.getY();
points1.add(point1);
invalidate();
Log.d(TAG, "point: " + point1);
return true;
case MotionEvent.ACTION_UP:
Point point2 = new Point();
point2.x = event.getX();
point2.y = event.getY();
points2.add(point2);
invalidate();
Log.d(TAG, "point: " + point2);
return true;
}
return false;
// return true;*/
}
}
class Point {
float x, y;
@Override
public String toString() {
return x + ", " + y;
}
}
我想用手指在这些点之间画一条直线。如果我的手指远离那个特定点,然后我向后移动我的手指,那么画的线就会被抹去。即仅在这些特定点之间绘制的线。