自定义控件的抽奖转盘

本文介绍了一种自定义的抽奖转盘视图实现,该视图使用Android的View进行绘制,通过RotateAnimation实现旋转效果,并展示了如何设置旋转角度、速度及随机抽奖结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##MainActivity的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <liqinyang.bwie.com.dametwo3.TurnTableView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

##TurnTableView页面(自定义控件)

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

public class TurnTableView extends View implements View.OnClickListener {
    private final int widthPixels;
    private final int heightPixels;
    private final int centerX;
    private final int centerY;
    private final int[] colors;
    private String[] desc = new String[]{"性感", "丰满", "知性", "聪明", "贤惠", "优秀"};
    private Paint paint;
    private Paint textPaint;
    private int radius;
    private RectF rangeRectF;
    private float startAngle;
    private RotateAnimation rotateAnimation;
    private boolean isRote;
    private int mSpeed;

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

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

    public TurnTableView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        widthPixels = displayMetrics.widthPixels;
        heightPixels = displayMetrics.heightPixels;
        centerX = widthPixels / 2;
        centerY = heightPixels / 2;
        init(context);
        colors = new int[]{Color.BLUE, Color.GRAY, Color.GREEN, Color.CYAN, Color.BLACK, Color.YELLOW};
        //初始化旋转动画
        initAnimation();
        this.setOnClickListener(this);
    }

    private void initAnimation() {
        rotateAnimation = new RotateAnimation(0, 360, centerX, centerY);
        rotateAnimation.setDuration(800);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setRepeatCount(-1);
        rotateAnimation.setInterpolator(new LinearInterpolator());
        rotateAnimation.setRepeatMode(Animation.RESTART);
    }

    private void startAnima() {
        isRote = true;
        startAnimation(rotateAnimation);
    }


    private void stopAnima() {
        isRote = false;
        clearAnimation();
    }

    //给一个随机的抽奖结果
    private void setRoundDom() {

        double random = Math.random();
        RotateAnimation rotateAnimation2 = new RotateAnimation(0, (float) (360 * random),
                centerX, centerY);
        rotateAnimation2.setDuration(100);
        rotateAnimation2.setFillAfter(true);
        startAnimation(rotateAnimation2);
        mSpeed = 0;
    }

    private void init(Context context) {
        paint = new Paint();
        paint.setDither(true);// 防抖动
        paint.setColor(Color.RED);
        paint.setStrokeWidth(20);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(600, 600);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //移动画布的坐标原点
        canvas.translate(centerX, centerY);

        //画6个圆弧
        RectF rectF = new RectF(-300, -300, 300, 300);
        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 rectText = new Rect();
        paint.getTextBounds("start", 0, 5, rectText);
        int width = rectText.width();
        int height = rectText.height();
        canvas.drawText("start", -width / 2, height / 2, paint);

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

    //点击事件
    @Override
    public void onClick(View view) {
        mSpeed = 5;// 每次旋转的角度
        if (isRote) {
            stopAnima();
            setRoundDom();
        } else {

            startAnima();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值