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對象

相關文章
相關標籤/搜索