android plot命令,在AndroidPlot中获取点的正确位置

我不知道是否有更好的内置解决方案,但这是一个手动方法.

以下代码将光标置于用户触摸屏幕的X坐标和绘图的第一个数据系列的相应Y坐标.

//NOTE about XYPlotZoomPan: when an OnTouchListener is set, zooming is disabled. Subclass to avoid it.

mPlot.setOnTouchListener(new View.OnTouchListener(){

@Override

public boolean onTouch(View v, MotionEvent me) {

float touchX = me.getX();

float touchY = me.getY();

XYGraphWidget widget = mPlot.getGraphWidget();

RectF gridRect = widget.getGridRect();

if(gridRect.contains(touchX, touchY)){ //Check the touch event is in the grid

XYSeries xyData = mPlot.getSeriesSet().iterator().next();

long targetValX = Math.round(widget.getXVal(touchX));

Log.d(TAG, "Touched at " + touchX + ", " + touchY + ". Target val X: " + targetValX);

Long targetValY = null;

Long prevValX = null;

if(mPlot.getSeriesSet().size() > 1){

Log.w(TAG, "More than one series in plot. Using only the first one");

}

for(int i = 0; i < xyData.size(); ++i){

long currValX = xyData.getX(i).longValue();

long currValY = xyData.getY(i).longValue();

//Calculate the range value of the closest domain value (assumes xyData is sorted in ascending X order)

if(currValX >= targetValX){

long currDiff = currValX - targetValX;

if(prevValX != null && (targetValX - prevValX) < currDiff){

targetValY = xyData.getY(i-1).longValue();

}else{

targetValY = currValY;

}

break;

}

prevValX = currValX;

}

if(targetValY != null){

long maxValY = mPlot.getCalculatedMaxY().longValue();

long minValY = mPlot.getCalculatedMinY().longValue();

float pixelPosY = gridRect.top + ValPixConverter.valToPix(

(double)targetValY, (double)minValY, (double)maxValY, (float)gridRect.height(), true);

widget.setRangeCursorPosition(pixelPosY);

widget.setDomainCursorPosition(touchX);

Log.d(TAG, String.format("Domain cursor set at Y %.2f, val %.2f = %d, min-maxValY (%d, %d)",

pixelPosY,

widget.getRangeCursorVal(), targetValY,

minValY, maxValY));

}else{

Log.w(TAG, "Couldn't find the closest range to the selected domain coordinate");

}

mPlot.invalidate();

}else{

Log.d(TAG, "Touched outside the plot grid");

}

return false;

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值