创建文件

使用Studio默认创建自定义视图会自动创建3个文件:
java文件MyView
直接xml文件的myView
属性文件attrs_my_view(属性文件不用全命名是attrs)

分别解释

Java文件

首先显示的是大量的自定义属性

 private String mExampleString; // TODO: use a default from R.string...    private int mExampleColor = Color.RED; // TODO: use a default from R.color...    private float mExampleDimension = 0; // TODO: use a default from R.dimen...    private Drawable mExampleDrawable;    private TextPaint mTextPaint;    private float mTextWidth;    private float mTextHeight;

3个重写的带有不同参数的构造方法

public MyView(Context context) {        super(context);        init(null, 0);    }    public MyView(Context context, AttributeSet attrs) {        super(context, attrs);        init(attrs, 0);    }    public MyView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        init(attrs, defStyle);    }

各种自定义属性的get和set方法

public String getExampleString() {        return mExampleString;    }    /** * Sets the view's example string attribute value. In the example view, this string * is the text to draw. * * @param exampleString The example string attribute value to use. */    public void setExampleString(String exampleString) {        mExampleString = exampleString;        invalidateTextPaintAndMeasurements();    }    /** * Gets the example color attribute value. * * @return The example color attribute value. */    public int getExampleColor() {        return mExampleColor;    }    /** * Sets the view's example color attribute value. In the example view, this color * is the font color. * * @param exampleColor The example color attribute value to use. */    public void setExampleColor(int exampleColor) {        mExampleColor = exampleColor;        invalidateTextPaintAndMeasurements();    }    /** * Gets the example dimension attribute value. * * @return The example dimension attribute value. */    public float getExampleDimension() {        return mExampleDimension;    }    /** * Sets the view's example dimension attribute value. In the example view, this dimension * is the font size. * * @param exampleDimension The example dimension attribute value to use. */    public void setExampleDimension(float exampleDimension) {        mExampleDimension = exampleDimension;        invalidateTextPaintAndMeasurements();    }    /** * Gets the example drawable attribute value. * * @return The example drawable attribute value. */    public Drawable getExampleDrawable() {        return mExampleDrawable;    }    /** * Sets the view's example drawable attribute value. In the example view, this drawable is * drawn above the text. * * @param exampleDrawable The example drawable attribute value to use. */    public void setExampleDrawable(Drawable exampleDrawable) {        mExampleDrawable = exampleDrawable;    }

重写onDraw方法自定义视图

protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        // TODO: consider storing these as member variables to reduce        // allocations per draw cycle.        int paddingLeft = getPaddingLeft();        int paddingTop = getPaddingTop();        int paddingRight = getPaddingRight();        int paddingBottom = getPaddingBottom();        int contentWidth = getWidth() - paddingLeft - paddingRight;        int contentHeight = getHeight() - paddingTop - paddingBottom;        // Draw the text.        canvas.drawText(mExampleString,                paddingLeft + (contentWidth - mTextWidth) / 2,                paddingTop + (contentHeight + mTextHeight) / 2,                mTextPaint);        // Draw the example drawable on top of the text.        if (mExampleDrawable != null) {            mExampleDrawable.setBounds(paddingLeft, paddingTop,                    paddingLeft + contentWidth, paddingTop + contentHeight);            mExampleDrawable.draw(canvas);        }    }

初始化数据的两个方法(写法超赞)

private void init(AttributeSet attrs, int defStyle) {        // Load attributes        final TypedArray a = getContext().obtainStyledAttributes(                attrs, R.styleable.MyView, defStyle, 0);        mExampleString = a.getString(                R.styleable.MyView_exampleString);        mExampleColor = a.getColor(                R.styleable.MyView_exampleColor,                mExampleColor);        // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with        // values that should fall on pixel boundaries.        mExampleDimension = a.getDimension(                R.styleable.MyView_exampleDimension,                mExampleDimension);        if (a.hasValue(R.styleable.MyView_exampleDrawable)) {            mExampleDrawable = a.getDrawable(                    R.styleable.MyView_exampleDrawable);            mExampleDrawable.setCallback(this);        }        a.recycle();        // Set up a default TextPaint object        mTextPaint = new TextPaint();        mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);        mTextPaint.setTextAlign(Paint.Align.LEFT);        // Update TextPaint and text measurements from attributes        invalidateTextPaintAndMeasurements();    }    private void invalidateTextPaintAndMeasurements() {        mTextPaint.setTextSize(mExampleDimension);        mTextPaint.setColor(mExampleColor);        mTextWidth = mTextPaint.measureText(mExampleString);        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();        mTextHeight = fontMetrics.bottom;    }

TypedArray是存储属性的数组,而MyView_exampleDimension这种写法的MyView就是自定义属性的命名name属性

没事巴巴几句:终于能看懂系统自动创建的模板之一了,世界有爱了

更多相关文章

  1. Android(安卓)将自己的应用程序改成系统的应用程序
  2. Android调用WebAPI之传参、文件
  3. 【Android】自定义dialog的布局样式
  4. android项目结构
  5. android flutter 混合开发(配置篇)
  6. Android(安卓)Drawable 系列——ClipDrawable
  7. android tools:
  8. Xamarin开发Android时Visual Studio 2012没有智能提示解决办法
  9. NPM 和webpack 的基础使用

随机推荐

  1. CentOS 7下安装与配置MySQL 5.7
  2. 带你5分钟读懂MySQL字符集设置
  3. MySQL曝中间人攻击Riddle漏洞可致用户名
  4. mysql 数据库取前后几秒 几分钟 几小时
  5. mysql 获取昨天日期、今天日期、明天日期
  6. mysql解压缩方式安装和彻底删除的方法图
  7. 一个小时学会MySQL数据库(张果)
  8. (MariaDB)MySQL数据类型和存储机制全面讲
  9. 十个实用且简单的MySQL函数
  10. Windows下MySQL下载与安装、配置与使用教