2013-01-22 108 views
0

首先我在我的應用程序http://www.jjoe64.com/p/graphview-library.html中使用此庫來創建圖形。然後我使用這個代碼(我在評論中找到)來獲取觸摸事件的x位置。android graphview獲取觸摸位置

//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值的。

任何人都有這方面的見解?

編輯:有時,當我點擊圖中的位置xValue將返回正確的x值,有時它會返回一個低於或大於對應於y值的x值的xValue。用於在圖上創建點的示例x值是12,y值是30,但用上面的代碼計算的xValue可能會返回9.因此,如果我然後嘗試查找x = 9,我將得到錯誤的y值。數據被繪圖:x:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15:5,16,8,11,1,30,20 ,11,18,29,27,30,8,10,19

+1

什麼是你確切的問題? –

+0

@sahil mahajan mj查看編輯 – deerkiller11

回答

0

我解決了它這樣的:

graphView = new LineGraphView(context,"Title"); 
graphView = 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; 
       } 

      }); 

它看起來差不多的,你是,但記得設置graphView.setScrollable(false)graphView.setScalable(false)每當你想要從視圖中獲取數字。我認爲這是一些奇怪的包裝。我最終不得不停止數據收集並「凍結」圖表,以便從系列中獲取x和y值。

有誰知道更好的方法?

0

我解決了它這樣的:

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; 
       } 

      });