自定义控件View

先在布局中写

<com.luyao.dell.yuan.YurnTableView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

然后在定义一个class
不用再MainActivity里写东西

public class YurnTableView extends View implements View.OnClickListener {

    private Paint paint;
    private int widthPixels;
    private int heightPixels;
    private int centerX;
    private int centerY;
    private int[] colors;
    private String[] desc = new String[]{"女生", "性感", "知性", "聪明", "贤惠", "真好"};
    private RotateAnimation rotateAnimation;
    private boolean isRote;

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

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

    public YurnTableView(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.GRAY, 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 init(Context context) {
        paint = new Paint();
        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 rect = new Rect();
        paint.getTextBounds("start", 0, 5, rect);
        int width = rect.width();
        int height = rect.height();
        canvas.drawText("start", -width / 2, height / 2, paint);

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

    //点击事件
    @Override
    public void onClick(View view) {
        if (isRote) {
            stopAnima();
            setRoundDom();
        } else {
            startAnima();
        }
    }

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

    private void stopAnima() {
        isRote = false;
        clearAnimation();
    }
    //给一个随机的抽奖结果
    private void setRoundDom() {
        double random = Math.random();
        RotateAnimation rotateAnimation = new RotateAnimation(0, (float) (360 * random), centerX, centerY);
        rotateAnimation.setDuration(100);
        rotateAnimation.setFillAfter(true);
        startAnimation(rotateAnimation);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43413728/article/details/83684009