Android抽奖轮盘的制作方法
程序员文章站
2023-12-16 11:38:28
本文实例为大家分享了android抽奖轮盘的具体代码,供大家参考,具体内容如下
main布局(图片资源请自行寻找,抱歉)
本文实例为大家分享了android抽奖轮盘的具体代码,供大家参考,具体内容如下
main布局(图片资源请自行寻找,抱歉)
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center"> <imageview android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/bigwheelgg" /> <imageview android:id="@+id/light" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/light" /> <imageview android:id="@+id/main_wheel" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/bigwheel" /> <imageview android:id="@+id/point" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/point" /> </framelayout>
main代码
//设置一个时间常量,此常量有两个作用,1.圆灯视图显示与隐藏中间的切换时间;2.指针转一圈所需要的时间,现设置为500毫秒 private static final long one_wheel_time = 500; //记录圆灯视图是否显示的布尔常量 private boolean lightson = true; //开始转动时候的角度,初始值为0 private int startdegree = 0; private imageview lightiv; private imageview pointiv; private imageview wheeliv; //指针转圈圈数数据源 private int[] laps = { 5, 7, 10, 15 }; //指针所指向的角度数据源,因为有6个选项,所有此处是6个值 private int[] angles = { 0, 60, 120, 180, 240, 300 }; //转盘内容数组 private string[] lotterystr = { "索尼psp", "10元红包", "谢谢参与", "dnf钱包", "oppo mp3", "5元红包", }; //子线程与ui线程通信的handler对象 private handler mhandler = new handler() { public void handlemessage(android.os.message msg) { switch (msg.what) { case 0: if (lightson) { // 设置lightiv不可见 lightiv.setvisibility(view.invisible); lightson = false; } else { // 设置lightiv可见 lightiv.setvisibility(view.visible); lightson = true; } break; default: break; } }; }; //监听动画状态的监听器 private animation.animationlistener al = new animation.animationlistener() { @override public void onanimationstart(animation animation) { // todo auto-generated method stub } @override public void onanimationrepeat(animation animation) { // todo auto-generated method stub } @override public void onanimationend(animation animation) { string name = lotterystr[startdegree % 360 / 60]; toast.maketext(mainactivity.this, name, toast.length_long).show(); } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); setupviews(); flashlights(); pointiv.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int lap = laps[(int) (math.random() * 4)]; int angle = angles[(int) (math.random() * 6)]; //每次转圈角度增量 int increasedegree = lap * 360 + angle; //初始化旋转动画,后面的四个参数是用来设置以自己的中心点为圆心转圈 rotateanimation rotateanimation = new rotateanimation( startdegree, startdegree + increasedegree, rotateanimation.relative_to_self, 0.5f, rotateanimation.relative_to_self, 0.5f); //将最后的角度赋值给startdegree作为下次转圈的初始角度 startdegree += increasedegree; //计算动画播放总时间 long time = (lap + angle / 360) * one_wheel_time; //设置动画播放时间 rotateanimation.setduration(time); //设置动画播放完后,停留在最后一帧画面上 rotateanimation.setfillafter(true); //设置动画的加速行为,是先加速后减速 rotateanimation.setinterpolator(mainactivity.this, android.r.anim.accelerate_decelerate_interpolator); //设置动画的监听器 rotateanimation.setanimationlistener(al); //开始播放动画 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); } //控制灯圈动画的方法 private void flashlights() { timer timer = new timer(); timertask tt = new timertask() { @override public void run() { // 向ui线程发送消息 mhandler.sendemptymessage(0); } }; // 每隔one_wheel_time毫秒运行tt对象的run方法 timer.schedule(tt, 0, one_wheel_time); } @override public 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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。