I have created a path objects as shown below which draws a different shapes.Different buttons response to drawing different shapes on the canvas. I would like to shift the path objects that i have created in the canvas but I do not know how to.
I only know the method of implementing ontouchlistener on a bitmap and not on path objects.
my codes are as follows:
ArrayList<Path> paths = new ArrayList<Path>();
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
paintColor.setColor(Color.RED);
paintColor.setStrokeWidth(2);
paintColor.setStyle(Paint.Style.STROKE);
if (MainActivity.isRectangle) {
Path path = new Path();
path.moveTo(1, 1);
path.lineTo(90, 1);
path.lineTo(90, 60);
path.lineTo(1, 60);
path.close();
paths.add(path);
}
for (Path p : paths) {
canvas.drawPath(p, paintColor );
}
canvas.drawPath(path, paintColor);
invalidate();
}
//mainActivity
rectbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
isTriangle = false;
isRectangle = true;
isCircle= false;
isParallelogram = false;
isTapezium = false;
dv.invalidate();
}// onclick
});
onTouchEvent
@Override
public boolean onTouch(View v,MotionEvent event){
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//screen touch get x of the touch event
x = event.getX();
//screen touch get y of the touch event
y =event.getY();
for (Path p : paths) {
path.moveTo(x,y);
}
dv.invalidate();
break;
case MotionEvent.ACTION_UP:
//screen touch get x of the touch event
x = event.getX();
//screen touch get y of the touch event
y =event.getY();
break;
case MotionEvent.ACTION_MOVE:
//screen touch get x of the touch event
x = event.getX();
//screen touch get y of the touch event
y =event.getY();
break;
}
return true;
}
Please advice. Thank you.
There is no onTouchListener on path object.However, Follow the steps to achive this functionality
1.) override onTouchEvent() method to Find the coordinates you have touched. This link may help. http://developer.android.com/training/graphics/opengl/touch.html
2.) create a RectF boundsRect and for each path object store its boundary coordinates by using
method and simultaneously check whether the touch coordinates lie in the rectF boundsRect(using bounds.contains(x,y)) in a loop in the above method.
3.)select that path and do desired operation on it now.
Edited Code