幸运转盘

public class LuckView extends View implements View.OnClickListener {
private Paint mPaint;
private String[] contents = new String[]{“A”,“B”,“C”,“D”,“E”,“F”,“G”,“H”};
//颜色时int类型的
private int[] colors = new int[]{Color.parseColor("#8EE5EE"),Color.parseColor("#FFD700"),Color.parseColor("#8EE5EE"),Color.parseColor("#FFD700"), Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"), Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C")};
private int mWidth;
private String mStr = “start”;

public LuckView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mPaint = new Paint();
    setOnClickListener(this);//点击事件
}

//画图
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //1.用RectF画扇形
    RectF rectF = new RectF(0,0,mWidth,mWidth);
    //设置样式--填充
    mPaint.setStyle(Paint.Style.FILL);
    //for循环,将颜色填充
    for (int i = 0; i <colors.length ; i++) {
        //数据
        mPaint.setColor(colors[i]);
        //起始角度的计算
        int starts = i * 45;
        //圆弧
        canvas.drawArc(rectF,starts,45,true,mPaint);
    }

    //2.扇形中的字体
    //设置字体的颜色、大小
    mPaint.setTextSize(25);
    mPaint.setColor(Color.BLACK);
    //for循环,将字体填充
    for (int i = 0; i <contents.length ; i++) {
        int starts = i * 45;
        Path path = new Path();
        path.addArc(rectF,starts,45);
        canvas.drawTextOnPath(contents[i],path,60,60,mPaint);
    }

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(500,500);
    mWidth = getMeasuredWidth();//得到测量后的宽度
}

@Override
public void onClick(View v) {
    Random random = new Random();
    RotateAnimation animation = new RotateAnimation(0f,random.nextInt(3600),mWidth/2,mWidth/2);
    animation.setDuration(3000);
    animation.setFillAfter(true);
    startAnimation(animation);
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值