关于开源图表hellocharts-android的一些使用心得

转载请注明出处:http://blog.csdn.net/binbinqq86/article/details/71698336

项目GitHub地址:https://github.com/lecho/hellocharts-android(ps:本文不适合对hellocharts不了解的人)

关于hellocharts-android简单的使用,作者在GitHub上介绍的很详细,而且网上大把大把的文章介绍其基本使用方式,所以我们在这里就不再说这些简单使用了,今天主要就是记录一下自己使用过程中遇到的一些坑,当然这些问题在GitHub的Issues里面也有很多人提问,但回答却不太让人满意,网上也找不到相关答案,所以只能自己去分析源码来寻求答案了。。。

上图是我们看到的小米天气,对比之下,hellocharts的主要问题:
- 当列表内容很多的时候,怎么进行滚动(默认出来是显示所有数据,很拥挤)
- 当数据跳跃过大的时候,部分曲线的绘制被遮挡不能完全显示
- 有些坐标轴标签显示不出来,当数据过多的时候,只有放大后才能显示
- 同上的问题出现在坐标轴上垂直的分割线,不能显示完全
- 分割线怎么去自定义效果

下面我门就来看看怎么解决这些问题。关于滑动的,其实它内置的就已经写好了滑动逻辑,比如在放大缩小的时候,上下左右滑动很流畅,我们肯定畅想调用一下或者设置一下某个方法就可以实现我们想要的功能了,但是找来找去,始终找不到想要的那个方法或属性,怎么设置都没有用。最后通过源码我发现其实这一切滑动都是跟ViewPort有关系,我们来看下AbstractChartView里面的touch事件:

@Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);

        if (isInteractive) {

            boolean needInvalidate;

            if (isContainerScrollEnabled) {
                needInvalidate = touchHandler.handleTouchEvent(event, getParent(), containerScrollType);
            } else {
                needInvalidate = touchHandler.handleTouchEvent(event);
            }

            if (needInvalidate) {
                ViewCompat.postInvalidateOnAnimation(this);
            }

            return true;
        } else {

            return false;
        }
    }

可以看到,所有的触摸事件都交给了一个touchHandler的类来处理,跟进去看:

/**
     * Handle chart touch event(gestures, clicks). Return true if gesture was handled and chart needs to be
     * invalidated.
     */
    public boolean handleTouchEvent(MotionEvent event) {
        boolean needInvalidate = false;

        // TODO: detectors always return true, use class member needInvalidate instead local variable as workaround.
        // This flag should be computed inside gesture listeners methods to avoid invalidation.
        needInvalidate = gestureDetector.onTouchEvent(event);

        needInvalidate = scaleGestureDetector.onTouchEvent(event) || needInvalidate;

        if (isZoomEnabled && scaleGestureDetector.isInProgress()) {
            // Special case: if view is inside scroll container and user is scaling disable touch interception by
            // parent.
            disallowParentInterceptTouchEvent();
        }

        if (isValueTouchEnabled) {
            needInvalidate = computeTouch(event) || needInvalidate;
        }

        return needInvalidate;
    }

这里把手势都交给了GestureDetector和ScaleGestureDetector这两个类,我们应该很熟悉,专门用来处理手势事件的两个类:

protected class ChartScaleGestureListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
   

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            if (isZoomEnabled) {
                float scale = 2.0f - detector.getScaleFactor();
                if (Float.isInfinite(scale)) {
                    scale = 1;
                }
                return chartZoomer.scale(computator, detector.getFocusX(), detector.getFocusY(), scale);
            }

            return false;
        }
    }

    protected class ChartGestureListener extends GestureDetector.SimpleOnGestureListener {
   

        protected ScrollResult scrollResult = new ScrollResult();

        @Override
        public boolean onDown(MotionEvent e) {
            if (isScrollEnabled) {

                disallowParentInterceptTouchEvent();

                return chartScroller.startScroll(computator);
            }

            return false;

        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if (isZoomEnabled) {
                return chartZoomer.startZoom(e, computator);
            }

            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (isScrollEnabled) {
                boolean canScroll = chartScroller
                        .scroll(computator, distanceX, distanceY, scrollResult);

                allowParentInterceptTouchEvent(scrollResult);

                return canScroll;
            }

            return false;

        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (isScrollEnabled) {
                return chartScroller.fling((int) -velocityX, (int) -velocityY, computator);
            }

            
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值