防股票折線圖十字定位(achartengine)
* @param context
* @param view4:曲線view
* @param x:曲線x軸集合
* @param marginLeft:移動x軸左邊距
* @param marginBottom:移動x軸下邊距
* @param marginLeftY:移動y軸左邊距
* @param marginBottomY:移動y軸左邊距
* @param mChart
* @param tvXY:顯示textview
* @param lineWidth:xy線寬
* @param mPopupView:移動y軸視圖
* @param mPopupView2:移動x軸視圖
* @param type:二分查詢類型
public static void markXY(final Context context, final GraphicalView view4, final List<double[]> x, final int marginLeft, final int marginBottom, final int marginLeftY, final int marginBottomY, final XYChart mChart, final TextView tvXY, final int lineWidth, final View mPopupView, final View mPopupView2, final int type){
OnTouchListener chartViewOnTouchListener = new OnTouchListener() {
PopupWindow mPopupWindow=null;
PopupWindow mPopupWindow2=null;
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPopupWindow = new PopupWindow(mPopupView,lineWidth, view4.getHeight());
mPopupWindow2 = new PopupWindow(mPopupView2,view4.getWidth(), lineWidth);
break;
case MotionEvent.ACTION_MOVE:
// //自定義設定xy
//擷取點選位置螢幕xy
float screenX = event.getX();
float screenY = event.getY();
//裝換為圖示xy
double[] realPoint = mChart.toRealPoint(screenX, screenY);
XYMultipleSeriesDataset dataset = mChart.getDataset();
XYSeries seriesAt = dataset.getSeriesAt(0);
if(x!=null&&x.size()>0){
int index=binarySearch(x.get(0), realPoint[0],type);
if(index<x.get(0).length){
Log.i("TAG", "dataset:"+seriesAt.getX(index)+";"+seriesAt.getY(index));
double[] screenPoint2 = mChart.toScreenPoint(new double[]{seriesAt.getX(index),seriesAt.getY(index)});
double[] screenPoint = mChart.toScreenPoint(new double[]{0,0});
int toBottom=(int) (view4.getHeight()-screenPoint[1]);
int toLeft=(int)(view4.getWidth()-screenPoint[0]);
if(seriesAt.getY(index)==0.0){
tvXY.setText("");
}else{
tvXY.setText("x:"+seriesAt.getX(index)+" y:"+seriesAt.getY(index));
}
Log.i("TAG", "screenPoint2:"+screenPoint2.length+":"+screenPoint2[0]+";"+screenPoint2[1]);
if (mPopupWindow.isShowing()) {
mPopupWindow.update((int)screenPoint2[0]+dip2px(context, marginLeftY),dip2px(context, marginBottomY)+toBottom, lineWidth,view4.getHeight()-toBottom);
mPopupWindow2.update(dip2px(context, marginLeft),view4.getHeight()-(int)screenPoint2[1]+dip2px(context, marginBottom), view4.getWidth(),lineWidth);
} else {
mPopupWindow.showAtLocation(view4,Gravity.BOTTOM|Gravity.LEFT, (int)screenPoint2[0]+dip2px(context, marginLeftY)+toLeft,dip2px(context, marginBottomY));
mPopupWindow2.showAtLocation(view4,Gravity.BOTTOM|Gravity.LEFT, dip2px(context, marginLeft),view4.getHeight()-(int)screenPoint2[1]+dip2px(context, marginBottom));
}
}
}
break;
case MotionEvent.ACTION_UP:
//隐藏定位軸
if (mPopupWindow != null) {
if (mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
}
mPopupWindow = null;
}
if (mPopupWindow2 != null) {
if (mPopupWindow2.isShowing()) {
mPopupWindow2.dismiss();
}
mPopupWindow2 = null;
}
break;
}
return false;
}
};
view4.setOnTouchListener(chartViewOnTouchListener);
view4.setId(0);
}