オンタッチ方式で線を引こうとしています。
ユーザーが特定のポイントに触れてから指をドラッグすると、線が引かれることを望んでいます。
使用canvas.drawLine(_,_,_,_,_ );
は線を引くことではありません。一度だけonDraw
動作します。
私の主な質問は、「実行時に使用せずに線を引く方法onDraw()
」です。
編集 :
public class DrawView extends View {
int radius=30;
int initialX =0;
int initialY=0;
int finalX=0;
int finalY=0;
private Point currentPoint;
private int index;
static ArrayList<Point> pointListDrawView = new ArrayList<Point>();
public DrawView(Context context, ArrayList<Point> pointList) {
super(context);
// TODO Auto-generated constructor stub
this.setBackgroundColor(Color.WHITE);
//pointListDrawView = pointList;
pointListDrawView.add(pt(600,200));
pointListDrawView.add(pt(500,200));
pointListDrawView.add(pt(400,200));
pointListDrawView.add(pt(400,300));
pointListDrawView.add(pt(400,400));
pointListDrawView.add(pt(500,400));
pointListDrawView.add(pt(400,400));
pointListDrawView.add(pt(400,500));
pointListDrawView.add(pt(400,600));
pointListDrawView.add(pt(500,600));
pointListDrawView.add(pt(600,600));
}
private Point pt(int i, int j) {
// TODO Auto-generated method stub
Point P = new Point(i, j);
return P ;
}
Paint paint= new Paint();
Canvas universalCanvas;
@Override
protected void onDraw(final Canvas canvas) {
paint.setColor(Color.GREEN);
paint.setStrokeWidth(10);
for(int i=0; i<pointListDrawView.size();i++){
canvas.drawCircle(pointListDrawView.get(i).x, pointListDrawView.get(i).y, 15, paint);
}
universalCanvas = canvas;
// drawLines(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int i =0;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if(((event.getX()>pointListDrawView.get(i).x-radius) && (event.getX()<pointListDrawView.get(i).x+radius) && (event.getY()>pointListDrawView.get(i).y-radius) && (event.getY()<pointListDrawView.get(i).y+radius)))
{
Log.d("touch", "touched correct point");
initialX=pointListDrawView.get(i).x;
initialY=pointListDrawView.get(i).y;
}
else{
Log.d("touch", "touch discarded");
return false;
}
break;
case MotionEvent.ACTION_MOVE:
universalCanvas.drawLine(initialX, initialY, event.getX(), event.getY(), paint);
break;
case MotionEvent.ACTION_UP:
if(((event.getX()>pointListDrawView.get(i+1).x-radius) && (event.getX()<pointListDrawView.get(i+1).x+radius) && (event.getY()>pointListDrawView.get(i+1).y-radius) && (event.getY()<pointListDrawView.get(i+1).y+radius)))
{
finalX=pointListDrawView.get(i+1).x;
finalY=pointListDrawView.get(i+1).y;
universalCanvas.drawLine(initialX,initialY, finalX,finalY, paint);
Log.d("touch", "line drawn");
return true;
}
else
{
Log.d("touch", "correct point touched and discarded");
return false;
}
default:
break;
}
invalidate();
return true;
}
}