@Override public boolean onTouchEvent(MotionEvent event) { /** 此处可添加修改高度的,没有用到 */ int action = event.getAction(); switch (action) { case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: float rawX = event.getRawX(); float rawY = event.getRawY(); float x = event.getX(); float y = event.getY(); System.out.println("X--" + x); System.out.println("y" + y); int[] location = new int[2]; cv_chart.getLocationOnScreen(location); float offsetY = y - location[1]; float offsetX = x - 50; System.out.println("x--" + location[0]); System.out.println("y--" + location[1]); System.out.println("offsetX--" + offsetX); System.out.println("offsetY--" + offsetY); judgeHeight(offsetY); if (offsetX < 50 + ChartView.width / 4 / 2) { /** 修改第一列的数据 */ x1 = judgeHeight(offsetY); } else if (offsetX < 50 + ChartView.width * 3 / 4 / 2) { /** 修改第二列的数据 */ x2 = judgeHeight(offsetY); } else if (offsetX < 50 + ChartView.width * 5 / 4 / 2) { /** 修改第三列的数据 */ x3 = judgeHeight(offsetY); } else if (offsetX < 50 + ChartView.width * 7 / 4 / 2) { /** 修改第四列的数据 */ x4 = judgeHeight(offsetY); } else { /** 修改第五列的数据 */ x5 = judgeHeight(offsetY); } cv_chart.invalidate(); break; } return super.onTouchEvent(event); }
@SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chart); ll_full = (RelativeLayout) findViewById(R.id.ll_full); cv_chart = (ChartView) findViewById(R.id.cv_chart); // bt_change = (Button) findViewById(R.id.bt_change); // bt_change.setOnClickListener(this); random = new Random(); list = new ArrayList<ChartBean>(); for (int i = 0; i < 5; i++) { ChartBean chartBean = new ChartBean(15 + (i * 3) % 9, "05.05"); list.add(chartBean); } cv_chart.setData(list, "元/斤", true); cv_chart.invalidate(); }