试图绘制自定义视图如何检测单击了哪个切片

问题描述 投票:0回答:1

我正在创建自定义视图,如下图所示。我可以绘制视图,但是无法为每个视图创建单独的单击事件。如何为每个弧形视图设置单独的单击事件?预先感谢。

这里是代码:

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类以检测高度和宽度并转换为px

我想等式检查我单击哪个圆圈

this custom view that i want to detect slice click

java android android-custom-view
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.