public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: currX = event.getX(); break; case MotionEvent.ACTION_DOWN: if (event.getX() > bg_on.getWidth() || event.getY() > bg_on.getHeight()) return false; OnSlip = true; downX = event.getX(); currX = downX; break; case MotionEvent.ACTION_CANCEL: OnSlip = false; boolean choose = NowChoose; if (currX >= (bg_on.getWidth() / 2)) { currX = bg_on.getWidth() - slip_btn.getWidth() / 2; NowChoose = true; } else { currX = currX - slip_btn.getWidth() / 2; NowChoose = false; } if (isChgLsnOn && (choose != NowChoose)) ChgLsn.OnChanged(NowChoose); break; case MotionEvent.ACTION_UP: OnSlip = false; boolean LastChoose = NowChoose; if (event.getX() >= (bg_on.getWidth() / 2)) { currX = bg_on.getWidth() - slip_btn.getWidth() / 2; NowChoose = true; } else { currX = currX - slip_btn.getWidth() / 2; NowChoose = false; } if (isChgLsnOn && (LastChoose != NowChoose)) ChgLsn.OnChanged(NowChoose); break; default: } invalidate(); return true; }
public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) // ��ݶ�����ִ�д��� { case MotionEvent.ACTION_MOVE: // ���� NowX = event.getX(); break; case MotionEvent.ACTION_DOWN: // ���� if (event.getX() > bg_on.getWidth() || event.getY() > bg_on.getHeight()) return false; OnSlip = true; DownX = event.getX(); NowX = DownX; break; case MotionEvent.ACTION_CANCEL: // �Ƶ��ؼ��ⲿ OnSlip = false; boolean choose = NowChoose; if (NowX >= (bg_on.getWidth() / 2)) { NowX = bg_on.getWidth() - slip_btn.getWidth() / 2; NowChoose = true; } else { NowX = NowX - slip_btn.getWidth() / 2; NowChoose = false; } if (isChgLsnOn && (choose != NowChoose)) // ��������˼�����,�͵����䷽��.. ChgLsn.OnChanged(NowChoose); break; case MotionEvent.ACTION_UP: // �ɿ� OnSlip = false; boolean LastChoose = NowChoose; if (event.getX() >= (bg_on.getWidth() / 2)) { NowX = bg_on.getWidth() - slip_btn.getWidth() / 2; NowChoose = true; } else { NowX = NowX - slip_btn.getWidth() / 2; NowChoose = false; } if (isChgLsnOn && (LastChoose != NowChoose)) // ��������˼�����,�͵����䷽��.. ChgLsn.OnChanged(NowChoose); break; default: } invalidate(); // �ػ��ؼ� return true; }
@Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { if (event.getX() > bg_off.getWidth() || event.getY() > bg_off.getHeight()) { return false; } else { onSlip = true; downX = event.getX(); nowX = downX; } break; } case MotionEvent.ACTION_MOVE: { nowX = event.getX(); break; } case MotionEvent.ACTION_UP: { onSlip = false; if (event.getX() >= (bg_on.getWidth() / 2)) { nowStatus = true; nowX = bg_on.getWidth() - slipper_btn.getWidth(); } else { nowStatus = false; nowX = 0; } if (listener != null) { listener.OnChanged(SwitchButton.this, nowStatus); } break; } } // 刷新界面 invalidate(); return true; }