Android原生Gallery的时间相册自定义分组的实现方法
2016-06-22 15:28
633 查看
作者:王挺(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<SmallItem> 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代码
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<SmallItem> 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代码
相关文章推荐
- Chapter5-Android Scroll 分析
- Android开源框架BRVAH由来篇
- Material Design 最全解析_1 概述
- Android实用代码模块集锦
- 项目中---关于AlertDialog的显示
- AndroidM如何给所有预装软件所有请求的权限
- Android进程管理详解
- android 画虚线,显示实心解决方法
- Android 实现沉浸式状态栏效果(systembartint库)
- Android SQLite分页查询的三种方法
- Android retrofit 日志拦截器
- Android应用的目录组织结构,每个文件夹及文件的用途?
- Android中应用程序如何获得系统签名权限
- android 6.0 动态权限处理
- Android 冷知识(1)开机广播及一些特殊广播
- Android 自定义实现switch开关按钮
- Android_个人中心_修改个人信息
- Android Fragment 你应该知道的一切
- android 软键盘设置
- Android Fragment 真正的完全解析(下)