[英]how to get position(x and y coordinates) of circle drwan on canvas in android?
我在canvas android中有两个疑问。
解释如下:
首先,我在画布上绘制了一行圆,我想捕获绘制的圆的x和y坐标(仅在一个圆上),以便可以在圆上绘制一个位图(在中心)。
其次,我想暗示触摸事件,即我希望它们在有人触摸时改变颜色。有人可以帮助我吗?
对于#2:计算您的点的中心和触摸事件之间的距离-如果该距离小于圆的半径-您在圆上按了
首先,您应该为“圆形区域”创建GridSector类。
public class GridSector {
private int x = 0;
private int y = 0;
private Rect rect = new Rect(0, 0, 0, 0);
public GridSector(int x, int y, Rect rect) {
this.x = x;
this.y = y;
this.rect = rect;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Rect getRect() {
return rect;
}
public void setRect(Rect rect) {
this.rect.set(rect.left, rect.top, rect.right, rect.bottom);
}
}
然后创建您想触摸的视图。
public class GridSectorsView extends View {
GridSector currentSelectedSector;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawCircle(canvas); // draw your circles;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
invalidate(currentSelectedSector.getRect()); // update your circles (call onDraw Function ) ;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.