自定义View——转盘

public class ZhuanPanView extends View implements View.OnClickListener {

    private String[] contents = new String[]{"A","B","C","D","E","F"};  //文字
    private int[] colors = new int[]{Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"),
            Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"),
            Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C")};  //颜色

    private int mWidth;
    private Paint mPaint;
    private Context mContext;

    private String mStr="Go";

    public ZhuanPanView(Context context,AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mPaint = new Paint();
        setOnClickListener(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.RED); //设置边缘的颜色
        mPaint.setStyle(Paint.Style.STROKE);  //描边 边缘
        mPaint.setStrokeWidth(3);  //设置边缘的宽度
        mPaint.setAntiAlias(true);  //设置边缘锯齿
        canvas.drawCircle(mWidth/2,mWidth/2,mWidth/2,mPaint);  //radius:半径

        //画扇形 RectF
        RectF rectF = new RectF(0, 0, mWidth, mWidth);
        mPaint.setStyle(Paint.Style.FILL);
        for (int i = 0; i < colors.length; i++) {
            mPaint.setColor(colors[i]);
            int startjd = i * 60;
            canvas.drawArc(rectF, startjd, 60, true, mPaint);
        }

        //文字
        mPaint.setColor(Color.BLACK); //设置文字颜色
        mPaint.setTextSize(30);  //设置文字大小
        for (int i=0;i<contents.length;i++){
            int startjd = i * 60;
            Path path = new Path();   //Path 代表路径 想怎么画就怎么画
            path.addArc(rectF,startjd,60);  //
            canvas.drawTextOnPath(contents[i],path,100,100,mPaint);
        }

        //在最中心画一个圆Go
        drawCenterCircle(canvas);
    }

    //在最中心画一个圆  Go
    private void drawCenterCircle(Canvas canvas) {
        mPaint.setColor(Color.GREEN); //画中心圆  颜色
        canvas.drawCircle(mWidth/2,mWidth/2,50,mPaint);
        mPaint.setColor(Color.BLACK); //设置文字颜色
        mPaint.setTextSize(24);
        Rect rect = new Rect();
        mPaint.getTextBounds(mStr,0,mStr.length(),rect);
        int width = rect.width();
        int height = rect.height();
        canvas.drawText(mStr, mWidth / 2 - width / 2, mWidth / 2 + height / 2, 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();
        int jd = random.nextInt(3600);
        RotateAnimation rotateAnimation = new RotateAnimation(0f, jd, mWidth / 2, mWidth / 2);
        rotateAnimation.setDuration(3000);
        rotateAnimation.setFillAfter(true); //停留到最后的状态
        startAnimation(rotateAnimation);
    }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值