自定义View_1

一、先来看看Android各控件的继承关系(搞清控件怎么来的,我们又怎么样去借鉴模仿重新定义一个view)。

下面是安卓控件的继承关系类图,其中红色为常用控件,Android中所有控件都继承自android.view.View,其中android.view.ViewGroup是View的一个重要子类,绝大部分的布局都继承自ViewGroup。


20140404100148953(1).jpg

虽然看上去错综复杂,理清思路条线还是比较明晰,view相当于java的object,是所有控件的子类,所以你如果想创建一个全新的控件先要继承view,这是第一步。

二、继承了view必然要写构造函数,这是重点
1、先看看view的构造函数,它有四个
View(Context)
View(Context, AttributeSet)
View(Context, AttributeSet, defStyleAttr)
View(Context, AttributeSet, defStyleAttr, defStyleRes)
一般我们定义好了CustomView类,在xml引用时是调用View(Context, AttributeSet),我刚开始的时候看见Attributeset时候也是懵的,这个参数可复杂了,稍后讲;
先继续看看view(context)的源码,如下图:


粘贴图片.png

上面这图确实是实现了构造函数得作用——初始化数据;

接着看View(Context, AttributeSet, defStyleAttr)的源码,如下图:


粘贴图片(2).png

上面这图的构造函数里调用了View(Context, AttributeSet, defStyleAttr)这个构造函数;

粘贴图片(3).png

上面这图的构造函数里调用了View(Context, AttributeSet, defStyleAttr, defStyleRes)构造函数;

粘贴图片(1).png

上面这图的构造函数里调用了View(context)构造函数,这样便于不重复写代码(数据初始化的代码);
我们大概清楚了自定义view的构造方法,现在具体说说构造函数里的参数:
context这个自然不用多说,Android最常用的,上下文本;
AttributeSet,这个参数是关键,有关于你自定义view的新属性值在你在xml引用的时候都是通过AttributeSet获取的,从源码里我们看到 final TypedArray a = context.obtainStyledAttributes( attrs, com.android.internal.R.styleable.View, defStyleAttr, defStyleRes);这段源码,TypedArray 这个又是什么呢?

Google 开发者平台是这么解释这个类的:


20160326141058890.png

大体意思是:TypedArray 是一个数组容器,在这个容器中装由 obtainStyledAttributes(AttributeSet, int[], int, int) 或者 obtainAttributes(AttributeSet, int[]) 函数获取到的属性值。用完之后记得调用 recycle() 函数回收资源。索引值用来获取 Attributes 对应的属性值(这个 Attributes 将会被传入 obtainStyledAttributes() 函数)。
具体我们来实际操作下 ,我们先自定义一个view,先不看自定义view的功能,自定义view的类如下如:

  public class VideoLoadingView extends View { 
  public VideoLoadingView(Context context) {
      super(context);
      init(context);
  }
  
public VideoLoadingView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public VideoLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray attributes = context.getTheme()
            .obtainStyledAttributes(attrs, R.styleable.VideoLoadingView, defStyleAttr, 0);
    initAttrs(attributes);
    init(context);
}

private void initAttrs(TypedArray attributes) {
    try {
        mArcColor = attributes.getColor(R.styleable.VideoLoadingView_ArcColor, Color.GREEN);
        mTriangleColor = attributes.getColor(R.styleable.VideoLoadingView_TriangleColor, Color.GREEN);
    } finally {
        attributes.recycle();
    }
}

private void init(Context context) {
    mArcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mArcPaint.setColor(mArcColor);
    mArcPaint.setStyle(Paint.Style.STROKE);

    mTrianglePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mTrianglePaint.setColor(mTriangleColor);
    mTrianglePaint.setStrokeWidth(2);
    mTrianglePaint.setStyle(Paint.Style.FILL);         
      } 
 }

1.在资源文件 values 下创建文件 attrs.xml,如下:

粘贴图片(4).png

2.在xml里引用这个自定义的view

?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   tools:context="com.goldmantis.wb.viewdemo.MainActivity">

<com.goldmantis.wb.viewdemo.VideoLoadingView
    android:id="@+id/videoLoadingView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/stop_btn"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="90dp"
    app:ArcColor="@color/colorPrimaryDark"
    app:TriangleColor="@color/colorPrimary" />
  </RelativeLayout>

上面的 app:ArcColor="@color/colorPrimaryDark"
app:TriangleColor="@color/colorPrimary"
这两个属性值就是通过下面的代码获取的


粘贴图片(5).png

是不是对于自定义的新属性取值大概有了个明白,其实对于这个自定义新属性的取值还有好多要讲的,继续深入TypedArray ,获取 TypedArray 对象 的函数一共四个:

1.public TypedArray obtainStyledAttributes (int[] attrs);

2.public TypedArray obtainStyledAttributes (int resid, int[] attrs);

3.public TypedArray obtainAttributes (AttributeSet set, int[] attrs);

4.public TypedArray obtainStyledAttributes (AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)。

讲解之前,需要说明一点:函数 1、2、4 都是 Resources.Theme 的函数,而 3 是 Resources 的函数。
obtainStyledAttributes (int[] attrs)
Google Developer 是这么解释这个函数的:

20160327145022999.png

上面主要信息Return a TypedArray holding the values defined by Theme which are listed in attrs
它的大意是:返回一个与 attrs 中列举出的属性相关的数组,数组里面的值由 Theme 指定。从上面的概述中,我们可以知道:这个 Theme 就是关键。找各种资料发现attrs 对应的属性值必须定义在 Application 中 Android:theme 对应的 style 下,换句话说在定义应用主题时我们要在对应主题下设置attrs属性,
我们在为 Application 设置主题的同时需要在对应的主题下为 attrs 设置相关的属性,理论与实践相结合,人这个动物思维+视觉 才会把抽象的事物具体展现在大脑中,印象更深刻,理解更透彻,

粘贴图片(7).png
粘贴图片(8).png

上面style中是不是包含我们上面自定义view的属性app:ArcColor="@color/colorPrimaryDark"
app:TriangleColor="@color/colorPrimary" 然后我们自定义view的构造函数里也要改一改,改成下图:

粘贴图片(9).png

上面这函数不管你在布局xml里面引用的app:ArcColor="@color/xxx" app:TriangleColor="@color/xxx"属性采用什么颜色值,最终显示的是style里设置的属性值的颜色。

obtainStyledAttributes (int resid, int[] attrs)
Google Developer 是这么解释这个函数的:

20160327160456295.png

上面主要信息Return a TypedArray holding the values defined by the style resource resid which are listed in attrs
意思跟上面的一个参数差不多,只不过区别在于可以不用在android:them指定的style里加自定义属性,在另外的style里加自定义属性。

粘贴图片(10).png

粘贴图片(11).png

作用效果跟一个参数的作用差不多,不累赘。

obtainAttributes (AttributeSet set, int[] attrs)
Google Developer 是这么解释这个函数的:

20160327163828401.png

上面主要信息Retrieve a set of basic attribute values from an AttributeSet, not performing styling of them using a theme and/or style resources.这句话的大意是:从 AttributeSet 中获取 attrs 对应的属性值,不为这些属性值设置样式。
同样构造函数要改下,style里面跟没有自定义view一样,在布局文件里引用自定义view,构造函数修改如下:

粘贴图片(12).png

这样的自定义view展示出来的效果根据你在布局xml里定义新属性值而定的,但是这样会有一个问题,那就是你引用自定义view的布局xml里必须把你新定义属性全部写在xml文件中,不然就会有报错出现,具体表现在如下图代码,他没有相应的属性值可取,自然而然会报错。


粘贴图片(13).png

obtainStyledAttributes (AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)
Google Developer 是这么解释这个函数的:

20160327170932196.png

上面的 Google 开发者文档的大意是:

返回一个与 attrs 属性相对应的数组。另外,如果在 AttributeSet 中为 attrs 指定了样式属性,那么这个样式属    性就会应用在这些属性上。

attribute 最终由下面四个因素决定:

在 AttributeSet 中定义的属性(Any attribute values in the given AttributeSet);

AttributeSet 指定的样式资源文件(The style resource specified in the AttributeSet (named “style”));

由 defStyleAttr 和 defStyleRes 指定的样式资源文件(The default style specified by defStyleAttr and defStyleRes);

主题中的默认值(The base values in this theme)。

上面四种元素的优先级是从上到下排序的,也就是说:如果在 AttributeSet 中定义了某个属性的值,那么无论后面的样式属性如何定义,它的值都不会改变。

接下来我们分别解释下,函数中各参数的含义:

AttributeSet set :XML 中定义的属性值,可能为 null;

int[] attrs :目标属性值;

int defStyleAttr :在当前主题中有一个引用指向样式文件,这个样式文件将 TypedArray 设置默认值。如果此参数为 0 则表示不进行默认值设置。

int defStyleRes :默认的样式资源文件,只有当 defStyleAttr 为 0 或者无法在对应的主题下找到资源文件时才起作用。如果此参数为 0 则表示不进行默认设置。
粘贴图片(14).png
粘贴图片(17).png
粘贴图片(18).png

对于第三个参数要重点讲下:
defStyleAttr,这个参数表示的是一个<style>中某个属性的ID,当Android在AttributeSet和style属性所定义的style资源中都没有找到XML属性值时,就会尝试查找当前theme(theme其实就是一个<style>资源)中属性为defStyleAttr的值,如果其值是一个style资源,那么Android就会去该资源中再去查找XML属性值。在AppTheme中,我们设置了viewStyle这个属性的值<item name="viewStyle">@style/AppTheme2</item>
viewStyle这个属性是在values/attrs.xml中定义了<attr name="customstyle" format="reference"/>
viewStyle被定义为一个reference格式,即其值指向一个资源类型,我们在AppTheme中将其赋值为@style/AppTheme2,即在AppTheme中,viewStyle的就是AppTheme2,其指向了一个style资源。
如上图所示运行后显示的结果是根据布局xml文件里设置的新属性值app:ArcColor="@color/colorPrimaryDark"
app:TriangleColor="@color/colorPrimary"来显示。
但如果TypedArray typedArray = theme.obtainStyledAttributes(null, R.styleable.MyView, R.attr.viewStyle, R.style.AppTheme1);则显示的结果会根据R.attr.viewStyle里的属性值展示。
但如果TypedArray typedArray = theme.obtainStyledAttributes(null, R.styleable.MyView, 0, R.style.AppTheme1);则显示的结果会根据R.style.AppTheme1里的属性值展示。

有没有发现它们的优先级是从高到底依次排列的:
AttributeSet > defStyleAttr > defStyleRes
它的调用逻辑是这样的:

20160327225112499.png

这篇的字数有点多,有点难驾驭,大家看的也比较头疼,先到这里,有空再继续。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,911评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,014评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,129评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,283评论 1 264
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,159评论 4 357
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,161评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,565评论 3 382
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,251评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,531评论 1 292
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,619评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,383评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,255评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,624评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,916评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,199评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,553评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,756评论 2 335

推荐阅读更多精彩内容