美文网首页
自定义控件02

自定义控件02

作者: EvanPoison | 来源:发表于2017-06-02 14:17 被阅读5次
    1. rect和rectF
      构造方法传入左顶点和右下角的坐标值
      先来看一段代码:
        @Override
        protected void onDraw(Canvas canvas) {
    
            RectF rect = new RectF(10, 10, 100, 100);
            Rect rect2 = new Rect(10, 200, 100, 300);
            canvas.drawRect(rect, mPaint);
            canvas.drawRect(rect2, mPaint);
        }
    

    效果是这样的,两者看起来貌似没有什么区别

    image.png

    再看一段代码:

     @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            /**
             * 这是一个居中的圆
             */
            float x = (getWidth() - getHeight() / 2) / 2;
            float y = getHeight() / 4;
    
            mOval = new RectF( x, y,
                    getWidth() - x, getHeight() - y);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawRect(mOval,mPaint);
           canvas.drawArc(mOval,-90,90,true,mPaint2);
    
        }
    

    当我们设置为true时,就经过圆心
    canvas.drawArc(mOval,-90,90,true,mPaint2);
    画出来的效果:

    image.png

    当我们设置为false时,不经过圆心
    canvas.drawArc(mOval,-90,90,false,mPaint2);

    image.png

    相关文章

      网友评论

          本文标题:自定义控件02

          本文链接:https://www.haomeiwen.com/subject/qhkufxtx.html