[英]How make a custom shape button?
I found a solution, I create a polygon by points and by clicking on it when checking. 我找到了一个解决方案,通过点创建多边形,然后在检查时单击该多边形。 whether the coordinate is in a polygon 坐标是否在多边形中
public class CustomFormView extends View {
private ArrayList<Point> points;
public Paint paint;
public CustomFormView(Context context, ArrayList<Point> points) {
super(context);
paint = new Paint();
this.points = points;
}
@Override
protected void onDraw(Canvas canvas) {
//paint.setColor(Color.TRANSPARENT);
paint.setStyle(Paint.Style.STROKE);
Path path = new Path();
path.moveTo(points.get(0).x, points.get(0).y);
for (int i = 1; i < points.size(); i++) {
path.lineTo(points.get(i).x, points.get(i).y);
}
path.close();
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!contains(new Point(event.getX(), event.getY()))) {
return false;
}
}
return super.onTouchEvent(event);
}
public boolean contains(Point test) {
int i;
int j;
boolean result = false;
for (i = 0, j = points.size() - 1; i < points.size(); j = i++) {
if ((points.get(i).y > test.y) != (points.get(j).y > test.y) &&
(test.x < (points.get(j).x - points.get(i).x) * (test.y - points.get(i).y) / (points.get(j).y - points.get(i).y) + points.get(i).x)) {
result = !result;
}
}
return result;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.