Android之實現“抽獎大輪盤”
這個執行個體可以協助我們學習旋轉動畫和計時器這兩個知識點,廢話不多說,上主程式:
package com.bear.lotterywheel;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation.AnimationListener;import android.view.animation.Animation;import android.view.animation.RotateAnimation;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {//設定一個時間常量,此常量有兩個作用,1.圓燈視圖顯示與隱藏中間的切換時間;2.指標轉一圈所需要的時間,現設定為500毫秒private static final long ONE_WHEEL_TIME = 500;//記錄圓燈視圖是否顯示的布爾常量private boolean lightsOn = true;//開始轉動時候的角度,初始值為0private int startDegree = 0;private ImageView lightIv;private ImageView pointIv;private ImageView wheelIv;//指標轉圈圈數資料來源private int[] laps = { 5, 7, 10, 15 };//指標所指向的角度資料來源,因為有6個選項,所有此處是6個值private int[] angles = { 0, 60, 120, 180, 240, 300 };//轉盤內容數組private String[] lotteryStr = { "索尼PSP", "10元紅包", "謝謝參與", "DNF錢包","OPPO MP3", "5元紅包", };//子線程與UI線程通訊的handler對象private Handler mHandler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case 0:if (lightsOn) {// 設定lightIv不可見lightIv.setVisibility(View.INVISIBLE);lightsOn = false;} else {// 設定lightIv可見lightIv.setVisibility(View.VISIBLE);lightsOn = true;}break;default:break;}};};//監聽動畫狀態的監聽器private AnimationListener al = new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationEnd(Animation animation) {String name = lotteryStr[startDegree % 360 / 60];Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);setupViews();flashLights();pointIv.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {int lap = laps[(int) (Math.random() * 4)];int angle = angles[(int) (Math.random() * 6)];//每次轉圈角度增量int increaseDegree = lap * 360 + angle;//初始化旋轉動畫,後面的四個參數是用來設定以自己的中心點為圓心轉圈RotateAnimation rotateAnimation = new RotateAnimation(startDegree, startDegree + increaseDegree,RotateAnimation.RELATIVE_TO_SELF, 0.5f,RotateAnimation.RELATIVE_TO_SELF, 0.5f);//將最後的角度賦值給startDegree作為下次轉圈的初始角度startDegree += increaseDegree;//計算動畫播放總時間long time = (lap + angle / 360) * ONE_WHEEL_TIME;//設定動畫播放時間rotateAnimation.setDuration(time);//設定動畫播放完後,停留在最後一幀畫面上rotateAnimation.setFillAfter(true);//設定動畫的加速行為,是先加速後減速rotateAnimation.setInterpolator(MainActivity.this,android.R.anim.accelerate_decelerate_interpolator);//設定動畫的監聽器rotateAnimation.setAnimationListener(al);//開始播放動畫pointIv.startAnimation(rotateAnimation);}});}private void setupViews(){lightIv = (ImageView) findViewById(R.id.light);pointIv = (ImageView) findViewById(R.id.point);wheelIv = (ImageView) findViewById(R.id.main_wheel);}//控制燈圈動畫的方法private void flashLights() {Timer timer = new Timer();TimerTask tt = new TimerTask() {@Overridepublic void run() {// 向UI線程發送訊息mHandler.sendEmptyMessage(0);}};// 每隔ONE_WHEEL_TIME毫秒運行tt對象的run方法timer.schedule(tt, 0, ONE_WHEEL_TIME);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
如下:
下載連結:
android之幸運抽獎大輪盤