亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

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
蒙城县| 桂东县| 广宗县| 卢龙县| 阜平县| 迁安市| 巴南区| 镇雄县| 玉环县| 新竹市| 平邑县| 新巴尔虎右旗| 扶风县| 安顺市| 屏南县| 新绛县| 榆中县| 仙桃市| 军事| 亳州市| 桂阳县| 林芝县| 土默特右旗| 雷州市| 绵竹市| 吴桥县| 双江| 曲靖市| 仪陇县| 尉氏县| 商水县| 罗定市| 辉南县| 大连市| 德钦县| 辽宁省| 阿克陶县| 新源县| 永丰县| 南京市| 常德市|