這篇文章主要介紹Android如何制作抽獎輪盤,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體內(nèi)容如下
main布局(圖片資源請自行尋找,抱歉)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/bigwheelgg" /> <ImageView android:id="@+id/light" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/light" /> <ImageView android:id="@+id/main_wheel" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/bigwheel" /> <ImageView android:id="@+id/point" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/point" /> </FrameLayout>
main代碼
//設(shè)置一個時間常量,此常量有兩個作用,1.圓燈視圖顯示與隱藏中間的切換時間;2.指針轉(zhuǎn)一圈所需要的時間,現(xiàn)設(shè)置為500毫秒 private static final long ONE_WHEEL_TIME = 500; //記錄圓燈視圖是否顯示的布爾常量 private boolean lightsOn = true; //開始轉(zhuǎn)動時候的角度,初始值為0 private int startDegree = 0; private ImageView lightIv; private ImageView pointIv; private ImageView wheelIv; //指針轉(zhuǎn)圈圈數(shù)數(shù)據(jù)源 private int[] laps = { 5, 7, 10, 15 }; //指針所指向的角度數(shù)據(jù)源,因為有6個選項,所有此處是6個值 private int[] angles = { 0, 60, 120, 180, 240, 300 }; //轉(zhuǎn)盤內(nèi)容數(shù)組 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) { // 設(shè)置lightIv不可見 lightIv.setVisibility(View.INVISIBLE); lightsOn = false; } else { // 設(shè)置lightIv可見 lightIv.setVisibility(View.VISIBLE); lightsOn = true; } break; default: break; } }; }; //監(jiān)聽動畫狀態(tài)的監(jiān)聽器 private Animation.AnimationListener al = new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { String name = lotteryStr[startDegree % 360 / 60]; Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setupViews(); flashLights(); pointIv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int lap = laps[(int) (Math.random() * 4)]; int angle = angles[(int) (Math.random() * 6)]; //每次轉(zhuǎn)圈角度增量 int increaseDegree = lap * 360 + angle; //初始化旋轉(zhuǎn)動畫,后面的四個參數(shù)是用來設(shè)置以自己的中心點為圓心轉(zhuǎn)圈 RotateAnimation rotateAnimation = new RotateAnimation( startDegree, startDegree + increaseDegree, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); //將最后的角度賦值給startDegree作為下次轉(zhuǎn)圈的初始角度 startDegree += increaseDegree; //計算動畫播放總時間 long time = (lap + angle / 360) * ONE_WHEEL_TIME; //設(shè)置動畫播放時間 rotateAnimation.setDuration(time); //設(shè)置動畫播放完后,停留在最后一幀畫面上 rotateAnimation.setFillAfter(true); //設(shè)置動畫的加速行為,是先加速后減速 rotateAnimation.setInterpolator(MainActivity.this, android.R.anim.accelerate_decelerate_interpolator); //設(shè)置動畫的監(jiān)聽器 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() { @Override public void run() { // 向UI線程發(fā)送消息 mHandler.sendEmptyMessage(0); } }; // 每隔ONE_WHEEL_TIME毫秒運行tt對象的run方法 timer.schedule(tt, 0, ONE_WHEEL_TIME); } @Override public 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如何制作抽獎輪盤”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享名稱:Android如何制作抽獎輪盤-創(chuàng)新互聯(lián)
本文路徑:http://chinadenli.net/article34/epdpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、微信小程序、App設(shè)計、建站公司、網(wǎng)站設(shè)計公司、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容