大转盘抽奖 luckywheel

10 篇文章 0 订阅
5 篇文章 0 订阅

参考 luckywheel官网
背景 - blocks
转盘的背景是可以多层级的我这里使用了3层

      blocks: [
        {
          padding: '50px',
          imgs: [
            {
              src: require('/src/assets/images/prize/bg1.png'),
              width: '100%',
              height: '100%',
              top: '4px',
              left: '-13px'
            }
          ]
        },
        {
          padding: '20px',
          imgs: [
            {
              src: require('/src/assets/images/prize/bg2.png'),
              width: '100%',
              rotate: true
            }
          ]
        },
        {
          imgs: [
            {
              src: require('/src/assets/images/prize/zhizhen.png'),
              width: '22px',
              height: '45px',
              top: '-45px'
            }
          ]
        }
      ],
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的大转盘的设计代码示例,主要利用了Android的Canvas绘图功能: ``` public class LuckyWheelView extends View { private Paint mPaint; private RectF mRectF; private String[] mPrizes = {"Prize 1", "Prize 2", "Prize 3", "Prize 4", "Prize 5", "Prize 6"}; private int[] mColors = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW, Color.MAGENTA, Color.CYAN}; private int mWheelSize = 6; // 大转盘的奖品数量 private int mSelectedIndex = -1; // 当前选中的奖品 private float mStartAngle = 0; // 大转盘开始旋转的角度 private float mSweepAngle = 60; // 大转盘每个奖品的角度 private float mRadius; // 大转盘半径 public LuckyWheelView(Context context) { super(context); init(); } public LuckyWheelView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public LuckyWheelView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setStyle(Paint.Style.FILL); mRectF = new RectF(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 计算大转盘半径 mRadius = Math.min(getWidth(), getHeight()) / 2f - 100f; // 绘制大转盘每个奖品的扇形区域 for (int i = 0; i < mWheelSize; i++) { mPaint.setColor(mColors[i % mColors.length]); mRectF.set(getWidth() / 2f - mRadius, getHeight() / 2f - mRadius, getWidth() / 2f + mRadius, getHeight() / 2f + mRadius); canvas.drawArc(mRectF, mStartAngle, mSweepAngle, true, mPaint); mStartAngle += mSweepAngle; } // 绘制大转盘中间的圆形区域 mPaint.setColor(Color.WHITE); canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, mRadius / 3f, mPaint); // 绘制大转盘每个奖品的文字 mPaint.setColor(Color.BLACK); mPaint.setTextSize(50f); mPaint.setTextAlign(Paint.Align.CENTER); mStartAngle = mSweepAngle / 2f; for (int i = 0; i < mWheelSize; i++) { canvas.save(); canvas.rotate(mStartAngle, getWidth() / 2f, getHeight() / 2f); canvas.drawText(mPrizes[i], getWidth() / 2f, getHeight() / 2f - mRadius / 2f, mPaint); canvas.restore(); mStartAngle += mSweepAngle; } // 绘制指针 mPaint.setColor(Color.RED); canvas.drawLine(getWidth() / 2f, getHeight() / 2f, getWidth() / 2f + mRadius, getHeight() / 2f, mPaint); canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, 20f, mPaint); } public void startSpin() { Random random = new Random(); mSelectedIndex = random.nextInt(mWheelSize); float angle = (360f / mWheelSize) * mSelectedIndex + mSweepAngle / 2f; ObjectAnimator animator = ObjectAnimator.ofFloat(this, "rotation", 0f, angle + 720f); animator.setDuration(5000); animator.setInterpolator(new DecelerateInterpolator()); animator.start(); } public int getSelectedIndex() { return mSelectedIndex; } } ``` 在布局文件中,可以使用以下代码将该自定义View添加到界面上: ``` <com.example.luckywheel.LuckyWheelView android:id="@+id/lucky_wheel_view" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 在代码中,可以通过调用`startSpin()`方法来启动大转盘旋转,并通过`getSelectedIndex()`方法获取最后停留的奖品索引。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值