Android 在路径对象上实现 onTouchListener [英] Android Implement onTouchListener on path objects
问题描述
我创建了一个路径对象,如下所示,它绘制不同的形状.不同的按钮响应在画布上绘制不同的形状.我想移动我在画布中创建的路径对象,但我不知道如何.
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.
我只知道在位图上而不是在路径对象上实现ontouchlistener的方法.
I only know the method of implementing ontouchlistener on a bitmap and not on path objects.
我的代码如下:
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
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;
}
请指教.谢谢.
推荐答案
path对象上没有onTouchListener.但是,按照步骤来实现这个功能
There is no onTouchListener on path object.However, Follow the steps to achive this functionality
1.) 覆盖 onTouchEvent() 方法以查找您触摸的坐标.此链接可能会有所帮助.http://developer.android.com/training/graphics/opengl/touch.html
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.) 创建一个 RectF boundsRect 并使用
2.) create a RectF boundsRect and for each path object store its boundary coordinates by using
Path.getBounds(boundsRect);
方法,同时在上述方法中循环检查触摸坐标是否在rectF boundsRect(using bounds.contains(x,y))中.
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.) 选择该路径并立即对其进行所需的操作.
3.)select that path and do desired operation on it now.
编辑代码
@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();
//check if touch point intersects the path bounds
for (Path p : paths) {
RectF pBounds = new RectF();
p.computeBounds(pBounds,true);
if(pBounds.contains(x,y)){
//select path
selected Path = p;// where selectedPath is assumed declared.
break;
}
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;
}
这篇关于Android 在路径对象上实现 onTouchListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!