Android Studio : Simple Roulette Game II (輪盤遊戲第二版)
寫完 [程式] Android Studio : Simple Roulette Game 後,忽然想到有另外一種輪盤遊戲的呈現方式,因此稍微修改一下後,做了指針式的羅盤遊戲,重點在於羅盤與指針的圖片大小一樣,這樣程式在撰寫比較簡單,否則計算旋轉的中心點,有時候還蠻麻煩的。
相關資訊圖片:
| ![[程式] Android Studio : Simple Roulette Game II Roulette view畫面](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXxqYHDMWLfRAD_nFxp1ivr38AJHOvqWIjVLYgvLyX6PlIP2w_v61iFxlgU8VVQO8x_tBH0bXO2gMauJZlU677F6Zp6kt7EKnLsupXONWh77yic1V1lmL-MZmzfSfvN112iUDHZ0qph1yF/s640/RouletteII_01.jpg) | 
| Roulette view畫面 | 
| ![[程式] Android Studio : Simple Roulette Game II 初始畫面](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpOBNaAcRL_mF1O19H-OvDpLnw3QkDkXiMIgSTKeAfzYYbhrYjSonyFub44pvxPySry1x7r-h2k34Kva8D7bN4KdqkpDII6mb_w9WfhA2qgTHV6cfNllCTtpl4D7_g1YXQG2kPDrJb3Aau/s400/RouletteII_02.jpg) | 
| 初始畫面 | 
| ![[程式] Android Studio : Simple Roulette Game II 正確執行畫面 (degree % 360)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1VMq1KtxwF5NQq35FkCtvMV3WS8nauTV_6YS8f1hHutOP4YvlkKOulEzmBKrgccMV2-qxasZI2p3LnzVBhGa2PI_mCkGJG3YmKPTwYR6rhSproO5-0IgJWXa-sQGXUjGQgl3lqxwHmnDs/s400/RouletteII_03.jpg) | 
| 正確執行畫面 (degree % 360) | 
透過
指針與
綠線、
藍線的角度,就可以知道差異在哪裡了,若還是不清楚,回去
[程式] Android Studio : Simple Roulette Game這篇,最底下的補充說明圖片,應該就會清楚了。
| ![[程式] Android Studio : Simple Roulette Game II 錯誤執行畫面 (360 - (degree % 360))](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimNt42YviNHX__mqAVDbPQaCi1E9x1u2n6gMiCWDHnMmQxomHoJdPrUnkGA5pHJXLtFaQLwu1OSbiFYSIGqgEHRw2ZjlITvoxOwt8D9B78YdkRRaPH3Lid7ZzfL0QqYCCgNFR3SR6hWl6V/s400/RouletteII_04.jpg) | 
| 錯誤執行畫面 (360 - (degree % 360)) | 
| ![[程式] Android Studio : Simple Roulette Game II 相同尺寸圖片](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3XaSdh5mhJju39HACA9Q1ZRtPYCBrpH3DWywrz-tnQAfusOGIeRHdITncZzHsrxwVRX1Ox4yJPCxlVvtW4DknZaVs10O0uPhSOViBtxExAmPYLvMe53AY0F_XVNajwReX7wqzqsWQDMdz/s640/RouletteII_05.jpg) | 
| 相同尺寸圖片 | 
Source Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.rouletteII.MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="SPIN" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="32dp"
        android:text="" />
    <ImageView
        android:id="@+id/iv_wheel"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        app:srcCompat="@drawable/ic_wheel" />
    <ImageView
        android:id="@+id/iv_pointer"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        app:srcCompat="@drawable/ic_pointer" />
</RelativeLayout>
MainActivity.java
package tw.idv.wenyen.rouletteii;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
    Button button;
    TextView textView;
//    ImageView iv_wheel;
    // 將輪盤改為指針
    ImageView iv_pointer;
    Random r;
    int degree = 0, degree_old = 0;
    //because there is 37 sectors on the wheel (9.72 degrees each)
    private static final float FACTOR = 4.86f;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        textView = findViewById(R.id.textView);
//        iv_wheel = findViewById(R.id.iv_wheel);
        iv_pointer = findViewById(R.id.iv_pointer);
        r = new Random();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 紀錄輪盤的度數
                degree_old = degree % 360;
                // 輪盤的轉動度數
                degree = r.nextInt(3600) + 720;
                RotateAnimation rotate = new RotateAnimation(degree_old, degree,
                        RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                // Animation : attributes
                // https://developer.android.com/reference/android/view/animation/Animation.html
                // 動畫執行時間
                rotate.setDuration(3600);
                // true : 動畫執行完畢後,View對象保留在終止的位置
                // false : 動畫執行完畢後,回復到最初狀態
                rotate.setFillAfter(true);
                // 動畫速度由快到慢
                rotate.setInterpolator(new DecelerateInterpolator());
                rotate.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        textView.setText("");
                    }
                    @Override
                    public void onAnimationEnd(Animation animation) {
//                        textView.setText(currentNumber(360 - (degree % 360)));
                        // 簡單說明就是,羅盤轉動是座標旋轉,指針轉動是角度旋轉
                        textView.setText(currentNumber(degree % 360));
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                // 動畫開始
//                iv_wheel.startAnimation(rotate);
                // 將輪盤改為指針
                iv_pointer.startAnimation(rotate);
            }
        });
    }
    /**
     * 傳入轉動角度
     * 顯示輪盤的數值與顏色字串
     * @param degrees
     * @return number & color string
     */
    private String currentNumber(int degrees) {
        String text = "";
        // do this for each of the numbers
        if (degrees >= (FACTOR * 1) && degrees < (FACTOR * 3)) {
            text = "32 red";
        }
        if (degrees >= (FACTOR * 3) && degrees < (FACTOR * 5)) {
            text = "15 black";
        }
        if (degrees >= (FACTOR * 5) && degrees < (FACTOR * 7)) {
            text = "19 red";
        }
        if (degrees >= (FACTOR * 7) && degrees < (FACTOR * 9)) {
            text = "4 black";
        }
        if (degrees >= (FACTOR * 9) && degrees < (FACTOR * 11)) {
            text = "21 red";
        }
        if (degrees >= (FACTOR * 11) && degrees < (FACTOR * 13)) {
            text = "2 black";
        }
        if (degrees >= (FACTOR * 13) && degrees < (FACTOR * 15)) {
            text = "25 red";
        }
        if (degrees >= (FACTOR * 15) && degrees < (FACTOR * 17)) {
            text = "17 black";
        }
        if (degrees >= (FACTOR * 17) && degrees < (FACTOR * 19)) {
            text = "34 red";
        }
        if (degrees >= (FACTOR * 19) && degrees < (FACTOR * 21)) {
            text = "6 black";
        }
        if (degrees >= (FACTOR * 21) && degrees < (FACTOR * 23)) {
            text = "21 red";
        }
        if (degrees >= (FACTOR * 23) && degrees < (FACTOR * 25)) {
            text = "13 black";
        }
        if (degrees >= (FACTOR * 25) && degrees < (FACTOR * 27)) {
            text = "36 red";
        }
        if (degrees >= (FACTOR * 27) && degrees < (FACTOR * 29)) {
            text = "11 black";
        }
        if (degrees >= (FACTOR * 29) && degrees < (FACTOR * 31)) {
            text = "30 red";
        }
        if (degrees >= (FACTOR * 31) && degrees < (FACTOR * 33)) {
            text = "8 black";
        }
        if (degrees >= (FACTOR * 33) && degrees < (FACTOR * 35)) {
            text = "23 red";
        }
        if (degrees >= (FACTOR * 35) && degrees < (FACTOR * 37)) {
            text = "10 black";
        }
        if (degrees >= (FACTOR * 37) && degrees < (FACTOR * 39)) {
            text = "5 red";
        }
        if (degrees >= (FACTOR * 39) && degrees < (FACTOR * 41)) {
            text = "24 black";
        }
        if (degrees >= (FACTOR * 41) && degrees < (FACTOR * 43)) {
            text = "16 red";
        }
        if (degrees >= (FACTOR * 43) && degrees < (FACTOR * 45)) {
            text = "33 black";
        }
        if (degrees >= (FACTOR * 45) && degrees < (FACTOR * 47)) {
            text = "1 red";
        }
        if (degrees >= (FACTOR * 47) && degrees < (FACTOR * 49)) {
            text = "20 black";
        }
        if (degrees >= (FACTOR * 49) && degrees < (FACTOR * 51)) {
            text = "14 red";
        }
        if (degrees >= (FACTOR * 51) && degrees < (FACTOR * 53)) {
            text = "31 black";
        }
        if (degrees >= (FACTOR * 53) && degrees < (FACTOR * 55)) {
            text = "9 red";
        }
        if (degrees >= (FACTOR * 55) && degrees < (FACTOR * 57)) {
            text = "22 black";
        }
        if (degrees >= (FACTOR * 57) && degrees < (FACTOR * 59)) {
            text = "18 red";
        }
        if (degrees >= (FACTOR * 59) && degrees < (FACTOR * 61)) {
            text = "29 black";
        }
        if (degrees >= (FACTOR * 61) && degrees < (FACTOR * 63)) {
            text = "7 red";
        }
        if (degrees >= (FACTOR * 63) && degrees < (FACTOR * 65)) {
            text = "28 black";
        }
        if (degrees >= (FACTOR * 65) && degrees < (FACTOR * 67)) {
            text = "12 red";
        }
        if (degrees >= (FACTOR * 67) && degrees < (FACTOR * 69)) {
            text = "35 black";
        }
        if (degrees >= (FACTOR * 69) && degrees < (FACTOR * 71)) {
            text = "3 red";
        }
        if (degrees >= (FACTOR * 71) && degrees < (FACTOR * 73)) {
            text = "26 black";
        }
        if ((degrees >= (FACTOR * 73) && degrees < 360) || (degrees >=0 && degrees < (FACTOR * 1))) {
            text = "0";
        }
        return text;
    }
}
 
0 意見:
張貼留言