Draw custom arc views and detect user click for each arc

后端 未结 1 2019
借酒劲吻你
借酒劲吻你 2021-02-20 08:29

I am creating custom arc views, which is like rainbow views. I can draw arc views but I am unable to create separate click events for each view. How to set separate click events

1条回答
  •  [愿得一人]
    2021-02-20 08:57

    A bit of a workaround, but it works. Put this inside 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;
    }
    

    Note that for the Toast to work, you need to make context global, but most likely you will want to get rid of the Toast anyway.

    0 讨论(0)
提交回复
热议问题