Java 绘制自定义圆弧视图并检测每个圆弧的用户单击
我正在创建自定义圆弧视图,这类似于彩虹视图。我可以绘制圆弧视图,但无法为每个视图创建单独的单击事件。如何为每个弧形视图设置单独的单击事件?。提前谢谢 代码如下: ArcView.javaJava 绘制自定义圆弧视图并检测每个圆弧的用户单击,java,android,android-layout,click,drawing,Java,Android,Android Layout,Click,Drawing,我正在创建自定义圆弧视图,这类似于彩虹视图。我可以绘制圆弧视图,但无法为每个视图创建单独的单击事件。如何为每个弧形视图设置单独的单击事件?。提前谢谢 代码如下: ArcView.java public class ArcView extends View implements View.OnTouchListener{ Paint paint; int radius, x, y; int color; public ArcView(Context conte
public class ArcView extends View implements View.OnTouchListener{
Paint paint;
int radius, x, y;
int color;
public ArcView(Context context) {
super(context);
init();
}
public ArcView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ArcView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public ArcView(Context context, int radius, int x, int y, int color) {
super(context);
this.radius = radius;
this.x = x;
this.y = y;
this.color = color;
init();
}
private void init(){
paint = new Paint();
paint.setColor(color);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.STROKE);
setOnTouchListener(this);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(x, y, radius, paint);
}
@Override
public boolean onTouch(View view, MotionEvent event) {
return false;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
RelativeLayout arcButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arcButton = (RelativeLayout) findViewById(R.id.arcButton);
arcButton1 = (RelativeLayout) findViewById(R.id.arcButton1);
arcButton2 = (RelativeLayout) findViewById(R.id.arcButton2);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int arcRadius1 = (int)(width/1.5);
int arcRadius2 = arcRadius1+(int)(width/3.5);
int arcRadius3 = arcRadius2+(int)(width/3.5);
int xCoor = width / 2;
int yCoor = height;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
arcButton.setLayoutParams(params);
View arcView1=new ArcView(this, arcRadius3, xCoor, yCoor, Color.RED);
View arcView2=new ArcView(this, arcRadius2, xCoor, yCoor, Color.BLACK);
View arcView3=new ArcView(this, arcRadius1, xCoor, yCoor, Color.BLUE);
arcButton.addView(arcView1);
arcButton1.addView(arcView2);
arcButton2.addView(arcView3);
}
}
输出:
如何为每个圆弧按钮创建单独的单击事件?这是一种解决方法,但它可以工作。将其放入ArcView中:
@Override
public boolean performClick() {
return super.performClick();
}
@Override
public boolean onTouch(View view, MotionEvent event) {
view.performClick();
if (isPointInCircle((int) event.getX(), (int) event.getY())) {
// do what you want to do and get rid of the next two lines
String color = (((ArcView) view).color == Color.RED) ? "RED" : (((ArcView) view).color == Color.BLACK) ? "BLACK" : "BLUE";
Toast.makeText(context, color, Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
private boolean isPointInCircle(int clickX, int clickY) {
return (clickX - x) * (clickX - x) + (clickY - y) * (clickY - y) <= radius * radius;
}
@覆盖
公共布尔performClick(){
返回super.performClick();
}
@凌驾
公共布尔onTouch(视图、运动事件){
view.performClick();
if(isPointInCircle((int)event.getX(),(int)event.getY()){
//做你想做的事,摆脱接下来的两行
字符串颜色=((ArcView)视图)。颜色==颜色.红色)?“红色”:((ArcView)视图)。颜色==颜色.黑色)?“黑色”:“蓝色”;
Toast.makeText(上下文、颜色、Toast.LENGTH_SHORT).show();
返回true;
}
返回false;
}
专用布尔值isPointInCircle(整数单击X,整数单击Y){
返回(clickX-x)*(clickX-x)+(clickY-y)*(clickY-y)您不能这样做-而是创建一个具有多个“环”的自定义视图-每个都有自己的click listener@pskink感谢您的快速回复。我将尝试此操作。我尝试分别创建三个环,并创建了每个click listener,但它将使用最后一个视图的click事件。@VigneshK为什么不能为每个视图添加click listener?