自定义View轮盘抽奖

canvas.rotate(360/count,0,0);//旋转画布
public class LuckyDraw extends View implements  View.OnClickListener {
    private final int widthPixel;
    private final int heightPixel;
    private final int certenX;
    private final int certenY;
    private final int[] colors;
    private Paint paint;
    private String[] desc = new String[]{"性感", "丰满", "知性", "聪明", "贤惠", "优秀"};
    private RotateAnimation rotateAnimation;
    private boolean isRote;//是否在旋转状态

    public LuckyDraw(Context context) {
        this(context,null);
    }

    public LuckyDraw(Context context, AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public LuckyDraw(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取屏幕宽高信息
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        //宽
        widthPixel = metrics.widthPixels;
        //高
        heightPixel = metrics.heightPixels;
        //中心坐标
        certenX = widthPixel/2;
        certenY = heightPixel/2;
        //初始化画笔
        initPaint();
        colors = new int[]{Color.CYAN, Color.GRAY, Color.YELLOW, Color.BLUE, Color.GREEN, Color.DKGRAY, Color.WHITE};
        initAnimation();
        this.setOnClickListener(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        setMeasuredDimension(100,100);
    }
    //绘图
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //移动图的原点
        canvas.translate(certenX,certenY);
        //绘制6个圆弧
        RectF rectF = new RectF(-250,-250,250,250);
        float start = 60;
        for (int i=0;i<6;i++){
            paint.setColor(colors[i]);
            canvas.drawArc(rectF,start*i,60,true,paint);
        }
        //绘制中心的圆
        paint.setColor(Color.RED);
        canvas.drawCircle(0,0,100,paint);
        paint.setColor(Color.WHITE);
        paint.setTextSize(40);
        //获取文字宽和高
        Rect rect = new Rect();
        paint.getTextBounds("start",0,5,rect);
        int width = rect.width();
        int height = rect.height();
        canvas.drawText("start",-width/2,height/2,paint);
        //绘制描述信息
        RectF rectF1 = new RectF(-150, -150, 150, 150);
        for (int i=0;i<6;i++){
            paint.setColor(Color.WHITE);
            Path path = new Path();
            path.addArc(rectF1,start*i+15,60);
            canvas.drawTextOnPath(desc[i],path,0,0,paint);
        }

    }
    //初始化旋转动画
    private void initAnimation() {
        rotateAnimation = new RotateAnimation(0, 360, certenX, certenY);
        rotateAnimation.setDuration(800);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setRepeatCount(-1);
        rotateAnimation.setInterpolator(new LinearInterpolator());
        rotateAnimation.setRepeatMode(Animation.RESTART);
    }
    private void startAnima(){
        isRote=true;
        double random = Math.random();
        RotateAnimation rotateAnimation2 = new RotateAnimation(0, (float) (720 * random), certenX, certenY);
        rotateAnimation2.setDuration(800);
        rotateAnimation2.setFillAfter(true);
        //设置重复次数
//        rotateAnimation2.setRepeatCount(-1);
        rotateAnimation2.setInterpolator(new LinearInterpolator());
        //设置重复模式
        rotateAnimation2.setRepeatMode(Animation.RESTART);
        //给动画添加监听
        rotateAnimation2.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isRote=false;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        startAnimation(this.rotateAnimation);
    }
    private void stopAnima(){
        isRote=false;
        clearAnimation();
    }
    //画笔
    private void initPaint() {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(30);


    }

    @Override
    public void onClick(View v) {
if (isRote){
    stopAnima();
    setRoundDom();
}else {
    startAnima();
}
    }
//    给一个随机的抽奖结果
    private void setRoundDom() {
        double random = Math.random();
        RotateAnimation rotateAnimatio = new RotateAnimation(0, (float) (360*random),certenX,certenY);
        rotateAnimatio.setDuration(100);
        rotateAnimatio.setFillAfter(true);
        startAnimation(rotateAnimatio);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值