彩票View的实现

Android 实现Lotto彩票池View的实现

项目地址:https://github.com/Warkey1991/Jackview

最近项目中需要用到一个类似彩票池的效果View
在这里插入图片描述
上图看上去还是很简单的,下面我就分析一下实现的原理:

  • 绘制黑色背景圆
  • 绘制中间的圆弧以及文字
  • 绘制内边圆
  • 绘制里面的小球(此处比较复杂点)
  • 绘制进度圆弧
1.绘制黑色圆背景

就是简单的绘制圆的方法,实心圆只需要将画笔设置为

paint.setStyle(Paint.Style.FILL_AND_STROKE);

即可。

2.绘制中间圆弧

此处需要计算每段圆弧的间距,此处我将每段间距设置为10度,见代码

//绘制圆弧
        rectF.set(-midRadius, -midRadius, midRadius, midRadius);
        canvas.drawArc(rectF, -85, 80, false, linePaint); //5
        canvas.drawArc(rectF, 5, 80, false, linePaint);  //10---- 85
        canvas.drawArc(rectF, 95, 80, false, linePaint);
        canvas.drawArc(rectF, 185, 80, false, linePaint);
3.绘制文字,

设计绘制文本的时候,需要注意的是确定x,y的值。

canvas.drawText("0", -textPaint.measureText("0") / 2, -midRadius + getTextDiffY(textPaint), textPaint);
        canvas.drawText("30", midRadius - textPaint.measureText("30") / 2, getTextDiffY(textPaint), textPaint);
        canvas.drawText("60", -textPaint.measureText("60") / 2, midRadius + getTextDiffY(textPaint), textPaint);
        canvas.drawText("90", -midRadius - textPaint.measureText("90") / 2, getTextDiffY(textPaint), textPaint);

这里,先确定x的位置,只要将x的坐标减去文本的宽度的一半就OK了;y 的位置稍微复杂点,

Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        return Math.abs(fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent;

确定文本的基线,然后进行绘制就OK了。

4.绘制文本处的短线,

简单粗暴直接代码之

//绘制直线
        canvas.drawLine(0, -innerRaduis, 0, -midRadius + lineLength, linePaint);
        canvas.drawLine(innerRaduis, 0, midRadius - lineLength, 0, linePaint);
        canvas.drawLine(0, innerRaduis, 0, midRadius - lineLength, linePaint);
        canvas.drawLine(-innerRaduis, 0, -midRadius + lineLength, 0, linePaint);
5.绘制内圆圈

就是绘制圆,将画笔设置为只绘制边框即可

linePaint.setStyle(Paint.Style.STROKE);
6.绘制内部的圆球

以最下面最居中的那个球为基点,先设置球的半径,绘制下面一圈的球,根据球的半径算出每个球所占的角度,涉及到三角函数的知识,(后期补上图做说明)。

 float r = dip2px(18);
 float midBallDiffY = innerRaduis - dip2px(2) - r;
 float minInnerRadius = midBallDiffY - r * 2;
 double eachMidArc = Math.toDegrees(Math.asin(r * 1.0d / midBallDiffY)) * 2;
 double eachInnerArc = Math.toDegrees(Math.asin(r * 1.0d / minInnerRadius)) * 2;

先绘制最下面一圈

for (int i = 5; i > 0; i--) {
            Ball ballX = new Ball();
            float x = (float) ((innerRaduis - dip2px(2) - r) * (Math.sin(Math.toRadians(eachMidArc * (5 - i)))));
            float y = (float) ((innerRaduis - dip2px(2) - r) * (Math.cos(Math.toRadians(eachMidArc * (5 - i)))));
            ballX.cx = x;
            ballX.cy = y;
            ballX.num = String.valueOf(random.nextInt(69) + 1);
            ballX.rotateDegress = random.nextInt(90) + 10;
            canvas.drawCircle(ballX.cx, ballX.cy, ballX.radius, dashPaint);
            canvas.rotate(ballX.rotateDegress, x, y);
            canvas.drawText(ballX.num, x - textPaint.measureText(ballX.num) / 2, y + getTextDiffY(textPaint), textPaint);
            canvas.rotate(-ballX.rotateDegress, x, y);
        }

        for (int i = 4; i > 0; i--) {
            Ball ballX = new Ball();
            float x = -(float) ((innerRaduis - dip2px(2) - r) * (Math.sin(Math.toRadians(eachMidArc * (5 - i)))));
            float y = (float) ((innerRaduis - dip2px(2) - r) * (Math.cos(Math.toRadians(eachMidArc * (5 - i)))));
            ballX.cx = x;
            ballX.cy = y;
            ballX.num = String.valueOf(random.nextInt(69) + 1);
            canvas.drawCircle(ballX.cx, ballX.cy, ballX.radius, dashPaint);
            ballX.rotateDegress = random.nextInt(90) + 10;
            canvas.rotate(ballX.rotateDegress, x, y);
            canvas.drawText(ballX.num, x - textPaint.measureText(ballX.num) / 2, y + getTextDiffY(textPaint), textPaint);
            canvas.rotate(-ballX.rotateDegress, x, y);

        }

然后绘制上面的一圈

for (int i = 3; i > 0; i--) {
            Ball ballX = new Ball();
            float x = (float) (minInnerRadius * (Math.sin(Math.PI * (eachInnerArc * (3 - i) / 180.0f))));
            float y = (float) (minInnerRadius * (Math.cos(Math.PI * (eachInnerArc * (3 - i) / 180.0f))));
            ballX.cx = x;
            ballX.cy = y;
            ballX.num = String.valueOf(random.nextInt(69) + 1);
            ballX.rotateDegress = random.nextInt(90) + 10;
            canvas.drawCircle(ballX.cx, ballX.cy, ballX.radius, dashPaint);
            canvas.rotate(ballX.rotateDegress, x, y);
            canvas.drawText(ballX.num, x - textPaint.measureText(ballX.num) / 2, y + getTextDiffY(textPaint), textPaint);
            canvas.rotate(-ballX.rotateDegress, x, y);

        }

        for (int i = 2; i > 0; i--) {
            Ball ballX = new Ball();
            float x = -(float) (minInnerRadius * (Math.sin(Math.PI * (eachInnerArc * (3 - i) / 180.0f))));
            float y = (float) (minInnerRadius * (Math.cos(Math.PI * (eachInnerArc * (3 - i) / 180.0f))));
            ballX.cx = x;
            ballX.cy = y;
            ballX.num = String.valueOf(random.nextInt(69) + 1);
            ballX.rotateDegress = random.nextInt(90) + 10;
            canvas.drawCircle(ballX.cx, ballX.cy, ballX.radius, dashPaint);
            canvas.rotate(ballX.rotateDegress, x, y);
            canvas.drawText(ballX.num, x - textPaint.measureText(ballX.num) / 2, y + getTextDiffY(textPaint), textPaint);
            canvas.rotate(-ballX.rotateDegress, x, y);
        }

看到效果图,每个球的文本都有一个角度的,关键代码将画笔旋转,绘制后再回位。

 canvas.rotate(ballX.rotateDegress, x, y);
 canvas.drawText(ballX.num, x - textPaint.measureText(ballX.num) / 2, y + getTextDiffY(textPaint), textPaint);
 canvas.rotate(-ballX.rotateDegress, x, y);
7.绘制进度条
public class LottoCircleProgressView extends View {
    private int innerRaduis = 200;
    private Paint progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int progress = 0;
    private RectF arcRectf = new RectF();
    private Bitmap iconBitmap;
    private Rect bitmapRect;

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

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

    public LottoCircleProgressView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        progressPaint.setColor(0xFFF7A400);
        progressPaint.setAntiAlias(true);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);
        progressPaint.setStrokeJoin(Paint.Join.ROUND);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeWidth(dip2px(4));

        iconBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_progress_ball);
        bitmapRect = new Rect();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        int size = Math.min(width, height);
        setMeasuredDimension(size, size);

        innerRaduis = size / 2 - dip2px(30);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate(getWidth() / 2, getHeight() / 2);
        arcRectf.set(-innerRaduis, -innerRaduis, innerRaduis, innerRaduis);
        canvas.drawArc(arcRectf, -90, progress, false, progressPaint);

        int left = (int) (innerRaduis * Math.sin(Math.PI * (double) (progress * 1.0f / 180)) - (iconBitmap.getWidth() + 20) / 2);
        int top = (int) (-innerRaduis * Math.cos(Math.PI * (double) (progress * 1.0f / 180)) - (iconBitmap.getHeight() + 20) / 2);
        int right = (int) (innerRaduis * Math.sin(Math.PI * (double) (progress * 1.0f / 180)) + (iconBitmap.getWidth() + 20) / 2);
        int bottom = (int) (-innerRaduis * Math.cos(Math.PI * (double) (progress * 1.0f / 180)) + (iconBitmap.getHeight() + 20) / 2);
        bitmapRect.set(left, top, right, bottom);

        canvas.drawBitmap(iconBitmap, null, bitmapRect, null);
        super.onDraw(canvas);
    }

    public static int dip2px(float dipValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startProgressAnim();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        clearAnimation();
    }

    private void startProgressAnim() {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360);
        valueAnimator.setDuration(60000);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                progress = ((int) animation.getAnimatedValue());
                invalidate();
            }
        });

        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
        valueAnimator.start();
    }


}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值