android - 如何制作自定义形状按钮?
解决方案
我找到了一个解决方案,我通过点创建一个多边形,并在检查时单击它。坐标是否在多边形中
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;
}
}
推荐阅读
- batch-file - 如何使用cmd合并多个tmp文件的前N个单词
- node.js - Google Datastore 节点查询 JS 中的 Order By 和 Group By
- javascript - 向下滚动向右滚动,向上滚动向左
- java - 使用 myBatis 从 DB 中读取 JSON
- mysql - 为什么'select * from Employee group by Id'比直接使用Employee表要快得多?
- angular - Angular (2+) 组件提供对投影内容的绑定
- ruby-on-rails - 在另一个目录中运行 rake 任务
- asp.net-core-mvc - 远程验证因 IFormFile 属性而失败
- r - 如何将标题与换行符左对齐?
- amazon-s3 - 如何重定向仅为下载创建的 Amazon S3 存储桶的根?