作者:王挺(ting.wong)
Android原生Gallery中的时间相册,在按时间分组时,会自动平均每个时间相册的照片数量和时间,结果会出现一天的照片出现在多个分组的情况,如下图:
查阅到时间分组相关的代码如下(Gallery2\src\com\android\gallery3d\data\TimeClustering.java):
@Override
public void run(MediaSet baseSet) {
……
int n = items.size();
long minTime = 0;
long maxTime = 0;
for (int i = 0; i < n; i++) {
long t = items.get(i).dateInMs;
if (t == 0)
continue;
if (minTime == 0) {
minTime = maxTime = t;
} else {
minTime = Math.min(minTime,t);
maxTime = Math.max(maxTime,t);
}
}
setTimeRange(maxTime - minTime, n);
for (int i = 0; i < n; i++) {
compute(items.get(i));
}
compute(null);
……
}
经过如下修改,时间相册分组就可以符合要求了,相关的代码分享如下:
@Override
public void run(MediaSet baseSet) {
……
if ( ATpvConstVariable.ALBUMSET_TIME_USE_OLD )
{
int n = items.size();
long minTime = 0;
long maxTime = 0;
for (int i = 0; i < n; i++) {
long t = items.get(i).dateInMs;
if (t == 0)
continue;
if (minTime == 0) {
minTime = maxTime = t;
} else {
minTime = Math.min(minTime,t);
maxTime = Math.max(maxTime, t);
}
}
setTimeRange(maxTime - minTime, n);
for (int i = 0; i < n; i++) {
compute(items.get(i));
}
compute(null);
}
else
{
computeNew(items);
}
……
}
public void computeNew(ArrayList items)
{
int n = items.size();
if ( n<1 )
{
return;
}
int i=0;
StringstrDataLast="";
for ( i = 0; i < n; i++) {
SmallItemcurrentItem=items.get(i);
long t = currentItem.dateInMs;
if (t == 0)
continue;
StringstrDate=ATpvConstVariable.getDateByDateInMs(t);
if ( strDataLast.isEmpty() ||strDataLast.equalsIgnoreCase(strDate) )
{
mCurrCluster.addItem(currentItem);
}
else
{
mClusters.add(mCurrCluster);
mCurrCluster =
new Cluster();
mCurrCluster.addItem(currentItem);
}
strDataLast=strDate;
}
mClusters.add(mCurrCluster);
}
最终分组后情况如下(按当月每日一个相册,非当月每月一个相册分组):
注:文中Gallery基于MTK6755的Android 6.0中的Gallery代码