Tôi đang tạo chế độ xem vòng cung tùy chỉnh, giống như chế độ xem cầu vồng. Tôi có thể vẽ các chế độ xem vòng cung nhưng tôi không thể tạo các sự kiện nhấp chuột riêng cho từng chế độ xem. Cách đặt các sự kiện nhấp chuột riêng biệt cho mỗi chế độ xem vòng cung ?. Cảm ơn trước.Vẽ chế độ xem vòng cung tùy chỉnh và phát hiện nhấp chuột của người dùng cho mỗi vòng cung
Đây là mã:
ArcView.java
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);
}
}
Output:
Làm cách nào để tạo sự kiện nhấp riêng biệt cho mỗi nút vòng cung?
bạn không thể làm điều đó - thay vì làm một tùy chỉnh 'View' rằng có một số "nhẫn" - mỗi người có riêng nhấp chuột nghe – pskink
@ Cảm ơn pskink trả lời nhanh chóng của bạn. Tôi sẽ thử cái này. – VigneshK
Tôi đã cố gắng tạo ba vòng riêng biệt và tạo từng trình nghe nhấp chuột nhưng sẽ có sự kiện nhấp của chế độ xem cuối cùng. – VigneshK