Today, we will share a Lottery-like lottery and Lottery.

Source: Internet
Author: User

Today, we will share a Lottery-like lottery and Lottery.

/** Copyright (C) 2014 Jason Fang (ijasonfang@gmail.com) ** Licensed under the Apache License, Version 2.0 (the "License "); * you may not use this file before t in compliance with the License. * You may obtain a copy of the License at ** http://www.apache.org/licenses/LICENSE-2.0 ** Unless required by applicable law or agreed to in writing, software * distributed under the License is distribute D on an "as is" BASIS, * without warranties or conditions of any kind, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package fyc. framework. anim; import java. util. concurrent. timeUnit; import android. animation. animator; import android. animation. animator. animatorListener; import android. animation. objectAnimator; import Ndroid. animation. valueAnimator; import android. annotation. targetApi; import android. OS. build; import android. OS. countDownTimer; import android. view. animation. decelerateInterpolator; import android. view. animation. linearInterpolator; import android. widget. imageView; import fyc. framework. util. flog; import fyc. framework. util. randomUtils;/*** @ author Jason Fang * @ datetime 7:19:36 on January 1, January 29, 2015 */@ TargetApi (Bu Ild. VERSION_CODES.HONEYCOMB) public class Lottery {static final boolean DEBUG = true; private static final int LOADING_UNIT_DEGREE = 360*5; private static final int LOADING_DURATION = 720*5; private static final long LOTTERY_TIME_OUT = TimeUnit. SECONDS. toMillis (5); private ImageView mImageView; private ObjectAnimator mLoadingAnim; private float mInitDegree = 0; private int mMaxLevel = 10; priv Ate int mUnitDegree; private int [] mLevelMap; private long mTimeout = timeout; private boolean mIsLottering = false; private boolean mIsInStop = false; private OnLotteryListener mListener; private LotteryTimeOut mLotteryTimeOut; private Lottery (ImageView imageView, OnLotteryListener listener) {mImageView = imageView; mListener = listener;} public static Lottery newInstance (ImageView ima GeView, OnLotteryListener listener) {return new Lottery (imageView, listener);} public Lottery setLevel (int level) {mMaxLevel = level; mUnitDegree = 360/mMaxLevel; return this ;} public Lottery setLevelMap (int [] levelMap) {if (levelMap. length! = MMaxLevel) {throw new IllegalArgumentException ("levelMap length must equal MaxLevel! ");} MLevelMap = levelMap; return this;} public Lottery setTimeOut (int timeout) {if (timeout <= 0) return this; mTimeout = TimeUnit. SECONDS. toMillis (timeout); return this;} public Lottery start () {if (mIsLottering) return this; mIsLottering = true; if (DEBUG) Flog. I ("start"); loadingAnimStart (); if (mListener! = Null) {mListener. onLotteryStart ();} mLotteryTimeOut = new LotteryTimeOut (); mLotteryTimeOut. start (); return this;} public void stop (int level) {if (mIsInStop) return; mIsInStop = true; if (mLotteryTimeOut! = Null) {mLotteryTimeOut. cancel ();} int levelAward = getLevelAward (level); if (mLoadingAnim! = Null & mLoadingAnim. isRunning () {mLoadingAnim. cancel ();} if (levelAward <0 | levelAward> mMaxLevel) {throw new IllegalArgumentException ("level cannot below 0 or than MaxLevel! ");} Float stopDegree = 0; if (levelAward = 0) {stopDegree = queue-mUnitDegree/2;} else {stopDegree = (queue-mUnitDegree/2) + RandomUtils. getRandom (mUnitDegree * levelAward + 5, mUnitDegree * (levelAward + 1)-5);} float startDegree = 0f; if (mLoadingAnim! = Null) {startDegree = (Float) mLoadingAnim. getAnimatedValue () % 360;} else {throw new RuntimeException ("Must invoke start first! ");} Long duration = (long) (stopDegree-startDegree)/(vertex/(float) LOADING_DURATION); stopAnimStart (startDegree, stopDegree, duration, levelAward ); mInitDegree = stopDegree;} int getLevelAward (int level) {if (mLevelMap = null | mLevelMap. length = 0 | level = 0) return level; return mLevelMap [level-1];} void loadingAnimStart () {mLoadingAnim = ObjectAnimator. ofFloat (MImageView, "rotation", mInitDegree % 360, LOADING_UNIT_DEGREE); mLoadingAnim. setInterpolator (new LinearInterpolator (); mLoadingAnim. setRepeatCount (ValueAnimator. INFINITE); mLoadingAnim. setDuration (LOADING_DURATION); mLoadingAnim. start ();} void stopAnimStart (float startDegree, float stopDegree, long duration, int levelAward) {ObjectAnimator anim = ObjectAnimator. ofFloat (mImageView, "rotation ", StartDegree, stopDegree); anim. setInterpolator (new DecelerateInterpolator (); anim. setDuration (duration * 2); anim. addListener (new LotteryAnimatorListener (levelAward); anim. start ();} class LotteryTimeOut extends CountDownTimer {public LotteryTimeOut () {super (mTimeout, mTimeout);} @ Override public void onTick (long millisUntilFinished) {}@ Override public void onFinish () {stop (0) ;}} class LotteryAnimatorListener implements AnimatorListener {private int mLevel; public LotteryAnimatorListener () {} public listener (int level) {mLevel = level;} @ Override public void onAnimationStart (Animator animation) {}@ Override public void onAnimationEnd (Animator animation) {if (mListener! = Null) {mListener. onLotteryStop (mLevel); mIsLottering = false; mIsInStop = false ;}@override public void onAnimationCancel (Animator animation) {}@ Override public void onAnimationRepeat (Animator animation) {}} public interface OnLotteryListener {public void onLotteryStart (); public void onLotteryStop (int level );}}

If you want to initialize the gap pointing to the disc, you need to make a brief modification!

DEMO

MLottery = Lottery. newInstance (mWheel, new OnLotteryListener () {@ Override public void onLotteryStop (int level) {Flog. I ("onLotteryStop:" + level) ;}@ Override public void onLotteryStart () {Flog. I ("onLotteryStart ");}}). setLevel (10) // a total of several awards. setLevelMap (new int [] {5, 1, 1, 1, 1, 1, 1, 1, 1}) // ing award. setTimeOut (4 );

After obtaining the server value, call

MLottery. stop (5); // The parameter number.

 

Related Article

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.