实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
package com.demo.lr; import java.util.ArrayList; import android.app.Activity; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; import com.demo.view.DynamicImage; import com.demo.view.LotteryView; import com.demo.view.RotateListener; /** * * <dl> * <dt>MainActivity.java</dt> * <dd>Description: 抽奖页面</dd> * </dl> * * @author abner */ public class MainActivity extends Activity implements RotateListener,OnClickListener { //文字提示 private TextView title; //中奖显示 private TextView info; //抽奖转盘 private LotteryView lotteryView; //指针按钮 private DynamicImage arrowBtn; private int[] itemColor;//选项颜色 private String[] itemText;//选项文字 public ArrayList<String> arrayList; private float surfacViewWidth = 0; private float surfacViewHeight = 0; private SoundPool soundPool = null; private int explosionId = 0; //内存加载ID private int playSourceId = 0; //播放ID public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lottery_rotary); this.setVolumeControlStream(AudioManager.STREAM_MUSIC); initView(); } protected void onResume() { super.onResume(); if (soundPool == null) { //指定声音池的最大音频流数目为10,声音品质为5 soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); //载入音频流,返回在池中的id explosionId = soundPool.load(this, R.raw.music, 1); } } protected void onPause() { super.onPause(); if (soundPool != null) { soundPool.stop(explosionId); soundPool.release(); soundPool = null; } if(lotteryView != null){ lotteryView.rotateDisable(); } } /** * * Description:初始化界面元素 * */ public void initView(){ initItem(); title = (TextView)this.findViewById(R.id.title); info = (TextView)this.findViewById(R.id.info); arrowBtn = (DynamicImage) this.findViewById(R.id.arrowBtn); lotteryView = (LotteryView) this.findViewById(R.id.lotteryView); arrowBtn.setOnClickListener(this); lotteryView.initAll(itemColor, itemText); lotteryView.setRotateListener(this); lotteryView.start(); surfacViewHeight = lotteryView.getHeight(); surfacViewWidth = lotteryView.getWidth(); Log.d("Log", "width = " surfacViewWidth ":height = " surfacViewHeight); } /** * * Description:初始化转盘的颜色,文字 * */ public void initItem(){ // 转盘选项的颜色 itemColor = new int[] { 0xFFB0E0E6,// 粉蓝色 0xFF444444,// 深灰色 0xFF008B8B,// 暗青色 0xFFFFA500,// 橙色 0xFF7FFF00,// 黄绿色 0xFFF08080,// 亮珊瑚色 0xFFB0C4DE,// 亮钢兰色 0xFFFFFFFF // 白色 }; // 转盘选项的名称 itemText = new String[] { "恭喜发财", "智能手机", "5元话费", "2元话费", "1元话费","恭喜发财" }; } /** * * Description:转盘开始旋转 * * @param sp * @param isRoating */ public void begin(float speed, int group, boolean isRoating) { lotteryView.setDirection(speed,group,isRoating); lotteryView.rotateEnable(); /*播放音频,第二个参数为左声道音量;第三个参数为右声道音量; 第四个参数为优先级;第五个参数为循环次数,0不循环,-1循环; 第六个参数为速率,速率 最低0.5最高为2,1代表正常速度 */ playSourceId = soundPool.play(explosionId,1, 1, 0, -1, 1); } public Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { info.setText( (CharSequence) msg.obj); if(!lotteryView.isRotateEnabled()){ soundPool.stop(playSourceId); title.setText("恭喜您获得"); arrowBtn.stopRotation(); } }; }; @Override public void showEndRotate(String str) { Message msg = new Message(); msg.obj = str; handler.sendMessage(msg); } @Override public void onClick(View v) { // 没有旋转状态 if(!lotteryView.isRotateEnabled()){ title.setText("抽奖按钮变红时按下更容易中奖哦"); begin(Math.abs(50),8,false); arrowBtn.startRoation(new int[]{R.drawable.arrow_green,R.drawable.arrow_red}, 200); } //旋转状态 else { //一直旋转状态 if(!lotteryView.isRoating()){ //设置中奖项(随机的话请注释) lotteryView.setAwards(0); //设置为缓慢停止 lotteryView.setRoating(true); title.setText(""); } } } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)