Implementation of Android: "Lottery wheel" and android Wheel

Source: Internet
Author: User

Implementation of Android: "Lottery wheel" and android Wheel

This example can help us learn the rotation animation and timer knowledge points, not to mention nonsense, 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 {// sets a time constant. This constant has two functions: 1. switch time between the circle light view and hide; 2. the time required for turning the pointer to a circle is now set to 500 ms private static final long ONE_WHEEL_TIME = 500; // record whether the boolean constant private boolean lightsOn = true displayed in the circular lamp view; // The angle when the rotation starts. The initial value is 0 private int startDegree = 0; private ImageView lightIv; private ImageView pointIv; private ImageView wheelIv; // data source private int [] laps = {5, 7, 10, 15}; // The angle data source pointed to by the pointer, because there are 6 options, all the values here are 6 private int [] angles = {0, 60,120,180,240,300}; // turntable content array private String [] lotteryStr = {"Sony PSP ", "10 yuan red packet", "Thank you for participating", "DNF wallet", "OPPO MP3", "5 yuan red packet ",}; // handler object private Handler mHandler = new Handler () {public void handleMessage (android. OS. message msg) {switch (msg. what) {case 0: if (lightsOn) {// set lightIv to be invisible. setVisibility (View. INVISIBLE); lightsOn = false;} else {// set lightIv to 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 % 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)]; // increment int increaseDegree = lap * 360 + angle for each circle angle; // initialize the rotation animation, the following four parameters are used to set RotateAnimation rotateAnimation = new RotateAnimation (startDegree, startDegree + increaseDegree, RotateAnimation. RELATIVE_TO_SELF, 0.5f, RotateAnimation. RELATIVE_TO_SELF, 0.5f); // assign the final angle to startDegree as the initial angle of the next circle startDegree + = increaseDegree; // calculate the total animation playback time long time = (lap + angle/360) * ONE_WHEEL_TIME; // set the animation playback time rotateAnimation. setDuration (time); // sets rotateAnimation on the last frame after the animation is played. setFillAfter (true); // sets the acceleration behavior of the animation. It is to accelerate the animation first and then slow down rotateAnimation. setInterpolator (MainActivity. this, android. r. anim. accelerate_decelerate_interpolator); // sets the animation listener rotateAnimation. setAnimationListener (al); // start playing the animation 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);} // method for controlling the lamp circle animation private void flashLights () {Timer timer = new Timer (); TimerTask tt = new TimerTask () {@ Overridepublic void run () {// send the message mHandler to the UI thread. sendEmptyMessage (0) ;}}; // run the run method timer of the tt object in milliseconds every ONE_WHEEL_TIME. 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 ;}}


As follows:



Download the complete project code:

Lucky Draw for android

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.