简体   繁体   中英

Trying to draw custom view How to Detect which slice is clicked

I am creating custom view, which is like screen below. I can draw views but I am unable to create separate click events for each view. How to set separate click events for each arc view?. Thanks in advance.

Here is the code:

ArcView.java

public class ItemView extends View {

Utils utils;

int left, right, top, bottom;

private int color;
private int start;
private int sweep;
private boolean inner;
private RectF oval;
public float center_x, center_y;
int divOn;
public float radius;

public ItemView(Context context, int color, int start, int sweep) {
    super(context);
    this.color = color;
    this.start = start;
    this.sweep = sweep;
    utils = Utils.getInstance(getContext());
}

public ItemView(Context context, int color, int start, int sweep, boolean inner, int divOn) {
    super(context);
    this.color = color;
    this.start = start;
    this.sweep = sweep;
    this.inner = inner;
    this.divOn = divOn;
    utils = Utils.getInstance(getContext());
}

public RectF getOval() {
    return oval;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //There is another flag for inner white circle
    if (inner) {
        float width = (float) getWidth();
        float height = utils.getScreenHeight() - utils.getActionBarSize();

        radius = (width - utils.dpTopixel(50)) / divOn;




        Path path = new Path();
        path.addCircle((width - utils.dpTopixel(50)) / divOn,
                (height - utils.dpTopixel(50)) / divOn, radius,
                Path.Direction.CW);

        Paint paint = new Paint();
        paint.setColor(ContextCompat.getColor(getContext(), color));
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);


        final RectF oval = new RectF();
        paint.setStyle(Paint.Style.FILL);


        left = -(int) radius;
        right = (int) radius;
        top = (getHeight() / 2) - (int) radius;
        bottom = (getHeight() / 2) + (int) radius;

        oval.set(left,
                top,
                right,
                bottom);
        canvas.drawArc(oval, start, sweep, true, paint);
    } else {

        float width = (float) getWidth();
        float height = utils.getScreenHeight() - utils.getActionBarSize();
        float radius;
        radius = (width - utils.dpTopixel(50)) / divOn;

        Path path = new Path();
        path.addCircle((width - utils.dpTopixel(50)) / 1,
                (width - utils.dpTopixel(50)) / 1, radius,
                Path.Direction.CW);

        Paint paint = new Paint();
        paint.setColor(ContextCompat.getColor(getContext(), color));
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        oval = new RectF();
        paint.setStyle(Paint.Style.FILL);



        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
        textPaint.setColor(ContextCompat.getColor(getContext(), R.color.white));
        textPaint.setTextAlign(Paint.Align.CENTER);

        /*Paint.FontMetrics metrics = paint.getFontMetrics();
        float textheight = Math.abs(metrics.top - metrics.bottom);
        float x = getWidth() / 2;
        float y = (getHeight() / 2) + (height / 2);

        canvas.drawText("My Text", x, y, paint);*/





        left = -(int) radius;
        right = (int) radius;
        top = (getHeight() / 2) - (int) radius;
        bottom = (getHeight() / 2) + (int) radius;

        center_x = width / 2;
        center_y = utils.getOffset();

        int xPos = (getWidth() / 2);
        int yPos = (int) ((getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ;
        //((textPaint.descent() + textPaint.ascent()) / 2) is the distance from the baseline to the center.



        oval.set(left,
                top,
                right,
                bottom);
        canvas.drawArc(oval, start, sweep, true, paint);





    }
}

}

utils class to detect height and width and convert to px

i want equation to check i click which circle

我想检测切片点击的这个自定义视图

Create a slice selected listener, override the onTouchEvent in your custom class, calculate which slice the touch event (with the action ACTION_UP) is on using the x,y values. You can then call the function from your slice selected listener and pass details about the selected slice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM