Android's implementation "Lottery Big Roulette"

Source: Internet
Author: User

This example can help us to learn to rotate the animation and timer These two points of knowledge, nonsense not much to say, on the main program:

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 {//Set a time constant, which has two functions, 1. Circle Light View Show and hide middle switch time 2. The time required for the pointer to turn round is now set to 500 milliseconds private static final long One_wheel_time = 500;//record whether the circular light view shows the Boolean constant private Boolean Lightson = True ;//starting at the angle of rotation, the initial value is 0private int startdegree = 0;private ImageView lightiv;private ImageView pointiv;private ImageView wheeliv;//Pointer to circle Number data source Private int[] laps = {5, 7, 10, 15};//The angle data source pointed to by the pointer, because there are 6 options, all here is 6 values private int[] angles = {0, 60, 120 , 180, 240, 300};//turntable content array private string[] Lotterystr = {"Sony PSP", "10 yuan Red envelopes", "Thank you for participating", "DNF wallet", "OPPO MP3", "5 yuanRed envelope ",};//child thread communicates with UI thread Handler object private Handler Mhandler = new Handler () {public void Handlemessage (Android.os.Message msg) {switch (msg.what) {case 0:if (Lightson) {//Set Lightiv not visible lightiv.setvisibility (view.invisible); Lightson = false;} else { Set Lightiv visible lightiv.setvisibility (view.visible); Lightson = true;} break;default:break;}};};/ /listener for animation status 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% 36 0/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 ();p Ointiv.setonclicklistener (new Onclicklistener () {@ Overridepublic void OnClick (View v) {int lap = laps[(int) (Math.random () * 4)];int angle = angles[(int) (Math.random () * 6)];//each turn angle increment int in Creasedegree = lap * + angle;//Initialize the rotation animation, the following four parameters are used to set the center point of their own circles around the circle rotateanimation rotateanimation = new Rotateanimation (Startdegree, Startdegree + increasedegree,rotateanimation.relative_to_self, 0.5f,rotateanimation.relative_to_self , 0.5f);//assigns the last angle to startdegree as the initial angle of the next spin startdegree + = increasedegree;//Calculates the total time of the animation playback long times = (lap + angle/360) * ONE_WH eel_time;//set the animation playback time rotateanimation.setduration,///Set the animation after playback, stay on the last frame screen rotateanimation.setfillafter (true); /Set the acceleration behavior of the animation, is the first acceleration after deceleration rotateanimation.setinterpolator (mainactivity.this,android. R.anim.accelerate_decelerate_interpolator);//Set the listener for animation Rotateanimation.setanimationlistener (AL);// Start playing animation pointiv.startanimation (rotateanimation);}});} private void Setupviews () {Lightiv = (ImageView) Findviewbyid (r.id.light);p Ointiv = (ImageView) Findviewbyid (r.id.point ); Wheeliv = (ImageView) Findviewbyid (R.id.main_wheel);} The method of controlling the light circle animation Private void flashlights () {Timer timer = new Timer (); TimerTask tt = new TimerTask () {@Overridepublic void Run () {//Send message to UI thread Mhandler.sendemptymessage (0);}};/ /Every one_wheel_time milliseconds runs the TT object's Run method 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 PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}


as follows:



Complete Engineering code Download link:

Android Lucky Draw Big Roulette

Android's implementation "Lottery Big Roulette"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.