MPAndroidChart MarkerView 点击事件

一直以来,图标类UI都用MPAndroidChart这个库,但是今天今天感觉不好了,想实现一个标签,并可点击,我以为这玩意肯定支持,结果在库里找了好久,发现没有,然后我的第一想法就是,这玩意要重写touch,然后处理一些逻辑。 but,我还是到源码里找了一圈还是木有,想这个需求应该不是很古怪,就到issues里see see,果然有!!!Make MarkerView Click
不出我所料。

  1. 继承LineChart(其他chart也可),转移touch事件
public class MyChart extends LineChart {
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean handled = true;
        // if there is no marker view or drawing marker is disabled
        if (isShowingMarker() && this.getMarker() instanceof ChartInfoMarkerView){
        // 
            ChartMarkerView markerView = (ChartMarkerView) this.getMarker();
            Rect rect = new Rect((int)markerView.drawingPosX,(int)markerView.drawingPosY,(int)markerView.drawingPosX + markerView.getWidth(), (int)markerView.drawingPosY + markerView.getHeight());
            if (rect.contains((int) event.getX(),(int) event.getY())) {
                // touch on marker -> dispatch touch event in to marker
                markerView.dispatchTouchEvent(event);
            }else{
                handled = super.onTouchEvent(event);
            }
        }else{
            handled = super.onTouchEvent(event);
        }
        return handled;
    }

    private boolean isShowingMarker(){
        return mMarker != null && isDrawMarkersEnabled() && valuesToHighlight();
    }
}
  1. 自定义MarkerView,接管点击事件
public class ChartMarkerView extends MarkerView {
    @BindView(R.id.markerview_root)
    LinearLayout markerContainerView;
    
    protected float drawingPosX;
    protected float drawingPosY;
    private static final int MAX_CLICK_DURATION = 500;
    private long startClickTime;
	private MarkerViewClickCB cb;

    public void setMarkerViewClickCB(MarkerViewClickCB cb) {
        this.cb = cb;
    }
    public ChartInfoMarkerView(Context context, int layoutResource) {
        super(context, layoutResource);
        ButterKnife.bind(this);
        markerContainerView.setClickable(true);
        markerContainerView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cb != null) cb.onMarkerViewClick();
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                startClickTime = Calendar.getInstance().getTimeInMillis();
                break;
            }
            case MotionEvent.ACTION_UP: {
                long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                if(clickDuration < MAX_CLICK_DURATION) {
                    markerContainerView.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (cb != null) cb.onMarkerViewClick();
                        }
                    });
                }
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void draw(Canvas canvas, float posX, float posY) {
        super.draw(canvas, posX, posY);
        MPPointF offset = getOffsetForDrawingAtPoint(posX, posY);
        this.drawingPosX = posX + offset.x;
        this.drawingPosY = posY + offset.y;
    }
}
  1. 自定义接口,回调点击事件到合适的地方
public interface MarkerViewClickCB {
    void onMarkerViewClick();
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值