安卓graphview获得触摸位置位置、graphview

2023-09-09 21:02:07 作者:相见,不如怀念

首先,我用我的应用程序 HTTP这个库://www.jjoe64。 COM / p / graphview-library.html 来创建图表。然后我用这个code(我在评论中发现的)来获取触摸事件的x位置。

First off I am using this library in my app http://www.jjoe64.com/p/graphview-library.html to create the graphs. I then use this code (which I found in the comments) to get the x location of the touch event.

//intercepts touch events on the graphview
@Override
public boolean dispatchTouchEvent(MotionEvent event) { 
    switch (event.getAction()) {
        //when you touch the screen
        case MotionEvent.ACTION_DOWN:
            // gets the location of the touch on the graphview
            float x = event.getX(event.getActionIndex());
            // gets the boundries of what you are viewing from the function you just added
//  vp is set to vp[0] = 0 & vp[1] = numDaysOil, numDaysOil could be anywhere from 2 to 30 
            double[] vp = graphOilView.getViewPort();
            // gets the width of the graphview
            int width = graphOilView.getWidth();
             //gets the x-Value of the graph where you touched
            @SuppressWarnings("unused")
            double xValue = vp[0] + (x / width) * vp[1];
// trying a different ways to get different x vaules 
            double xVal = (x / width) * numDaysOil;
            //add a method to lookup a value and do anything you want based on xValue. 

            break;
        } // end switch
    return super.dispatchTouchEvent(event);
} // end dispatchTouch Event

这取决于你在哪里触摸(或点击模拟器),我会得到正确的x值,但有时它的小或大的x值的。

depending on where you touch (or click on the emulator) I will get the right x value but sometimes its to small or to large of an x value.

任何人有任何见解呢?

anyone have any insight into this?

编辑:有时,当我在图中xValue点击一个位置将返回正确的x值,有时它会返回一个xValue比大于对应的y值的x值更低或更高。用于创建图形点例如x值是12和y的值是30,但xValue上述code计算可以返回9.所以,如果我再尝试查找X = 9,我会得到错误的y值。被绘制数据:X:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 Y:5,16,8,11,1,30,20 ,11,18,29,27,30,8,10,19

sometimes when I click on a location in the graph xValue will return the right x value and sometimes it will return a xValue that is lower than or greater than the x value that corresponds to y value. example x value used to create point on graph is 12 and y value is 30, but xValue calculated with the above code could return 9. so if i then try to lookup x = 9 I will get the wrong y value. data being graphed: x: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 y: 5, 16, 8, 11, 1, 30, 20, 11, 18, 29, 27, 30, 8, 10, 19

推荐答案

我解决了它是这样的:

graphView = new LineGraphView(context,"Title");
initGraphView(graphView);    
graphView.setOnTouchListener(new OnTouchListener(){
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_DOWN) {
                        int size = series1.size();
                        float screenX = event.getX();
                        float screenY = event.getY();
                        float width_x = v.getWidth();
                        float viewX = screenX - v.getLeft();
                        float viewY = screenY - v.getTop();
                        float percent_x = (viewX/width_x);
                        int pos = (int) (size*percent_x);

                        System.out.println("X: " + viewX + " Y: " + viewY +" Percent = " +percent_x);
                        System.out.println("YVal = " +series1.getY(pos));
                        tvNum.setText(series1.getY(pos)+"");
                        return true;
                    }
                    return false;
                }

            });
 
精彩推荐
图片推荐