中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

android studio游戲搖桿開發教程

小億
140
2023-12-19 11:52:16
欄目: 編程語言

以下是一個簡單的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

0
神池县| 休宁县| 常山县| 山东省| 略阳县| 阿拉尔市| 霍林郭勒市| 清远市| 泾阳县| 广平县| 杭锦后旗| 华蓥市| 建水县| 随州市| 亳州市| 大足县| 靖西县| 远安县| 加查县| 略阳县| 东光县| 德格县| 鄂温| 南溪县| 乐东| 江西省| 托里县| 长武县| 张家界市| 黔江区| 隆林| 萨迦县| 策勒县| 涿鹿县| 孟州市| 正蓝旗| 化州市| 安康市| 黄平县| 宁远县| 康乐县|