Android Gallery EcoGallery 與 FancyCoverFlow 的詳細(xì)分析
Gallery被標(biāo)記為過(guò)時(shí)
一切的起源源于Gallery被標(biāo)記為過(guò)時(shí)迁酸,所以才有本文的出現(xiàn)锯蛀。
過(guò)時(shí)的原因绘雁,可以參考下面鏈接赋续。
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控件時(shí)照藻,需要先了解RecycleBin饺鹃。RecycleBin是一個(gè)容器類罐农,用來(lái)管理可回收的View条霜。
不像Adapter/ArrayList等,RecycleBin有很多同名類啃匿,它作為內(nèi)部類蛔外,存在于多個(gè)不同的類里面蛆楞,有著不同的實(shí)現(xiàn)方式。(目前只發(fā)現(xiàn)下面兩個(gè)類里面有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ù)一個(gè)和可見(jiàn)窗口同樣大小的View列表,當(dāng)列表滑動(dòng)時(shí)或南,不是不斷創(chuàng)建新的View孩等,而是重用滑出顯示區(qū)域的那些Views。RecycleBin容器相當(dāng)于回收區(qū)采够,用于管理滑出這個(gè)顯示區(qū)域的View肄方,表示可以重用的Views列表。(從命名字面意思也可以想到)
不同的列表控件蹬癌,有不同的回收機(jī)制权她,所以RecycleBin的實(shí)現(xiàn)也不同。反過(guò)來(lái)逝薪,如果使用了不當(dāng)?shù)腞ecycleBin隅要,會(huì)導(dǎo)致列表控件不能真正的利用到回收區(qū)的View,或者說(shuō)董济,不當(dāng)?shù)腞ecycleBin導(dǎo)致列表控件無(wú)法從回收區(qū)“命中”重用的View步清,導(dǎo)致每次都創(chuàng)建新的View。(從這個(gè)角度看虏肾,RecycleBin更像是一個(gè)Cache)
所以廓啊,RecycleBin扮演著一個(gè)回收區(qū)和緩存的角色。
Gallery控件的缺陷
Gallery控件就是這么回事询微,RecycleBin回收區(qū)設(shè)置不合理崖瞭,導(dǎo)致View的利用率很低狂巢。從代碼看撑毛,回收區(qū)以position為key,所以只有往回滑動(dòng)才能命中唧领,往前滑動(dòng)position是遞增的藻雌,每次都需要?jiǎng)?chuàng)建新的View。
實(shí)際上斩个,經(jīng)過(guò)測(cè)試胯杭,Gallery控件是每次都創(chuàng)建新的View,所以連往回滑動(dòng)都沒(méi)有“命中”受啥。通過(guò)代碼查看做个,除了最致命的選擇position作為回收區(qū)key外鸽心,在每次layout都對(duì)回收區(qū)進(jìn)行了clear,是導(dǎo)致連往回滾動(dòng)都無(wú)法命中的原因居暖。反正顽频,這應(yīng)該是Gallery控件的一個(gè)Bug,重用View的機(jī)制沒(méi)有生效太闺。(測(cè)試Demo見(jiàn)附錄)
Gallery控件另一個(gè)Bug是糯景,層疊效果。
Gallery控件通過(guò)重寫ViewGroup的getChildDrawingOrder
來(lái)使得繪制從兩邊到中間省骂,從而使列表項(xiàng)有層疊的效果蟀淮。然而,重寫的這個(gè)方法并不正確钞澳。重疊效果并不是越靠近中心的層級(jí)越高怠惶。如果你需要越靠近中心的層級(jí)越高,越靠近邊界的層級(jí)越低轧粟,那么你需要重寫這個(gè)方法甚疟。
@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逃延,那么上面這個(gè)方法的繪制順序是: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)記為過(guò)時(shí)览妖,至于過(guò)時(shí)的原因,據(jù)說(shuō)就是上面的View沒(méi)有重用導(dǎo)致效率低下揽祥。雖然Gallery控件有效率問(wèn)題讽膏,但它的展示效果還是比較獨(dú)特的:
- 選中居中效果
- 觸摸滑動(dòng)切換效果
- 多頁(yè)面Flip的效果(ViewPager只有3頁(yè)可見(jiàn))
- 頁(yè)面層疊效果(間距與層級(jí)的控制)
想快速完成上述效果,使用Gallery還是比較合適的拄丰,相比官方建議的HorizontalScrollView and ViewPager府树,還需要進(jìn)行一定量的修改。
EcoGallery的誕生就是因?yàn)镚allery被過(guò)時(shí)料按,但仍舊需要一個(gè)替代Gallery的控件奄侠。
EcoGallery并不神秘,它其實(shí)是Gallery源碼的一個(gè)拷貝载矿。作者將Gallery源碼從Android原生Widget中抽離出來(lái)垄潮,并對(duì)上述View重用的缺陷進(jìn)行了改進(jìn),實(shí)現(xiàn)了另一個(gè)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;
}
// 剩下的是沒(méi)用的
}
這個(gè)RecycleBin回收區(qū)是不管position逢勾,只要回收區(qū)有View牡整,就拿出來(lái)重用∧绻埃可以認(rèn)為逃贝,這里僅僅將RecycleBin當(dāng)作是回收區(qū)谣辞,而沒(méi)有作為緩存存在(緩存更接近于拿出來(lái)直接用,回收區(qū)重用會(huì)進(jìn)行一次初始化)沐扳。
Gallery另一個(gè)層疊的問(wèn)題潦闲,EcoGallery也存在。
FancyCoverFlow酷炫效果
如果想要下面3D Gallery的效果迫皱,那么就需要這個(gè)控件歉闰。FancyCoverFlow繼承于Gallery,而Gallery已經(jīng)過(guò)時(shí)卓起,所以可以修改使其繼承EcoGallery和敬。
[圖片上傳失敗...(image-7619da-1512009763589)]
附錄:
分析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控件。