首先我在我的應用程序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
什麼是你確切的問題? –
@sahil mahajan mj查看編輯 – deerkiller11