Android实现抽奖转盘实例代码
程序员文章站
2022-11-05 15:35:00
本文详述了android抽奖程序的实现方法,程序为一个抽奖大转盘代码,里面定义了很多图形方法和动画。
实现主要功能的slyderview.java源代码如下:...
本文详述了android抽奖程序的实现方法,程序为一个抽奖大转盘代码,里面定义了很多图形方法和动画。
实现主要功能的slyderview.java源代码如下:
import android.app.activity; import android.content.context; import android.graphics.blurmaskfilter; import android.graphics.canvas; import android.graphics.color; import android.graphics.colormatrixcolorfilter; import android.graphics.embossmaskfilter; import android.graphics.maskfilter; import android.graphics.paint; import android.graphics.porterduffxfermode; import android.graphics.paint.style; import android.graphics.porterduff.mode; import android.graphics.path; import android.graphics.radialgradient; import android.graphics.rectf; import android.graphics.shader.tilemode; import android.util.attributeset; import android.util.typedvalue; import android.view.view; public class slyderview extends view{ public slyderview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(context); } public slyderview(context context, attributeset attrs) { super(context, attrs); init(context); } public slyderview(context context) { super(context); init(context); } /** * 屏幕宽度 */ private int screenw; /** * 屏幕的高度 */ private int screenh; /** * 分割的度数 */ private int [] drgrees = {20,50,40,90,70,40,50}; /*** * 分割的文字 */ private string [] strs = {"level1","level2","level3","level4","level5","level6","level7"}; /** * 分割的颜色 */ private int [] colos = new int[] { 0xfed9c960, 0xfe57c8c8, 0xfe9fe558, 0xfef6b000, 0xfef46212, 0xfecf2911, 0xfe9d3011 }; /** * 画笔 */ private paint paint; /** * 文字的大小 */ private float textsize = typedvalue.applydimension(typedvalue.complex_unit_sp, 15, getresources().getdisplaymetrics()); /** * 文字的颜色 */ private int textcolor = color.white; /** * 园的半径 */ private float radius = typedvalue.applydimension(typedvalue.complex_unit_sp, 100, getresources().getdisplaymetrics()); /** * 画文字的距离 */ private float textdis = typedvalue.applydimension(typedvalue.complex_unit_sp, 80, getresources().getdisplaymetrics()); /** * 画箭头的大小 */ private float roketsize = typedvalue.applydimension(typedvalue.complex_unit_sp, 10, getresources().getdisplaymetrics()); private float initdegress = 0; /** * 圆心 */ private float centerx; /** * 圆心 */ private float centery; /** * 立体边缘 */ private maskfilter filter = new embossmaskfilter(new float[] { 1, 1, 1 },0.4f, 6, 3.5f); private maskfilter outerfilter = new blurmaskfilter(10, blurmaskfilter.blur.outer); private maskfilter innerfilter = new blurmaskfilter(10, blurmaskfilter.blur.inner); @suppresswarnings("deprecation") private void init(context context){ paint = new paint(); paint.setantialias(true); paint.setstyle(style.fill); paint.setcolor(color.white); screenw = ((activity)context).getwindowmanager().getdefaultdisplay().getwidth(); screenh = ((activity)context).getwindowmanager().getdefaultdisplay().getheight(); int[] colores = new int[3]; colores[0] = color.rgb(0xff, 0x99, 0x00); colores[1] = color.rgb(0xff, 0xff, 0x00); colores[2] = color.rgb(0xff, 0x99, 0x00); float[] positions = new float[3]; positions[0] = 0.0f; positions[1] = 0.5f; positions[2] = 1.0f; gradient = new radialgradient(centerx, centery, radius/5, colores, positions, tilemode.clamp); } /** * 绘制三角箭头 */ private path path = new path(); /** * 绘制矩形框 */ private rectf oval; /** * 外圆内阴影矩阵 */ private colormatrixcolorfilter colorfilter = new colormatrixcolorfilter(new float[]{ 1,0,0,0,0, 0,1,0,0,0, 0,0,1,0,0, 0,0,0,-1,255 }); @override protected void ondraw(canvas canvas) { super.ondraw(canvas); centerx = screenw/2; centery = screenh/2; oval = new rectf(centerx-radius,centery-radius,centerx+radius,centery+radius); float start = 0; paint.setcolor(color.rgb(0xdd, 0xdd, 0xdd)); paint.setalpha(127); canvas.drawcircle(centerx, centery, radius+10, paint); paint.setalpha(255); //画扇形 paint.setantialias(true); for(int i=0;i<drgrees.length;i++){ float sweepangle = drgrees[i]; float startangle = start; paint.setcolor(colos[i%colos.length]); canvas.drawarc(oval, startangle, sweepangle, true, paint); start += drgrees[i]; } //画文字 paint.setcolor(textcolor); paint.setantialias(true); paint.settextsize(textsize); paint.settextalign(paint.align.right); start = 0; for(int i=0;i<drgrees.length;i++){ canvas.save(); canvas.rotate(start+drgrees[i]/2, centerx, centery); canvas.drawtext(strs[i], centerx+textdis, centery, paint); canvas.restore(); start += drgrees[i]; } int savecount = canvas.save(); //画外层立体效果 paint.setcolorfilter(colorfilter); canvas.savelayer(oval,paint,canvas.all_save_flag); paint.setcolorfilter(null); canvas.drawargb(255, 0, 0, 0); paint.setxfermode(new porterduffxfermode(mode.clear)); canvas.drawcircle(centerx, centery, radius, paint); paint.setxfermode(null); paint.setmaskfilter(innerfilter); paint.setcolor(color.argb(0xff, 0, 0, 0)); canvas.drawcircle(centerx, centery, radius, paint); paint.setmaskfilter(null); canvas.restoretocount(savecount); //画内圆和内园效果 canvas.save(); paint.setcolor(color.argb(0xff, 0, 0, 0)); paint.setantialias(true); paint.setmaskfilter(outerfilter); canvas.rotate(initdegress, centerx, centery); canvas.drawcircle(centerx, centery, radius/3, paint); paint.setmaskfilter(null); paint.setcolor(color.white); canvas.drawcircle(centerx, centery, radius/3, paint); //画三角型叠加当箭头 path.moveto(centerx-radius/3, centery); path.lineto(centerx, centery-radius/3-roketsize); path.lineto(centerx+radius/3, centery); path.close(); canvas.drawpath(path, paint); canvas.restore(); paint.setmaskfilter(filter); paint.setcolor(color.green); paint.setshader(gradient); canvas.drawcircle(centerx, centery, radius/5, paint); paint.setmaskfilter(null); paint.setshader(null); //重绘调整三角的指向达到滚动的效果,现实项目中可不能这样用的,效率太低下了,拆分view用动画完成滚动才是王道 if(isrunning){ if(initdegress>=360){ initdegress = 0; } initdegress +=4; invalidate(); } if(isstoping){ if(initdegress<=360){ initdegress+=4; invalidate(); }else{ if(initdegress-360<stop_degress){ initdegress+=2; invalidate(); } } } } private boolean isrunning = false; private boolean isstoping = false; private int stop_degress =90; /** * 渐变 */ private radialgradient gradient; public void play(){ isrunning = true; invalidate(); } public void stop(int count){ for(int i =0;i<=count;i++){ if(i == count){ stop_degress +=drgrees[i]/2; }else{ stop_degress +=drgrees[i]; } } isstoping = true; isrunning = false; invalidate(); } }