Android Studio で タイマーを作ってみる⑤ (スワイプで円弧を描画)
スワイプで円弧の角度を変更できるように以下の処理を記載
①Viewの OnTouchListener の onTouch処理にて記載。
②ACTION_DOWN の位置で、円弧の外なら対象外、
中なら対象の判定
③②の判定で、円弧の内側ならその角度で円弧を描画
// スワイプでタイマーの時間を変更
public class DragViewListener implements View.OnTouchListener {
// ドラッグ対象のView
private RelativeLayout dragView;
// ドラッグ中に移動量を取得するための変数
private int oldx;
private int oldy;
double nPI = Math.PI;
public DragViewListener(RelativeLayout dragView) {
this.dragView = dragView;
}
@Override
public boolean onTouch(View view, MotionEvent event) {
// 処理中は動かない
if (mode == TIMER_START || mode == TIMER_RESTART) {
return false;
}
NumberPicker np = (NumberPicker)findViewById(R.id.numberPicker);
// タッチしている位置取得
int x = (int) event.getRawX();
int y = (int) event.getRawY();
TextView tw = (TextView)findViewById(R.id.textView);
Window window = getWindow();
av.top = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();;
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (oldx < 0 && oldy <0) {
return false;
}
int val;
if (x == 0 && y == 0 ) {
// 中心
return false;
} else if (x == 0) {
// X軸上
if (y > 0) {
// 右
val = 15;
} else {
// 左
val = 45;
}
} else if (y == 0 ) {
// Y軸上
if (x > 0) {
// 上
val = 60;
} else {
// 下
val = 30;
}
} else {
double rad = Math.atan2(x, y );
double deg = Math.toDegrees(rad);
if (0 < deg && deg < 90 ) {
// 右下
val = (int)((90 + 90 - deg) / 6);
} else if (90 < deg && deg < 180) {
// 右上
val = (int)((180 - deg) / 6);
} else if (-90 < deg && deg < 0) {
// 左下
val = (int) ((Math.abs(deg) + 180) / 6);
} else if (-180 < deg && deg < -90) {
// 左下
val = (int) ((Math.abs(deg) + 180) / 6);
} else {
return false;
}
}
if (val > 60) {
val = 60;
} else if (val < 1) {
val = 60;
}
// 描画処理を記載
break;
case MotionEvent.ACTION_DOWN:
oldx = -1;
oldy = -1;
if // 円弧の描画の外側なら {
return false;
}
if // 円弧の描画の外側なら {
return false;
}
break;
}
// 今回のタッチ位置を保持
oldx = x;
oldy = y;
// イベント処理完了
return true;
}
}
みたいな感じです。