Android 自定义View相关API和方法(未完)

Canvas类相关API

1、绘制圆 drawCircle
  //x,y为圆心坐标,radius为半径
  void drawCircle (float cx, float cy, float radius, Paint) 
2、绘制矩形 RectF,Rect
  //RectF(float),Rect (int)
  void drawRect(RectF rect, Paint paint)
  void drawRect(Rect  rect, Paint paint)
  void drawRect(float left, float top, float right, float bottom, Paint paint)
3、绘制圆角矩形 drawRoundRect
   //rx,ry  X和Y轴上各自的半径,跟矩形的圆角角度有直接关系
  void drawRoundRect(RectF rect, float rx, float ry, Paint paint)
  void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint)
4、绘制扇形 drawArc
  //startAngle开始角度,sweepAngle所需扫过的角度大小,useCenter是否使用中心点
  void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)   
  void drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
5、填充画布颜色 drawColor
  void drawColor(int color)
6、绘制直线 drawLine,drawLines
   //startX,startY开始位置坐标,stopX,stopY结束点坐标
  void drawLine(float startX, float startY, float stopX, float stopY, @NonNull Paint paint)
  //可以绘制多条直线,pts(四个一组)多组坐标点如{50,100,200,100,......}
  void drawLines(float[] pts, [Paint])
   //offset跳过前面的几个数据,count你要取出多少数据绘制
   void drawLines(float[] pts, int offset, int count,Paint paint) 
   例如:
  //跳过前四个数据(50, 50, 400, 50),取出12个绘制(400, 50, 400, 600,400, 600, 50, 600,60, 600, 50, 50)
   canvas.drawLines(new float[]{50, 50, 400, 50,400, 50, 400, 600,400, 600, 50, 600,60, 600, 50, 50}, 4, 12, mPaint);
7、绘制椭圆 drawOval
   //oval根据矩形大小来绘制
   void drawOval(RectF oval, Paint paint)
   void drawOval(float left, float top, float right, float bottom, Paint paint)
8、绘制文本 drawText
    void drawText (String text ,float x,float y,Paint paint)
    //start 开始截取字符的索引号,end 结束截取字符的索引号
    void drawText (String text,int start, int end, float x, float y, Paint paint)
    void drawText(CharSequence text, int start, int end, float x, float y, Paint paint)
    //text 字节数组,index 表示第一个要绘制的文字索引,count 需要绘制的文字个数
    void drawText(char[] text, int index, int count, float x, float y, Paint paint)
     /**
      *一般情况用不上
      * @param text   要绘制的文字
      * @param start 从那个字开始绘制 
      * @param end  绘制到哪个字结束
      * @param x 文字左边的坐标
      * @param y 文字的基线坐标
      * @param contextStart 上下文的起始位置。contextStart 需要小于等于 start
      * @param contextEnd 上下文的结束位置。contextEnd 需要大于等于 end 
      * @param isRtl 是否是 RTL(Right-To-Left,从右向左)
      */
    void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd, void float x, float y, boolean isRtl, Paint paint)

onMeasure的使用

    /**
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
       @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     }
      int widthMode = MeasureSpec.getMode(widthMeasureSpec);   //获取宽的模式
      int heightMode = MeasureSpec.getMode(heightMeasureSpec); //获取高的模式
      Mode模式{
      1.   MeasureSpec.UNSPECIFIED 没有指定大小
      2.   MeasureSpec.EXACTLY   指 match_parent或者具体指定的大小
      3.   MeasureSpec.AT_MOST   指 wrap-content 
      } 
      int widthSize = MeasureSpec.getSize(widthMeasureSpec);   //获取宽的尺寸
      int heightSize = MeasureSpec.getSize(heightMeasureSpec); //获取高的尺寸
      onMeasure方法最后需要调用setMeasuredDimension方法来保存测量的宽高值
     
        // 测量某一个child的宽高(ViewGroup的方法)
        protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
        final LayoutParams lp = child.getLayoutParams();
        //获取子控件的宽高约束规则
        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,mPaddingLeft + mPaddingRight, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,mPaddingTop + mPaddingBottom, lp.height);
        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }
       //遍历ViewGroup中所有的子控件,调用measuireChild测量宽高(ViewGroup的方法)
        protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
        final int size = mChildrenCount;
        final View[] children = mChildren;
        for (int i = 0; i < size; ++i) {
            //遍历获取某一子View
            final View child = children[i];
            if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
                 //测量某一个子控件宽高
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
                }
            }
        }
      //测量某一个child的宽高,考虑margin值(ViewGroup的方法)
      protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,int parentHeightMeasureSpec, int heightUsed) {
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
         //获取子控件的宽高约束规则
        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpecmPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin+ widthUsed, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin+ heightUsed, lp.height);
         //测量子控件
        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }
    原文作者:崔鹏宇
    原文地址: https://www.jianshu.com/p/159ec6c29311
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞