Android实现抽奖转盘实例代码
本文详述了android抽奖程序的实现方法,程序为一个抽奖大转盘代码,里面定义了很多图形方法和动画。
实现主要功能的SlyderView.java源代码如下:
importandroid.app.Activity;
importandroid.content.Context;
importandroid.graphics.BlurMaskFilter;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.ColorMatrixColorFilter;
importandroid.graphics.EmbossMaskFilter;
importandroid.graphics.MaskFilter;
importandroid.graphics.Paint;
importandroid.graphics.PorterDuffXfermode;
importandroid.graphics.Paint.Style;
importandroid.graphics.PorterDuff.Mode;
importandroid.graphics.Path;
importandroid.graphics.RadialGradient;
importandroid.graphics.RectF;
importandroid.graphics.Shader.TileMode;
importandroid.util.AttributeSet;
importandroid.util.TypedValue;
importandroid.view.View;
publicclassSlyderViewextendsView{
publicSlyderView(Contextcontext,AttributeSetattrs,intdefStyleAttr){
super(context,attrs,defStyleAttr);
init(context);
}
publicSlyderView(Contextcontext,AttributeSetattrs){
super(context,attrs);
init(context);
}
publicSlyderView(Contextcontext){
super(context);
init(context);
}
/**
*屏幕宽度
*/
privateintscreenW;
/**
*屏幕的高度
*/
privateintscreenH;
/**
*分割的度数
*/
privateint[]drgrees={20,50,40,90,70,40,50};
/***
*分割的文字
*/
privateString[]strs={"level1","level2","level3","level4","level5","level6","level7"};
/**
*分割的颜色
*/
privateint[]colos=newint[]{0xfed9c960,0xfe57c8c8,0xfe9fe558,0xfef6b000,0xfef46212,0xfecf2911,0xfe9d3011};
/**
*画笔
*/
privatePaintpaint;
/**
*文字的大小
*/
privatefloattextSize=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,15,getResources().getDisplayMetrics());
/**
*文字的颜色
*/
privateinttextcolor=Color.WHITE;
/**
*园的半径
*/
privatefloatradius=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,100,getResources().getDisplayMetrics());
/**
*画文字的距离
*/
privatefloattextdis=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,80,getResources().getDisplayMetrics());
/**
*画箭头的大小
*/
privatefloatroketSize=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,10,getResources().getDisplayMetrics());
privatefloatinitDegress=0;
/**
*圆心
*/
privatefloatcenterX;
/**
*圆心
*/
privatefloatcenterY;
/**
*立体边缘
*/
privateMaskFilterfilter=newEmbossMaskFilter(newfloat[]{1,1,1},0.4f,6,3.5f);
privateMaskFilterouterFilter=newBlurMaskFilter(10,BlurMaskFilter.Blur.OUTER);
privateMaskFilterinnerFilter=newBlurMaskFilter(10,BlurMaskFilter.Blur.INNER);
@SuppressWarnings("deprecation")
privatevoidinit(Contextcontext){
paint=newPaint();
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=newint[3];
colores[0]=Color.rgb(0xfF,0x99,0x00);
colores[1]=Color.rgb(0xff,0xff,0x00);
colores[2]=Color.rgb(0xff,0x99,0x00);
float[]positions=newfloat[3];
positions[0]=0.0f;
positions[1]=0.5f;
positions[2]=1.0f;
gradient=newRadialGradient(centerX,centerY,radius/5,colores,positions,TileMode.CLAMP);
}
/**
*绘制三角箭头
*/
privatePathpath=newPath();
/**
*绘制矩形框
*/
privateRectFoval;
/**
*外圆内阴影矩阵
*/
privateColorMatrixColorFiltercolorFilter=newColorMatrixColorFilter(newfloat[]{
1,0,0,0,0,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,-1,255
});
@Override
protectedvoidonDraw(Canvascanvas){
super.onDraw(canvas);
centerX=screenW/2;
centerY=screenH/2;
oval=newRectF(centerX-radius,centerY-radius,centerX+radius,centerY+radius);
floatstart=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(inti=0;i<drgrees.length;i++){
floatsweepAngle=drgrees[i];
floatstartAngle=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(inti=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];
}
intsaveCount=canvas.save();
//画外层立体效果
paint.setColorFilter(colorFilter);
canvas.saveLayer(oval,paint,Canvas.ALL_SAVE_FLAG);
paint.setColorFilter(null);
canvas.drawARGB(255,0,0,0);
paint.setXfermode(newPorterDuffXfermode(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();
}
}
}
}
privatebooleanisRunning=false;
privatebooleanisStoping=false;
privateintstop_degress=90;
/**
*渐变
*/
privateRadialGradientgradient;
publicvoidplay(){
isRunning=true;
invalidate();
}
publicvoidstop(intcount){
for(inti=0;i<=count;i++){
if(i==count){
stop_degress+=drgrees[i]/2;
}else{
stop_degress+=drgrees[i];
}
}
isStoping=true;
isRunning=false;
invalidate();
}
}