2014-09-11 111 views
0

我正在使用AndroidPlot編寫一個應用程序,用戶需要能夠觸摸散點圖上的點並調出有關該特定點的信息。換句話說,應用程序需要識別觸摸位置的最近點或者識別點已被觸摸,並且能夠返回點的特定標識。這個散點圖中的所有點將始終是一個系列,因此在系列之間進行識別不是問題,但我不知道如何實現查找或識別觸摸點。AndroidPlot:檢測點上的觸摸事件

我能得到儘可能:

plot.setOnTouchListener(new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     PointF click = new PointF(motionEvent.getX(), motionEvent.getY()); 
     if(plot.getGraphWidget().containsPoint(click)) { 
      AlertDialog.Builder builder = new AlertDialog.Builder(GraphView.this); 
      builder.setTitle("Point: "); 
      builder.setMessage("Description: "); 
      AlertDialog dialog = builder.create(); 
      dialog.show(); 
     } 
     return false; 
    } 
    }); 
} 

它創建每當圖形被觸摸的AlertDialog。

回答

2

DemoApp's BarPlotExampleActivity在其onPlotClicked(...)方法中實現了此功能。它當然可以改進,但應該給你一個很好的起點。

的基本步驟是:

的情節的圖形區域內的點擊
  • 過濾器。 (你已經得到了這塊)
  • 使用XYPlot.getXVal(screenX)& XYPlot.getYVal(screenY)將屏幕座標轉換爲域/範圍值。
  • 在上述域/範圍值中查找模型中最近的點。