俄罗斯转盘简易实现

View

private Paint mFanPaint;
    private Paint mTextPaint;
    private List<Fan> list = new ArrayList<>();
    private boolean flag = false;
    private RotateAnimation animation;
    private void init() {
        mFanPaint = new Paint();
        mTextPaint = new Paint();
        mFanPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setTextSize(20);
        animation = new RotateAnimation(0,360,200,200);
        animation.setDuration(1000);
        animation.setRepeatCount(-1);
        animation.setInterpolator(new LinearInterpolator());
        setOnClickListener(this);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        RectF rectF = new RectF();
        rectF.right = 400;
        rectF.bottom = 400;
        rectF.top = 0;
        rectF.left = 0;
        for (int i = 0; i < list.size(); i++) {
            Fan fan = list.get(i);
            mFanPaint.setColor(fan.getColor());
            canvas.drawArc(rectF,fan.getStartAngle(),360/list.size(),true,mFanPaint);
            Path path = new Path();
            path.addArc(rectF,fan.getStartAngle(),360/list.size());
            mTextPaint.setColor(Color.BLACK);
            canvas.drawTextOnPath(fan.getName(),path,100,30,mTextPaint);
        }
        mFanPaint.setColor(Color.BLACK);
        canvas.drawCircle(200,200,50,mFanPaint);
        mTextPaint.setColor(Color.WHITE);
        canvas.drawText("start",200-mTextPaint.measureText("start")/2,200+mTextPaint.measureText("start")/2,mTextPaint);
    }
    public void addFan(Fan fan) {
        list.add(fan);
        invalidate();
    }
    @Override
    public void onClick(View v) {
        if(flag){
            clearAnimation();
            flag = false;
        }else {
            startAnimation(animation);
            flag = true;
        }
    }

Activity

  MyCircle myfan = findViewById(R.id.my_fan);
        int[] colorArray = new int[]{Color.BLUE,Color.CYAN,Color.DKGRAY,Color.GRAY,Color.GREEN,Color.LTGRAY};
        for (int  i =0;i<6;i++){
            Fan fan = new Fan();
            fan.setColor(colorArray[i]);
            fan.setName("扇形"+i);
            fan.setStartAngle(i*60);
            myfan.addFan(fan);
        }

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

colors.xml

    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

猜你喜欢

转载自blog.csdn.net/LG_lxb/article/details/84726986
今日推荐