我想在我的活動中設置圖像按鈕(pushClick),以啓用用於旋轉針圖形的onTouchEvents。不幸的是,無論我是否點擊圖像按鈕,onTouchEvent都處於活動狀態。我怎樣才能防止onTouchEvent發射,直到點擊圖像按鈕後?開始onTouchEvent之後onClick事件
public void pushClick(View pushClick) {
switch (pushClick.getId()) {
case R.id.btn_push:
make(degrees);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startRotating();
break;
case MotionEvent.ACTION_UP:
stopRotating();
break;
}
return super.onTouchEvent(event);
}
private void startRotating() {
returnRotating = false;
if (!keepRotating) {
keepRotating = true;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (keepRotating) {
degrees = (degrees + 1) % 360;
make(degrees);
handler.postDelayed(this, INTERVAL);
}
}
}, INTERVAL);
}
}
private void stopRotating() {
keepRotating = false;
if (!returnRotating) {
returnRotating = true;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (returnRotating) {
degrees = (degrees - 1) % 360;
make(degrees);
handler.postDelayed(this, INTERVAL);
}
}
}, INTERVAL);
}
}
這實際上就像一個魅力!我試圖找出如何使用int將on()添加到onTouchEvent,但是有很多語法問題。萬分感謝! – n00bdev