自定义抽奖
自定义抽奖
版本一
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
public class Crcir extends View implements View.OnClickListener {
private int width;
private int height;
private RectF rectF;
private int[] cicleColor = new int[]{Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"), Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"), Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C")};
private String[] cicleText = new String[]{"美 女","女 神","热 舞","丰 满","性 感","知 性"};
private Paint mPaint;
private int x;
private int y;
private Animation animation;
private boolean isRote;
public Crcir(Context context) {
this(context, null);
}
public Crcir(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public Crcir(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int widthPixels = displayMetrics.widthPixels;
int heightPixels = displayMetrics.heightPixels;
x = widthPixels / 2;
y = heightPixels / 2;
initAnimal();
setOnClickListener(this);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(300, 300);
width = getMeasuredWidth();
height = getMeasuredHeight();
rectF = new RectF(0, 0, width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.translate(x-180,y-200);
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(2);
mPaint.setAntiAlias(true);
for (int i = 0; i < 6; i++) {
mPaint.setColor(cicleColor[i]);
canvas.drawArc(rectF,i*60,60,true,mPaint);
//设置字
mPaint.setColor(Color.BLACK);
mPaint.setTextSize(20);
Path path = new Path();
path.addArc(rectF,i*60,60);
canvas.drawTextOnPath(cicleText[i],path,60,60,mPaint);
}
//设置小圆
float centerX = rectF.centerX();
float centerY = rectF.centerY();
mPaint.setColor(Color.RED);
mPaint.setAntiAlias(true);
canvas.drawCircle(centerX,centerY,50,mPaint);
String text = "奖";
mPaint.measureText(text);
mPaint.setTextSize(20);
mPaint.setColor(Color.WHITE);
canvas.drawText(text,width/2-10,height/2+10,mPaint);
Log.e("w",width+"");
Log.e("h",height+"");
}
private void initAnimal(){
animation = new RotateAnimation(0,360,300,475);
animation.setDuration(300);
animation.setFillAfter(true);
animation.setRepeatCount(-1);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatMode(Animation.RESTART);
}
@Override
public void onClick(View v) {
if (isRote) {
stopAnima();
setRoundDom();
}else {
startAnima();
}
}
private void startAnima() {
isRote=true;
startAnimation(animation);
}
private void stopAnima() {
isRote=false;
clearAnimation();
}
//给一个随机的抽奖结果
private void setRoundDom(){
double random = Math.random();
RotateAnimation rotateAnimation = new RotateAnimation(0, (float) (360*random),x,y);
rotateAnimation.setDuration(2000);
rotateAnimation.setFillAfter(true);
startAnimation(rotateAnimation);
}
}
版本二
public class Cicle extends View implements View.OnClickListener {
private int x;
private int y;
private Paint mPaint;
private int[] mColors;
private int mWidthPixels;
private int mHeightPixels;
private String[] cicleText = new String[]{"美 女","女 神","热 舞","丰 满","性 感","知 性"};
private boolean isRote;
private Animation mAnimation;
public Cicle(Context context) {
this(context,null);
}
public Cicle(Context context, AttributeSet attrs) {
this(context, attrs,-1);
}
public Cicle(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取屏幕宽高
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
mWidthPixels = displayMetrics.widthPixels;
mHeightPixels = displayMetrics.heightPixels;
//获取屏幕中心坐标
x = mWidthPixels / 2;
y = mHeightPixels / 2;
//初始化画笔
initPaint();
mColors = new int[]{Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"), Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"), Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C")};
initAnimation();
setOnClickListener(this);
}
private void initPaint() {
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(20);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(300, 300);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.translate(x,y);
RectF rectF = new RectF(-250,-250,250,250);
float start = 60;
for (int i =0;i<6;i++){
mPaint.setColor(mColors[i]);
canvas.drawArc(rectF,start*i,60,true,mPaint);
}
mPaint.setColor(Color.RED);
canvas.drawCircle(0,0,100,mPaint);
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(40);
Rect rect = new Rect();
mPaint.getTextBounds("start",0,5,rect);
int width = rect.width();
int height = rect.height();
canvas.drawText("start",width/2-80,height/2,mPaint);
RectF rectF1 = new RectF(-200,-200,200,200);
for(int i =0;i<6;i++){
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(30);
Path path = new Path();
path.addArc(rectF1,start*i+20,60);
canvas.drawTextOnPath(cicleText[i],path,0,0,mPaint);
}
}
public void initAnimation(){
mAnimation = new RotateAnimation(0,360,x,y);
mAnimation.setDuration(3000);
mAnimation.setFillAfter(true);
//设置重复次数
mAnimation.setRepeatCount(-1);
//是Animation的xml的一个属性
mAnimation.setInterpolator(new LinearInterpolator());
//设置重复模式
mAnimation.setRepeatMode(Animation.RESTART);
}
private void startAnimation(){
isRote = true;
startAnimation(mAnimation);
}
private void stopAnimation(){
isRote = false;
clearAnimation();
}
@Override
public void onClick(View v) {
if(isRote){
stopAnimation();
setDome();
}else{
startAnimation();
}
}
public void setDome(){
double random = Math.random();
RotateAnimation rotateAnimation = new RotateAnimation(0, (float) (360*random),x,y);
rotateAnimation.setDuration(300);
rotateAnimation.setFillAfter(true);
startAnimation(rotateAnimation);
}
}
下一篇: 手机自定义分区