自定义view实现抽奖转盘

------>自定义view类
public class LotteryView extends View implements View.OnClickListener {

    private Paint mPaint;
    private DisplayMetrics displayMetrics;
    private int widthPixels;
    private int heightPixels;
    private int centerX;
    private int centerY;
    private int[] colors;
    private String[] descPath = new String[]{
            "亲一下","抱一下","喝一杯","找人喝","一口闷","大冒险"
    };
    private boolean isPath;//定义一个变量用于判断旋转状态

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

    public LotteryView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public LotteryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //获取屏幕宽高信息
        displayMetrics = context.getResources().getDisplayMetrics();
        widthPixels = displayMetrics.widthPixels;//宽
        heightPixels = displayMetrics.heightPixels;//高
        //获取屏幕中心坐标
        centerX = widthPixels / 2;
        centerY = heightPixels / 2;
        //初始化画笔
        initPaint();
        //给每个扇形弧度添加颜色
        colors = new int[]{
                Color.CYAN, Color.GRAY, Color.DKGRAY,
                Color.BLUE, Color.GREEN, Color.RED
        };
        //给自己添加点击事件
        this.setOnClickListener(this);
    }

    //测量view大小
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }

    //绘图
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //移动画布的坐标原点
        canvas.translate(centerX, centerY);
        //绘制6个弧度,扇形,并添加颜色
        RectF f = new RectF(-300, -300, 300, 300);
        float start = 60;
        for (int i = 0; i < 6; i++) {
            mPaint.setColor(colors[i]);
            canvas.drawArc(f, start * i, 60, true, mPaint);
        }

        //绘制中心的圆
        mPaint.setColor(Color.BLUE);//设置中间圆的颜色
        canvas.drawCircle(0, 0, 100, mPaint);
        mPaint.setColor(Color.WHITE);//设置字体颜色
        mPaint.setTextSize(30);//设置字体大小
        //获取文字的宽高
        Rect rectText = new Rect();
        mPaint.getTextBounds("start", 0, 5, rectText);
        int width = rectText.width();//宽
        int height = rectText.height();//高
        canvas.drawText("start", -width / 2, height / 2, mPaint);

        //绘制描述信息
        RectF rectF = new RectF(-200,-200,200,200);
        for (int i = 0 ; i < 6 ; i++){
            mPaint.setColor(Color.WHITE);
            Path path = new Path();
            path.addArc(rectF,start * i + 15,60);
            canvas.drawTextOnPath(descPath[i],path,0,0,mPaint);
        }
    }


    //初始化画笔
    private void initPaint() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(20);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
    }


    @Override
    public void onClick(View view) {
        if (!isPath){
            //旋转状态
            startAnima();
        }
    }
    //旋转状态
    private void startAnima() {
        isPath = true;
        double random = Math.random();
        RotateAnimation rotateAnimation = new RotateAnimation(0, (float) (720 * random), centerX, centerY);
        rotateAnimation.setDuration(800);
        rotateAnimation.setFillAfter(true);
        //设置重复次数
        rotateAnimation.setRepeatMode(Animation.RESTART);

        //给动画添加监听
        rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

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

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        startAnimation(rotateAnimation);
    }

    //定制随机数,给出一个随机结果
    private void setRoundDom(){
        double random = Math.random();
        RotateAnimation rotateAnimation = new RotateAnimation(0, (float) (360 * random), centerX, centerY);
        rotateAnimation.setDuration(100);
        rotateAnimation.setFillAfter(true);
        startAnimation(rotateAnimation);
    }

}

猜你喜欢

转载自blog.csdn.net/fangShiKang/article/details/83742821