欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义View实现抽奖转盘

程序员文章站 2023-08-31 14:41:06
本文实例为大家分享了android自定义view实现抽奖转盘的具体代码,供大家参考,具体内容如下 public class luckcircle extends...

本文实例为大家分享了android自定义view实现抽奖转盘的具体代码,供大家参考,具体内容如下

public class luckcircle extends surfaceview implements surfaceholder.callback,runnable {
 
 private surfaceholder mholder;
 
 private canvas mcanvas;
 
 //用于绘制的线程
 
 private thread mthread;
 
 //线程开关的控制
 
 private boolean isrunning;
 
 private string[] mstr = new string[]{"优惠券","十元话费","恭喜发财","恭喜发财","英雄皮肤","50m流量"};

 //物品的图片
 
 private int[] mimgs = new int[]{r.mipmap.ic_launcher,
 r.mipmap.ic_launcher,r.mipmap.ic_launcher,
 r.mipmap.ic_launcher,r.mipmap.ic_launcher,r.mipmap.ic_launcher};
 
 private int mitemcount = 6;
 
 //盘快的颜色
 
 private int[] mcolor = new int[]{0xffffc300,0xffd9b114,0xffdc0b2e,0xff5510a4,0xff447c42,0xffec3636};

 //与图片对应的bitmap数组
 
 private bitmap[] mimgbitmap;

 //整个盘块的范围
 
 private rectf mrange = new rectf();
 
 //整个盘块的直径

 private int mradius;
 
 
 //绘制盘块的画笔
 
 private paint marcpaint;
 
 
 //绘制文本的画笔
 
 private paint mtextpaint;
 
 //滚动速度
 
 private double mspeed = 10;
 
 
 //绘制的角度

 private volatile int mstartangle = 0;
 
 
 //判断是否点击了停止按钮
 
 private boolean isshouldend;
 
 
 //转盘的中心位置

 private int mcenter;
 
 //padding取四个padding中的最小值
 
 private int mpadding;
 
 //背景图
 
 //private bitmap mbgbitmap = bitmapfactory.decoderesource(getresources(),r.mipmap.ic_launcher);
 
 private float mtextsize = typedvalue.applydimension(typedvalue.complex_unit_sp,20,getresources().getdisplaymetrics());
 public luckcircle(context context) {
 this(context,null);
 }
 
 public luckcircle(context context, attributeset attrs) {
 super(context, attrs);
 mholder = getholder();
 mholder.addcallback(this);
 // 可获得焦点
 setfocusable(true);
 setfocusableintouchmode(true);
 // 设置常亮
 setkeepscreenon(true);
 }
 
 
 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
 super.onmeasure(widthmeasurespec, heightmeasurespec);
 int width = math.min(getmeasuredwidth(),getmeasuredheight());
 
 mpadding = getpaddingleft();
 
 mradius = width - mpadding *2;
 
 mcenter = width / 2;
 setmeasureddimension(width,width);
 }
 
 @override
 public void surfacecreated(surfaceholder holder) {
 // 初始化绘制盘块的画笔
 marcpaint = new paint();
 marcpaint.setantialias(true);
 marcpaint.setdither(true);
 
 // 初始化绘制盘块的画笔
 mtextpaint = new paint();
 mtextpaint.setcolor(0xff0b25cf);
 mtextpaint.settextsize(mtextsize);
 
 // 初始化盘块绘制的范围
 mrange = new rectf(mpadding,mpadding,mpadding+mradius,mpadding+mradius);
 
 // 初始化图片
 mimgbitmap = new bitmap[mitemcount];
 for (int i = 0; i < mimgbitmap.length; i++) {
 mimgbitmap[i] = bitmapfactory.decoderesource(getresources(),mimgs[i]);
 }
 isrunning = true;
 mthread = new thread(this);
 mthread.start();
 }
 
 @override
 public void surfacechanged(surfaceholder holder, int format, int width, int height) {
 
 }
 
 @override
 public void surfacedestroyed(surfaceholder holder) {
 isrunning = false;
 
 }
 
 @override
 public void run() {
 while(isrunning){
 long start = system.currenttimemillis();
 draw();
 long end = system.currenttimemillis();
 if (end - start < 50) {
 try {
 thread.sleep(50 - (end - start));
 } catch (interruptedexception e) {
 e.printstacktrace();
 }
 }
 }
 }
 private void draw(){
 try {
 mcanvas = mholder.lockcanvas();
 if (mcanvas != null) {
 // 绘制背景
 drawbackground();
 // 绘制盘块
 float tmpangle = mstartangle;
 float sweepangle = 360 /mitemcount;
 for (int i = 0; i < mitemcount; i++) {
 marcpaint.setcolor(mcolor[i]);
 // 绘制盘块
 mcanvas.drawarc(mrange,tmpangle,sweepangle,true,marcpaint);
 
 // 绘制文本
 drawtext(tmpangle,sweepangle,mstr[i]);
 // 绘制icon
 drawicon(tmpangle,mimgbitmap[i]);
 tmpangle += sweepangle;
 }
 mstartangle += mspeed;
 // 如果点击了停止按钮
 if (isshouldend) {
 mspeed -= 1;
 }
 if (mspeed <= 0) {
 mspeed = 0;
 isshouldend = false;
 }
 }
 }catch (exception e){
 
 }finally {
 if (mcanvas != null) {
 // 释放canvas
 mholder.unlockcanvasandpost(mcanvas);
 }
 }
 }
 
 
 //点击启动旋转
 
 public void luckystart(){
 mspeed = 50;
 isshouldend = false;
 }
 public void luckend(){
 isshouldend = true;
 }
 public boolean isstart(){
 return mspeed != 0;
 }
 public boolean isshouldend(){
 return isshouldend;
 }
 
 //绘制icon
 
 private void drawicon(float tmpangle, bitmap bitmap) {
 // 设置图片的宽度为直径的1/8;
 int imgwidth = mradius / 8;
 
 float angle = (float) ((tmpangle + 360 / mitemcount / 2)* math.pi/180);
 
 int x = (int) (mcenter + mradius/2/2 * math.cos(angle));
 int y = (int) (mcenter + mradius/2/2 * math.sin(angle));
 // 确定图片的位置
 rect rect = new rect(x - imgwidth/2, y - imgwidth/2, x + imgwidth/2, y + imgwidth/2);
 mcanvas.drawbitmap(bitmap,null,rect,null);
 
 }
 
 
 //绘制每个盘块的文本
 
 private void drawtext(float tmpangle, float sweepangle, string s) {
 path path = new path();
 path.addarc(mrange,tmpangle,sweepangle);
 // 利用水平偏移量让文字居中
 float measuretext = mtextpaint.measuretext(s);
 int hoffset = (int) (mradius * math.pi/mitemcount/2 - measuretext/2);
 int voffset = mradius /2/6;
 mcanvas.drawtextonpath(s,path,hoffset,voffset,mtextpaint);
 }
 
 private void drawbackground() {
 mcanvas.drawcolor(0xff696565);
 paint paint = new paint();
 paint.setcolor(0xfff94905);
 mcanvas.drawcircle(getwidth()/2,getheight()/2,getwidth()/2,paint);
 
 }
}

效果图:

Android自定义View实现抽奖转盘

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。