91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
五大连池市| 德州市| 民县| 新沂市| 琼中| 万宁市| 新河县| 二连浩特市| 博兴县| 革吉县| 抚顺县| 保靖县| 通化市| 英山县| 大新县| 楚雄市| 朝阳市| 台安县| 怀柔区| 泗阳县| 汝阳县| 含山县| 凌源市| 昌图县| 卓尼县| 东方市| 海丰县| 即墨市| 芜湖县| 客服| 伊吾县| 金阳县| 翁源县| 长垣县| 阳高县| 定边县| 大田县| 张家川| 台中县| 柘城县| 康乐县|