转盘抽奖

public class DiscView extends View implements View.OnClickListener {
private RotateAnimation rotateAnimation;
private Paint mPaint;
private Paint strPaint;
private int mWidth;
private int mPadding;
private boolean isStart = false;
private RectF rectF;
private String str=“start”;
private String[] contents = new String[]{“美 女”,“女 神”,“热 舞”,“丰 满”,“性 感”,“知 性”};
public DiscView(Context context) {
this(context,null);
}

public DiscView(Context context, AttributeSet attrs) {
    this(context, attrs,0);
}

public DiscView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initPaint();
    initAnim();
    setOnClickListener(this);
}
private void initAnim() {
    //以view的中心点为旋转参考点
    rotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnimation.setRepeatCount(-1);
    rotateAnimation.setFillAfter(true);
}

private void initPaint() {
    strPaint = new Paint();
    strPaint.setStyle(Paint.Style.STROKE);
    strPaint.setAntiAlias(true);
    strPaint.setColor(Color.WHITE);
    strPaint.setStrokeWidth(5);

    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setAntiAlias(true);
    mPaint.setColor(Color.WHITE);
    mPaint.setStrokeWidth(3);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(300,300);
    mWidth = getMeasuredWidth();
    mPadding = 5;
    initRect();
}

private void initRect() {
    rectF = new RectF(0,0,mWidth,mWidth);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //绘制圆
    mPaint.setStyle(Paint.Style.STROKE);
    canvas.drawCircle(mWidth/2,mWidth/2,mWidth/2-mPadding,mPaint);
    //绘制 6个椭圆
    mPaint.setStyle(Paint.Style.FILL);

    initArc(canvas);

    //绘制里面的小圆
    mPaint.setColor(Color.RED);
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(mWidth/2,mWidth/2,50,mPaint);


    mPaint.setColor(Color.WHITE);
    mPaint.setTextSize(24);
    Rect rect = new Rect();
    mPaint.getTextBounds(str, 0, str.length(), rect);
    int strWidth = rect.width();//文本的宽度
    int textHeight = rect.height();//文本的高度
    canvas.drawText(str,mWidth/2-25+25-strWidth/2,mWidth/2+textHeight/2,mPaint);


}
/**
 * 绘制6个弧 和对应的文字
 */
private void initArc(Canvas canvas) {
    for(int i=0;i<6;i++){
        mPaint.setColor(colors[i]);
        canvas.drawArc(rectF,(i-1)*60+60,60,true,mPaint);
    }
    for(int i=0;i<6;i++){
        mPaint.setColor(Color.BLACK);
        Path path = new Path();
        path.addArc(rectF,(i-1)*60+60,60);
        canvas.drawTextOnPath(contents[i],path,60,60,mPaint);
    }
}
public  int[] colors = new int[]{Color.parseColor("#8EE5EE"),Color.parseColor("#FFD700"),Color.parseColor("#FFD39B"),Color.parseColor("#FF8247"),Color.parseColor("#FF34B3"),Color.parseColor("#F0E68C")};
@Override
public void onClick(View view) {
    if(!isStart){
        isStart = true;
        rotateAnimation.setDuration(1000);
        rotateAnimation.setInterpolator(new LinearInterpolator());//不停顿
        startAnimation(rotateAnimation);
    }else{
        isStart = false;
        stopAnim();
    }
}
/**
 * 停止动画
 */
public void stopAnim() {
    clearAnimation();
}

}

作者:zhoushenxian
来源:CSDN
原文:https://blog.csdn.net/coderinchina/article/details/53009809
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值