防股票折线图十字定位(achartengine)

   防股票折线图十字定位(achartengine)

  * @param context
     * @param view4:曲线view
     * @param x:曲线x轴集合
     * @param marginLeft:移动x轴左边距
     * @param marginBottom:移动x轴下边距
     * @param marginLeftY:移动y轴左边距
     * @param marginBottomY:移动y轴左边距
     * @param mChart
     * @param tvXY:显示textview
     * @param lineWidth:xy线宽
     * @param mPopupView:移动y轴视图
     * @param mPopupView2:移动x轴视图
     * @param type:二分查询类型

public static void markXY(final Context context, final GraphicalView view4, final List x, final int marginLeft, final int marginBottom, final int marginLeftY, final int marginBottomY, final XYChart mChart, final TextView tvXY, final int lineWidth, final View mPopupView, final View mPopupView2, final int type){

        
        OnTouchListener chartViewOnTouchListener = new OnTouchListener() {
            PopupWindow mPopupWindow=null;
            PopupWindow mPopupWindow2=null;
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mPopupWindow = new PopupWindow(mPopupView,lineWidth, view4.getHeight());
                    mPopupWindow2 = new PopupWindow(mPopupView2,view4.getWidth(), lineWidth);
                    break;
                case MotionEvent.ACTION_MOVE:
//                    //自定义设置xy
                    //获取点击位置屏幕xy
                    float screenX = event.getX();
                    float screenY = event.getY();
                    //装换为图标xy
                    double[] realPoint = mChart.toRealPoint(screenX, screenY);
                    XYMultipleSeriesDataset dataset = mChart.getDataset();
                    XYSeries seriesAt = dataset.getSeriesAt(0);
                    if(x!=null&&x.size()>0){
                    int index=binarySearch(x.get(0), realPoint[0],type);
                    if(index                     Log.i("TAG", "dataset:"+seriesAt.getX(index)+";"+seriesAt.getY(index));
                    double[] screenPoint2 = mChart.toScreenPoint(new double[]{seriesAt.getX(index),seriesAt.getY(index)});
                    double[] screenPoint = mChart.toScreenPoint(new double[]{0,0});
                    int toBottom=(int) (view4.getHeight()-screenPoint[1]);
                    int toLeft=(int)(view4.getWidth()-screenPoint[0]);
                    if(seriesAt.getY(index)==0.0){
                        tvXY.setText("");
                    }else{
                       tvXY.setText("x:"+seriesAt.getX(index)+" y:"+seriesAt.getY(index));
                    }
                    Log.i("TAG", "screenPoint2:"+screenPoint2.length+":"+screenPoint2[0]+";"+screenPoint2[1]);
                    if (mPopupWindow.isShowing()) {
                        mPopupWindow.update((int)screenPoint2[0]+dip2px(context, marginLeftY),dip2px(context, marginBottomY)+toBottom, lineWidth,view4.getHeight()-toBottom);
                        mPopupWindow2.update(dip2px(context, marginLeft),view4.getHeight()-(int)screenPoint2[1]+dip2px(context, marginBottom), view4.getWidth(),lineWidth);
                    } else {
                        mPopupWindow.showAtLocation(view4,Gravity.BOTTOM|Gravity.LEFT, (int)screenPoint2[0]+dip2px(context, marginLeftY)+toLeft,dip2px(context, marginBottomY));
                        mPopupWindow2.showAtLocation(view4,Gravity.BOTTOM|Gravity.LEFT, dip2px(context, marginLeft),view4.getHeight()-(int)screenPoint2[1]+dip2px(context, marginBottom));
                    }    
                    }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    //隐藏定位轴
                    if (mPopupWindow != null) {
                        if (mPopupWindow.isShowing()) {
                            mPopupWindow.dismiss();
                        }
                        mPopupWindow = null;
                    }
                    if (mPopupWindow2 != null) {
                        if (mPopupWindow2.isShowing()) {
                            mPopupWindow2.dismiss();
                        }
                        mPopupWindow2 = null;
                    }
                    break;    
                }
                return false;
            }
        };
        view4.setOnTouchListener(chartViewOnTouchListener);
        view4.setId(0);
    }

你可能感兴趣的:(防股票折线图十字定位(achartengine))