繁体   English   中英

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

[英]Trying to draw custom view How to Detect which slice is clicked

我正在创建自定义视图,就像下面的屏幕。 我可以绘制视图,但无法为每个视图创建单独的点击事件。 如何为每个圆弧视图设置单独的点击事件?。 提前致谢。

这是代码:

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

我想要等式来检查我点击哪个圆圈

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

创建切片选择监听器,覆盖onTouchEvent在自定义类,这片触摸事件(用行动ACTION_UP)计算是使用x,y值。 然后,您可以从切片选定的侦听器调用该函数并传递有关选定切片的详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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