/*
* Copyright (C) 2014 Jason Fang ( ijasonfang@gmail.com )
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except 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 distributed 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 android.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 2015年1月29日 下午7:19:36
*/
@TargetApi(Build.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;
private int mUnitDegree;
private int[] mLevelMap;
private long mTimeout = LOTTERY_TIME_OUT; 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 imageView, 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 = LOADING_UNIT_DEGREE - mUnitDegree / 2;
} else {
stopDegree = (LOADING_UNIT_DEGREE - 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) / ((LOADING_UNIT_DEGREE / (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 LotteryAnimatorListener(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);
}
}

如果要指针初始化指向圆盘的缝隙. 需要做简要的修改!

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)  //总共几个奖
.setLevelMap(new int[]{5, 1, 1, 1, 1, 1, 1, 1, 1, 1}) //映射奖项
.setTimeOut(4);

获取到服务器端值之后调用

mLottery.stop(5);  //参数为几等奖

今天分享一个抽奖的类Lottery的更多相关文章

  1. 分享一个Snackbar工具类 SnackbarUtils;

    分享一个Snackbar工具类,源代码也是在Github上面找的,自己做了一下修改: 功能如下: 1:设置Snackbar显示时间长短                 1.1:Snackbar.LEN ...

  2. [分享]一个String工具类,也许你的项目中会用得到

    每次做项目都会遇到字符串的处理,每次都会去写一个StringUtil,完成一些功能. 但其实每次要的功能都差不多: 1.判断类(包括NULL和空串.是否是空白字符串等) 2.默认值 3.去空白(tri ...

  3. 分享一个Redis帮助类

    最近在项目中使用了redis来存储已经下载过的URL,项目中用的是ServiceStack来操作Redis,一开始ServiceStack的版本用的是最新的,后来发现ServiceStack已经商业化 ...

  4. 分享一个FileUtil工具类,基本满足web开发中的文件上传,单个文件下载,多个文件下载的需求

    获取该FileUtil工具类具体演示,公众号内回复fileutil20200501即可. package com.example.demo.util; import javax.servlet.htt ...

  5. 分享一个手机端好用的jquery ajax分页类

    分享一个手机端好用的jquery ajax分页类 jquery-ias.min.js 1,引入jquery-ias.min.js 2,调用ajax分页 <script type="te ...

  6. .Net Excel 导出图表Demo(柱状图,多标签页) .net工具类 分享一个简单的随机分红包的实现方式

    .Net Excel 导出图表Demo(柱状图,多标签页) 1 使用插件名称Epplus,多个Sheet页数据应用,Demo为柱状图(Epplus支持多种图表) 2 Epplus 的安装和引用 新建一 ...

  7. 分享一个SqliteHelper类

    分享一个SqliteHelper类 SQLite作为一个本地文件数据库相当好用,小巧.快速.支持事务.关系型,甚至可以运行在Android上.在很久以前的一个项目中,我们用过它来将接收到的数据做本地统 ...

  8. 分享一个简单的C#的通用DbHelper类(支持数据连接池)

    每次新项目的时候,都要从头去找一遍数据库工具类.这里分享一个简单实用的C#的通用DbHelper工具类,支持数据连接池. 连接池配置 <connectionStrings> <add ...

  9. 分享一个自己用的Objective-C的Http接连类

    很久没有更新博客了,所以分享一个. @protocol HttpListenerDelegate; @interface BaseHttp : NSObject { } @property (nona ...

随机推荐

  1. WCF启用Session

    1 服务类添加ASPNETSESSION兼容标记 [System.ServiceModel.Activation.AspNetCompatibilityRequirements(Requirement ...

  2. 下破解安装Python开发工具WingIDE4.1

    步骤: 1.将系统时间调整到一个月之前,然后执行安装. 可以使用date命令调整系统时间,如:date -s '2012-08-14 10:00:00' 2.安装成功后,打开程序,按照提示信息,申请一 ...

  3. bzoj 1419 Red is good(期望DP)

    [题意] R红B蓝,选红得1选蓝失1,问最优状态下的期望得分. [思路] 设f[i][j]为i个Rj个B时的最优期望得分,则有转移式为: f[i][j]=max{ 0,(f[i-1][j]+1)*(i ...

  4. web服务器分析与设计(五)--一些总结

    随着年龄与经验的增加,对于软件方面的分析与设计也会有一些新的认识.下面做个近期的总结: 1,关于到底用不用作设计的问题: 在最近两个公司,原有人马是不会作设计(我自己的感觉),也察觉不到作设计的任何冲 ...

  5. Hive1.3 JDBC连接-代码片段

    package com.hive.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Re ...

  6. 在已创建的DataTable对象中添加在首列一列

    问题描述: 从数据库读取出来的表数据赋给到了DataTable上,将DataTable中数据显示到DataGridView中时希望在DataGridView的第一列显示一列. 解决方法: DataTa ...

  7. poj1000 A+B Problem

    Description Calculate a+b Input Two integer a,b (0<=a,b<=10) Output Output a+b Sample Input 1 ...

  8. 解读XMP元数据中ALAssetRepresentation

    当用户进行某些更改(裁剪,消除红眼,...),在内置 Photos.app iOS上,这些更改将不会应用到由相应 fullResolutionImage 返回的 ALAssetRepresentati ...

  9. WebForm中如何防止页面刷新,后退导致的重复提交

    当用户按下浏览器中的 F5 键刷新当前页面时,对这一过程进行检测所需的操作步骤.页面刷新是浏览器对特定用户操作(按 F5 键或单击"刷新"工具栏按钮)的响应.页面刷新操作是浏览器内 ...

  10. POJ 3670 Eating Together (DP,LIS)

    题意:给定 n 个数,让你修改最少的数,使得它变成一个不下降或者不上升序列. 析:这个就是一个LIS,但是当时并没有看出来...只要求出最长LIS的长度,用总数减去就是答案. 代码如下: #inclu ...