美文网首页
自定义View的一些积累

自定义View的一些积累

作者: 小菜鸟学Android | 来源:发表于2019-10-11 23:36 被阅读0次
  • 测量方法 onMeasure()
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 

返回的参数是32位,高两位是测量模式。后30位是尺寸

private static final int MODE_SHIFT = 30;
/**
* 这个模式,我也不知道他干嘛的,只要在xml里设置了,高两位都不会是00。至于没有设置的情况。。。。这个还真的不知道哦
* Measure specification mode: The parent has not imposed any constraint
* on the child. It can be whatever size it wants.
*/
public static final int UNSPECIFIED = 0 << MODE_SHIFT;

/**
 * 精准模式,如果当match_parent和一个固定的尺寸时,返回。
 * 这个模式下。子控件的大小是充满父布局,或者在自己想要的尺 * 寸下。子控件的大小收到父控件的影响
 * Measure specification mode: The parent has determined an exact size
 * for the child. The child is going to be given those bounds regardless
 * of how big it wants to be.
 */
public static final int EXACTLY     = 1 << MODE_SHIFT;

/**
 *如果是wrap_content
 *这个模式下,控件的大小是尽可能的去得到自己的大小。根据控件内容自填充。
 * Measure specification mode: The child can be as large as it wants up
 * to the specified size.
 */
public static final int AT_MOST     = 2 << MODE_SHIFT;

这个方法是用来测量控件尺寸的。

 // 返回的mode是32位int。后30位是0。
 int mode = MeasureSpec.getMode(measureSpec);
 int size = MeasureSpec.getSize(measureSpec);
 
 获取到测量模式和尺寸, 
        
  • 绘制View onDraw()
protected void onDraw(Canvas canvas)

通过这个方法来绘制view,通过画布Canvas和画笔Paint共同绘制。

一些Paint的设置

Paint

  • setAnitAlias(boolean aa)

去画笔锯齿

  • setStyle(Style style)

画笔的样式

public enum Style {
        /**
         * 将充满
         * Geometry and text drawn with this style will be filled, ignoring all
         * stroke-related settings in the paint.
         */
        FILL            (0),
        /** 绘制空心的状态
         * Geometry and text drawn with this style will be stroked, respecting
         * the stroke-related fields on the paint.
         */
        STROKE          (1),
        /**
         * Geometry and text drawn with this style will be both filled and
         * stroked at the same time, respecting the stroke-related fields on
         * the paint. This mode can give unexpected results if the geometry
         * is oriented counter-clockwise. This restriction does not apply to
         * either FILL or STROKE.
         */
        FILL_AND_STROKE (2);

        Style(int nativeInt) {
            this.nativeInt = nativeInt;
        }
        final int nativeInt;
    }
  • setDither(boolean dither)

    图片设置抖动处理

  • setColor(int color);

    设置画笔的颜色

  • setStrokeWidth(float width)

    设置线条的宽度

mPaint = new Paint();
        // 设置颜色
        mPaint.setColor(Color.BLUE);
        // 设置填充状态
        mPaint.setStyle(Paint.Style.STROKE);
        // 设置线条宽度
        mPaint.setStrokeWidth(3);
        // 去锯齿
        mPaint.setAntiAlias(true);
微信截图_20191011233116.png

相关文章

  • 自定义View的一些积累

    测量方法 onMeasure() 这个方法是用来测量控件尺寸的。 绘制View onDraw() 一些Paint的...

  • Android自定义View之仪表盘

    Android自定义View之仪表盘 欢迎访问我的博客 又是新系列(坑) 感觉都很零碎,能积累一些是一些了 背景 ...

  • 自定义View简介 - onMeasure,onDraw,自定义

    1.自定义View简介 自定义View可以认为是继承自View或者ViewGroup。经常是处理一些系统没有的效果...

  • Android View(转)

    自定义View的原理自定义View基础 - 最易懂的自定义View原理系列自定义View Measure过程 - ...

  • 自定义View5---完整的自定义View

    移步自定义View系列 1.自定义view的分类自定义单一view(不含子view)继承view继承特定view如...

  • Android自定义View--ClockView

    Android自定义View--ClockView 前一篇博客中,简要介绍了关于自定义View的流程,以及一些重要...

  • 自定义View系列

    自定义View1---知识储备自定义View2---View Measure过程自定义View3---View L...

  • Android 自定义View(一)

    Android 自定义View 自定义View主要用于:需要一些不规则的图形、事件冲突、特定的视图群组、扩展某些组...

  • 自定义view

    Android自定义View 为什么要自定义View自定义View的基本方法 自定义View的最基本的三个方法分别...

  • 自定义View

    自定义View系列文章 自定义View view向上滚动

网友评论

      本文标题:自定义View的一些积累

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