以下是一個簡單的Android Studio游戲搖桿開發教程的步驟:
步驟1:創建一個新的Android Studio項目并命名為“GameJoystickDemo”。
步驟2:在“app”模塊的“res”文件夾下創建一個新的文件夾“drawable”。在該文件夾下創建一個名為“joystick_background.png”的圖片文件。這將作為搖桿的背景。
步驟3:在“drawable”文件夾下創建一個名為“joystick_handle.png”的圖片文件。這將作為搖桿的手柄。
步驟4:在“app”模塊的“res”文件夾下創建一個名為“values”的文件夾。在該文件夾下創建一個名為“attrs.xml”的文件,并在其中添加以下代碼:
<resources>
<declare-styleable name="GameJoystickView">
<attr name="joystickBackground" format="reference" />
<attr name="joystickHandle" format="reference" />
</declare-styleable>
</resources>
步驟5:在“app”模塊的“java”文件夾下創建一個新的Java類文件,并命名為“GameJoystickView”。在該類中添加以下代碼:
public class GameJoystickView extends View {
private Paint paint;
private Bitmap joystickBackground;
private Bitmap joystickHandle;
private int handleX, handleY;
private float centerX, centerY, radius, handleRadius;
private boolean isPressed = false;
public GameJoystickView(Context context) {
super(context);
init(context, null);
}
public GameJoystickView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public GameJoystickView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
paint = new Paint();
paint.setAntiAlias(true);
if (attrs != null) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.GameJoystickView);
joystickBackground = BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.GameJoystickView_joystickBackground, R.drawable.joystick_background));
joystickHandle = BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.GameJoystickView_joystickHandle, R.drawable.joystick_handle));
ta.recycle();
} else {
joystickBackground = BitmapFactory.decodeResource(getResources(), R.drawable.joystick_background);
joystickHandle = BitmapFactory.decodeResource(getResources(), R.drawable.joystick_handle);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
centerX = w / 2f;
centerY = h / 2f;
radius = Math.min(w, h) / 3f;
handleRadius = Math.min(w, h) / 6f;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(joystickBackground, centerX - radius, centerY - radius, paint);
canvas.drawBitmap(joystickHandle, handleX - handleRadius, handleY - handleRadius, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isPressed = true;
updateHandlePosition(event);
break;
case MotionEvent.ACTION_MOVE:
updateHandlePosition(event);
break;
case MotionEvent.ACTION_UP:
isPressed = false;
handleX = (int) centerX;
handleY = (int) centerY;
break;
}
invalidate();
return true;
}
private void updateHandlePosition(MotionEvent event) {
float dx = event.getX() - centerX;
float dy = event.getY() - centerY;
float distance = (float) Math.sqrt(dx * dx + dy * dy);
if (distance <= radius) {
handleX = (int) event.getX();
handleY = (int) event.getY();
} else {
float ratio = radius / distance;
handleX = (int) (centerX + dx * ratio);
handleY = (int) (centerY + dy * ratio);
}
}
public int getHandleX() {
return handleX;
}
public int getHandleY() {
return handleY;
}
public boolean isPressed() {
return isPressed;
}
}
步驟6