Home
2018年3月17日 星期六

[程式] Android Studio : Simple Spin The Bottle Game

Android Studio : Simple Spin The Bottle Game

這個轉瓶子遊戲(Spin The Bottle Game)程式也算是簡單的範例,跟輪盤遊戲(Simple Roulette Game)差異不大,使用的主要是RotateAnimation,不過有多了一個還原功能(RESET),與程式內的 rotate.setFillAfter(false)有相同的效果,差異在於一個是手動還原一個是自動還原。
可以透過修改程式內的一些相關數值,產生順時針旋轉與逆時針旋轉的效果,這部分有興趣的人在玩玩看吧!


範例網址:

https://www.youtube.com/watch?v=YgOG62uPA5s&index=10&list=PL8h0boM9pEdRyn4bXj4qslAj5a3UzYdP_
範例相關圖片 ic_bottle.png 取自網路,為[不限使用權]圖片

相關資訊圖片:

Spin The Bottle View畫面
Android Studio : Simple Spin The Bottle Game


初始畫面 與 RESET畫面
Android Studio : Simple Spin The Bottle Game

執行畫面(點擊 GO 後)
Android Studio : Simple Spin The Bottle Game

相關LOG
Android Studio : Simple Spin The Bottle Game

Source Code:

activity_bottle.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="tw.idv.wenyen.spinthebottle.BottleActivity">

    <ImageView
        android:id="@+id/iv_bottle"
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:scaleType="centerInside"
        android:src="@drawable/ic_bottle" />

    <Button
        android:id="@+id/b_go"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="GO"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>

BottleActivity.java
package tw.idv.wenyen.spinthebottle;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;

import java.util.Random;

public class BottleActivity extends AppCompatActivity {

    ImageView iv_bottle;

    Button b_go;

    Random r;

    int angle;

    boolean restart = false;

    private static final String TAG = "BottleActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottle);
        Log.d(TAG, "onCreate(Bundle savedInstanceState)");
        r = new Random();

        iv_bottle = (ImageView) findViewById(R.id.iv_bottle);

        b_go = (Button) findViewById(R.id.b_go);

        b_go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 判斷是 GO 還是 RESET
                if(restart) {
                    Log.d(TAG, "restart=" + restart);
                    angle = angle % 360;
                    Log.d(TAG, "angle=" + angle);
                    // toDegree = 360 與 toDegree = 0 的差別,試試看就知道
                    // 另外 pivotXValue 與 pivotXValue 若非 0.5f,也請試試看
                    RotateAnimation rotate = new RotateAnimation(angle, 360,
                            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                            RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                    rotate.setFillAfter(true);
                    rotate.setDuration(1000);
                    //動畫速度由慢到快,再到慢
                    rotate.setInterpolator(new AccelerateDecelerateInterpolator());

                    // 動畫開始
                    iv_bottle.startAnimation(rotate);

                    b_go.setText("GO");
                    restart = false;
                } else {
                    Log.d(TAG, "restart=" + restart);
                    // 瓶子的轉動度數
                    angle = r.nextInt(3600) + 360;
                    Log.d(TAG, "angle=" + angle);
                    RotateAnimation rotate = new RotateAnimation(0, angle,
                            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                            RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                    rotate.setFillAfter(true);
                    rotate.setDuration(3600);
                    rotate.setInterpolator(new AccelerateDecelerateInterpolator());

                    // 動畫開始
                    iv_bottle.startAnimation(rotate);

                    restart = true;
                    b_go.setText("RESET");
                }

            }
        });
    }
}

實作心得:

有了內文所述兩個範例,就可以做些簡單的小遊戲,動動腦或許下個熱門遊戲就是你開發的。

0 意見:

張貼留言