Gallery被標(biāo)記為過時
一切的起源源于Gallery被標(biāo)記為過時,所以才有本文的出現(xiàn)。
過時的原因,可以參考下面鏈接屈暗。
I suspect that Gallery was deprecated because it did not properly use convertView with its adapter. Which meant that it had to create a new view for every item which was a drain on performance.
Another option you have is to use the 3rd party created EcoGallery which Joseph Earl created to overcome the issue, this version does recycle its views properly.
關(guān)于RecycleBin
在分析Gallery控件時,需要先了解RecycleBin脂男。RecycleBin是一個容器類养叛,用來管理可回收的View。
不像Adapter/ArrayList等宰翅,RecycleBin有很多同名類弃甥,它作為內(nèi)部類,存在于多個不同的類里面汁讼,有著不同的實(shí)現(xiàn)方式淆攻。(目前只發(fā)現(xiàn)下面兩個類里面有RecycleBin內(nèi)部類)
比如阔墩,AbsSpinner里面的RecycleBin:
class RecycleBin {
private final SparseArray<View> mScrapHeap = new SparseArray<View>();
public void put(int position, View v) {
mScrapHeap.put(position, v);
}
View get(int position) {
// System.out.print("Looking for " + position);
View result = mScrapHeap.get(position);
if (result != null) {
// System.out.println(" HIT");
mScrapHeap.delete(position);
} else {
// System.out.println(" MISS");
}
return result;
}
void clear() {
final SparseArray<View> scrapHeap = mScrapHeap;
final int count = scrapHeap.size();
for (int i = 0; i < count; i++) {
final View view = scrapHeap.valueAt(i);
if (view != null) {
removeDetachedView(view, true);
}
}
scrapHeap.clear();
}
}
比如,AbsListView里面的RecycleBin:
class RecycleBin {
private RecyclerListener mRecyclerListener;
/**
* The position of the first view stored in mActiveViews.
*/
private int mFirstActivePosition;
/**
* Views that were on screen at the start of layout. This array is populated at the start of
* layout, and at the end of layout all view in mActiveViews are moved to mScrapViews.
* Views in mActiveViews represent a contiguous range of Views, with position of the first
* view store in mFirstActivePosition.
*/
private View[] mActiveViews = new View[0];
/**
* Unsorted views that can be used by the adapter as a convert view.
*/
private ArrayList<View>[] mScrapViews;
private int mViewTypeCount;
private ArrayList<View> mCurrentScrap;
public void setViewTypeCount(int viewTypeCount) {
if (viewTypeCount < 1) {
throw new IllegalArgumentException("Can't have a viewTypeCount < 1");
}
//noinspection unchecked
ArrayList<View>[] scrapViews = new ArrayList[viewTypeCount];
for (int i = 0; i < viewTypeCount; i++) {
scrapViews[i] = new ArrayList<View>();
}
mViewTypeCount = viewTypeCount;
mCurrentScrap = scrapViews[0];
mScrapViews = scrapViews;
}
public void markChildrenDirty() {
if (mViewTypeCount == 1) {
final ArrayList<View> scrap = mCurrentScrap;
final int scrapCount = scrap.size();
for (int i = 0; i < scrapCount; i++) {
scrap.get(i).forceLayout();
}
} else {
final int typeCount = mViewTypeCount;
for (int i = 0; i < typeCount; i++) {
final ArrayList<View> scrap = mScrapViews[i];
final int scrapCount = scrap.size();
for (int j = 0; j < scrapCount; j++) {
scrap.get(j).forceLayout();
}
}
}
}
public boolean shouldRecycleViewType(int viewType) {
return viewType >= 0;
}
// 代碼太多瓶珊,省略
}
RecycleBin主要用于列表視圖啸箫,比如GridView/ListView等,這些列表視圖通常需要維護(hù)一個和可見窗口同樣大小的View列表艰毒,當(dāng)列表滑動時,不是不斷創(chuàng)建新的View搜囱,而是重用滑出顯示區(qū)域的那些Views丑瞧。RecycleBin容器相當(dāng)于回收區(qū),用于管理滑出這個顯示區(qū)域的View蜀肘,表示可以重用的Views列表绊汹。(從命名字面意思也可以想到)
不同的列表控件,有不同的回收機(jī)制扮宠,所以RecycleBin的實(shí)現(xiàn)也不同西乖。反過來,如果使用了不當(dāng)?shù)腞ecycleBin坛增,會導(dǎo)致列表控件不能真正的利用到回收區(qū)的View获雕,或者說,不當(dāng)?shù)腞ecycleBin導(dǎo)致列表控件無法從回收區(qū)“命中”重用的View收捣,導(dǎo)致每次都創(chuàng)建新的View届案。(從這個角度看,RecycleBin更像是一個Cache)
所以罢艾,RecycleBin扮演著一個回收區(qū)和緩存的角色楣颠。
Gallery控件的缺陷
Gallery控件就是這么回事,RecycleBin回收區(qū)設(shè)置不合理咐蚯,導(dǎo)致View的利用率很低童漩。從代碼看,回收區(qū)以position為key春锋,所以只有往回滑動才能命中矫膨,往前滑動position是遞增的,每次都需要創(chuàng)建新的View期奔。
實(shí)際上豆拨,經(jīng)過測試,Gallery控件是每次都創(chuàng)建新的View能庆,所以連往回滑動都沒有“命中”施禾。通過代碼查看,除了最致命的選擇position作為回收區(qū)key外搁胆,在每次layout都對回收區(qū)進(jìn)行了clear弥搞,是導(dǎo)致連往回滾動都無法命中的原因邮绿。反正,這應(yīng)該是Gallery控件的一個Bug攀例,重用View的機(jī)制沒有生效船逮。(測試Demo見附錄)
Gallery控件另一個Bug是,層疊效果粤铭。
Gallery控件通過重寫ViewGroup的getChildDrawingOrder
來使得繪制從兩邊到中間挖胃,從而使列表項(xiàng)有層疊的效果。然而梆惯,重寫的這個方法并不正確酱鸭。重疊效果并不是越靠近中心的層級越高。如果你需要越靠近中心的層級越高垛吗,越靠近邊界的層級越低凹髓,那么你需要重寫這個方法。
@Override
protected int getChildDrawingOrder(int childCount, int i) {
int selectedIndex = mSelectedPosition - mFirstPosition;
// Just to be safe
if (selectedIndex < 0) return i;
if (i == childCount - 1) {
// Draw the selected child last
return selectedIndex;
} else if (i >= selectedIndex) {
// Move the children after the selected child earlier one
return i + 1;
} else {
// Keep the children before the selected child the same
return i;
}
}
假設(shè)3是選中的怯屉,childCount是5蔚舀,那么上面這個方法的繪制順序是:1 2 4 5 3
而通常我們需要的是下面的繪制順序:1 2 5 4 3
@Override
protected int getChildDrawingOrder(int childCount, int i) {
int selectedIndex = mSelectedPosition - mFirstPosition;
if (i < selectedIndex) {
return i;
} else if (i >= selectedIndex) {
return childCount - 1 - i + selectedIndex;
} else {
return i;
}
}
EcoGallery替代Gallery
Gallery已經(jīng)被標(biāo)記為過時,至于過時的原因锨络,據(jù)說就是上面的View沒有重用導(dǎo)致效率低下赌躺。雖然Gallery控件有效率問題,但它的展示效果還是比較獨(dú)特的:
- 選中居中效果
- 觸摸滑動切換效果
- 多頁面Flip的效果(ViewPager只有3頁可見)
- 頁面層疊效果(間距與層級的控制)
想快速完成上述效果羡儿,使用Gallery還是比較合適的寿谴,相比官方建議的HorizontalScrollView and ViewPager,還需要進(jìn)行一定量的修改失受。
EcoGallery的誕生就是因?yàn)镚allery被過時讶泰,但仍舊需要一個替代Gallery的控件。
EcoGallery并不神秘拂到,它其實(shí)是Gallery源碼的一個拷貝痪署。作者將Gallery源碼從Android原生Widget中抽離出來,并對上述View重用的缺陷進(jìn)行了改進(jìn)兄旬,實(shí)現(xiàn)了另一個RecycleBin狼犯。關(guān)于如何抽離Android原生控件,可以參考附錄领铐。
class RecycleBin {
private SparseArray<View> mScrapHeap = new SparseArray<View>();
public void put(int position, View v) {
mScrapHeap.put(position, v);
}
public void add(int position, View v) {
mScrapHeap.put(mScrapHeap.size(), v);
}
public View get() {
if (mScrapHeap.size() < 1) return null;
View result = mScrapHeap.valueAt(0);
int key = mScrapHeap.keyAt(0);
if (result != null) {
mScrapHeap.delete(key);
}
return result;
}
// 剩下的是沒用的
}
這個RecycleBin回收區(qū)是不管position悯森,只要回收區(qū)有View,就拿出來重用绪撵∑耙觯可以認(rèn)為,這里僅僅將RecycleBin當(dāng)作是回收區(qū)音诈,而沒有作為緩存存在(緩存更接近于拿出來直接用幻碱,回收區(qū)重用會進(jìn)行一次初始化)绎狭。
Gallery另一個層疊的問題,EcoGallery也存在褥傍。
FancyCoverFlow酷炫效果
如果想要下面3D Gallery的效果儡嘶,那么就需要這個控件。FancyCoverFlow繼承于Gallery恍风,而Gallery已經(jīng)過時蹦狂,所以可以修改使其繼承EcoGallery。
附錄:
分析Demo https://github.com/jokinkuang/GalleryRecycle.git
EcoGallery https://github.com/falnatsheh/EcoGallery
FancyCoverFlow https://github.com/davidschreiber/FancyCoverFlow
關(guān)于抽離Android原生控件朋贬,搜索抽離Android原生控件的方法凯楔。
關(guān)于更深入的理解分析,搜索CGallery控件兄世。