android自定义View之TypedArray

Container for an array of values that were retrieved with obtainStyledAttributes(AttributeSet, int[], int, int) or obtainAttributes(AttributeSet, int[]). Be sure to call recycle() when done with them. The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes数组

TypedArray是存放使用obtainStyledAttributes(AttributeSet, int[], int, int)或obtainAttributes(AttributeSet, int[]). 方法检验出数组值的容器。必定压在作完以上操做后调用recycle() 。使用obtainStyledAttributes获取attrs里的属性与被检索出的索引结构一致。缓存

API表示:回收TypedArray,以便后面重用 ![输入图片说明this

/**
  * Give back a previously retrieved StyledAttributes, for later re-use.
  */
 public void recycle() {
      synchronized (mResources.mTmpValue) {
          TypedArray cached = mResources.mCachedStyledAttributes;
         if (cached == null || cached.mData.length < mData.length) {
             mXml = null;
             mResources.mCachedStyledAttributes = this;
        }
     }

}

在每次自定义View的时候都会建立TypedArray,对内存消耗有较大的影响。在TypedArray后调用recycle主要是为了缓存。当recycle被调用后,这就说明这个对象从如今能够被重用了。TypedArray 内部持有部分数组,它们缓存在Resources类中的静态字段中,这样就不用每次使用前都须要分配内存。.net

参考连接:http://blog.csdn.net/Monicabg/article/details/45014327code

https://yq.aliyun.com/articles/50702对象

相关文章
相关标签/搜索