@Override public boolean onTouchEvent(MotionEvent event) { int action = event.getActionMasked(); switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: // 处理摇杆事件 break; case MotionEvent.ACTION_UP: // 停止处理摇杆事件 break; } return true; }
public class JoystickView extends View { private Paint mPaint; private float centerX; private float centerY; private float deltaX; private float deltaY; private float radius; private int joystickBackground; private boolean isTouchable;
public JoystickView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.FILL);
joystickBackground = getResources().getColor(R.color.joystickBackground);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
centerX = w / 2;
centerY = h / 2;
radius = Math.min(w, h) / 2;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(joystickBackground);
canvas.drawCircle(centerX, centerY, radius, mPaint);
if (isTouchable) {
mPaint.setColor(getResources().getColor(R.color.touchableJo