标签:
转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/20481185),请尊重他人的辛勤劳动成果,谢谢!
大家好!过完年回来到现在差不多一个月没写文章了,一是觉得不知道写哪些方面的文章,没有好的题材来写,二是因为自己的一些私事给耽误了,所以过完年的第一篇文章到现在才发表出来,2014年我还是会继续在CSDN上面更新我的博客,欢迎大家关注一下,今天这篇文章主要的是介绍下开源库StickyGridHeaders的使用,StickyGridHeaders是一个自定义GridView带sections和headers的Android库,sections就是GridView item之间的分隔,headers就是固定在GridView顶部的标题,类似一些Android手机联系人的效果,StickyGridHeaders的介绍在https://github.com/TonicArtos/StickyGridHeaders,与此对应也有一个相同效果的自定义ListView带sections和headers的开源库https://github.com/emilsjolander/StickyListHeaders,大家有兴趣的可以去看下,我这里介绍的是StickyGridHeaders的使用,我在Android应用方面看到使用StickyGridHeaders的不是很多,而是在Iphone上看到相册采用的是这种效果,于是我就使用StickyGridHeaders来仿照Iphone按照日期分隔显示本地图片
我们先新建一个Android项目StickyHeaderGridView,去https://github.com/TonicArtos/StickyGridHeaders下载开源库,为了方便浏览源码我直接将源码拷到我的工程中了
com.tonicartos.widget.stickygridheaders这个包就是我放StickyGridHeaders开源库的源码,com.example.stickyheadergridview这个包是我实现此功能的代码,类看起来还蛮多的,下面我就一一来介绍了
GridItem用来封装StickyGridHeadersGridView 每个Item的数据,里面有本地图片的路径,图片加入手机系统的时间和headerId
- package com.example.stickyheadergridview;
- public class GridItem {
-
- private String path;
-
- private String time;
-
- private int headerId;
-
- public GridItem(String path, String time) {
- super();
- this.path = path;
- this.time = time;
- }
-
- public String getPath() {
- return path;
- }
- public void setPath(String path) {
- this.path = path;
- }
- public String getTime() {
- return time;
- }
- public void setTime(String time) {
- this.time = time;
- }
-
- public int getHeaderId() {
- return headerId;
- }
-
- public void setHeaderId(int headerId) {
- this.headerId = headerId;
- }
-
-
- }
图片的路径path和图片加入的时间time 我们直接可以通过ContentProvider获取,但是headerId需要我们根据逻辑来生成。
- package com.example.stickyheadergridview;
-
- import android.content.ContentResolver;
- import android.content.Context;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Message;
- import android.provider.MediaStore;
- public class ImageScanner {
- private Context mContext;
-
- public ImageScanner(Context context){
- this.mContext = context;
- }
-
-
- public void scanImages(final ScanCompleteCallBack callback) {
- final Handler mHandler = new Handler() {
-
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- callback.scanComplete((Cursor)msg.obj);
- }
- };
-
- new Thread(new Runnable() {
-
- @Override
- public void run() {
-
- mContext.sendBroadcast(new Intent(
- Intent.ACTION_MEDIA_MOUNTED,
- Uri.parse("file://" + Environment.getExternalStorageDirectory())));
-
- Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
- ContentResolver mContentResolver = mContext.getContentResolver();
-
- Cursor mCursor = mContentResolver.query(mImageUri, null, null, null, MediaStore.Images.Media.DATE_ADDED);
-
-
- Message msg = mHandler.obtainMessage();
- msg.obj = mCursor;
- mHandler.sendMessage(msg);
- }
- }).start();
-
- }
-
-
- public static interface ScanCompleteCallBack{
- public void scanComplete(Cursor cursor);
- }
-
-
- }
ImageScanner是一个图片的扫描器类,该类使用ContentProvider扫描手机中的图片,我们通过调用scanImages()方法就能对手机中的图片进行扫描,将扫描的Cursor回调到ScanCompleteCallBack 接口的scanComplete方法中,由于考虑到扫描图片属于耗时操作,所以该操作运行在子线程中,在我们扫描图片之前我们需要先发送广播来扫描外部媒体库,为什么要这么做呢,假如我们新增加一张图片到sd卡,图片确实已经添加了进去,但是我们此时的媒体库还没有同步更新,若不同步媒体库我们就看不到新增加的图片,当然我们可以通过重新启动系统来更新媒体库,但是这样不可取,所以我们直接发送广播就可以同步媒体库了。
- package com.example.stickyheadergridview;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
-
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Point;
- import android.os.Handler;
- import android.os.Message;
- import android.support.v4.util.LruCache;
- import android.util.Log;
-
- public class NativeImageLoader {
- private static final String TAG = NativeImageLoader.class.getSimpleName();
- private static NativeImageLoader mInstance = new NativeImageLoader();
- private static LruCache<String, Bitmap> mMemoryCache;
- private ExecutorService mImageThreadPool = Executors.newFixedThreadPool(1);
-
-
- private NativeImageLoader(){
-
- final int maxMemory = (int) (Runtime.getRuntime().maxMemory());
-
-
- final int cacheSize = maxMemory / 8;
- mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
-
-
- @Override
- protected int sizeOf(String key, Bitmap bitmap) {
- return bitmap.getRowBytes() * bitmap.getHeight();
- }
-
- };
- }
-
-
- public static NativeImageLoader getInstance(){
- return mInstance;
- }
-
-
-
- public Bitmap loadNativeImage(final String path, final NativeImageCallBack mCallBack){
- return this.loadNativeImage(path, null, mCallBack);
- }
-
-
- public Bitmap loadNativeImage(final String path, final Point mPoint, final NativeImageCallBack mCallBack){
-
- Bitmap bitmap = getBitmapFromMemCache(path);
-
- final Handler mHander = new Handler(){
-
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- mCallBack.onImageLoader((Bitmap)msg.obj, path);
- }
-
- };
-
-
- if(bitmap == null){
- mImageThreadPool.execute(new Runnable() {
-
- @Override
- public void run() {
-
- Bitmap mBitmap = decodeThumbBitmapForFile(path, mPoint == null ? 0: mPoint.x, mPoint == null ? 0: mPoint.y);
- Message msg = mHander.obtainMessage();
- msg.obj = mBitmap;
- mHander.sendMessage(msg);
-
-
- addBitmapToMemoryCache(path, mBitmap);
- }
- });
- }
- return bitmap;
-
- }
-
-
-
-
- private void addBitmapToMemoryCache(String key, Bitmap bitmap) {
- if (getBitmapFromMemCache(key) == null && bitmap != null) {
- mMemoryCache.put(key, bitmap);
- }
- }
-
-
- private Bitmap getBitmapFromMemCache(String key) {
- Bitmap bitmap = mMemoryCache.get(key);
-
- if(bitmap != null){
- Log.i(TAG, "get image for LRUCache , path = " + key);
- }
- return bitmap;
- }
-
-
- public void trimMemCache(){
- mMemoryCache.evictAll();
- }
-
-
-
- private Bitmap decodeThumbBitmapForFile(String path, int viewWidth, int viewHeight){
- BitmapFactory.Options options = new BitmapFactory.Options();
-
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(path, options);
-
- options.inSampleSize = computeScale(options, viewWidth, viewHeight);
-
-
- options.inJustDecodeBounds = false;
-
-
- Log.e(TAG, "get Iamge form file, path = " + path);
-
- return BitmapFactory.decodeFile(path, options);
- }
-
-
-
- private int computeScale(BitmapFactory.Options options, int viewWidth, int viewHeight){
- int inSampleSize = 1;
- if(viewWidth == 0 || viewWidth == 0){
- return inSampleSize;
- }
- int bitmapWidth = options.outWidth;
- int bitmapHeight = options.outHeight;
-
-
- if(bitmapWidth > viewWidth || bitmapHeight > viewWidth){
- int widthScale = Math.round((float) bitmapWidth / (float) viewWidth);
- int heightScale = Math.round((float) bitmapHeight / (float) viewWidth);
-
-
- inSampleSize = widthScale < heightScale ? widthScale : heightScale;
- }
- return inSampleSize;
- }
-
-
-
- public interface NativeImageCallBack{
-
- public void onImageLoader(Bitmap bitmap, String path);
- }
- }
NativeImageLoader该类是一个单例类,提供了本地图片加载,内存缓存,裁剪等逻辑,该类在加载本地图片的时候采用的是异步加载的方式,对于大图片的加载也是比较耗时的,所以采用子线程的方式去加载,对于图片的缓存机制使用的是LruCache,我们使用手机分配给应用程序内存的1/8用来缓存图片,给图片缓存的内存不宜太大,太大也可能会发生OOM,该类是用我之前写的文章Android 使用ContentProvider扫描手机中的图片,仿微信显示本地图片效果,在这里我就不做过多的介绍,有兴趣的可以去看看那篇文章,不过这里新增了一个方法trimMemCache(),,用来清空LruCache使用的内存
我们看主界面的布局代码,里面只有一个自定义的StickyGridHeadersGridView控件
- <?xml version="1.0" encoding="utf-8"?>
- <com.tonicartos.widget.stickygridheaders.StickyGridHeadersGridView xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/asset_grid"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:clipToPadding="false"
- android:columnWidth="90dip"
- android:horizontalSpacing="3dip"
- android:numColumns="auto_fit"
- android:verticalSpacing="3dip" />
在看主界面的代码之前我们先看StickyGridAdapter的代码
- package com.example.stickyheadergridview;
-
- import java.util.List;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Point;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.GridView;
- import android.widget.ImageView;
- import android.widget.TextView;
-
- import com.example.stickyheadergridview.MyImageView.OnMeasureListener;
- import com.example.stickyheadergridview.NativeImageLoader.NativeImageCallBack;
- import com.tonicartos.widget.stickygridheaders.StickyGridHeadersSimpleAdapter;
- public class StickyGridAdapter extends BaseAdapter implements
- StickyGridHeadersSimpleAdapter {
-
- private List<GridItem> hasHeaderIdList;
- private LayoutInflater mInflater;
- private GridView mGridView;
- private Point mPoint = new Point(0, 0);
-
- public StickyGridAdapter(Context context, List<GridItem> hasHeaderIdList,
- GridView mGridView) {
- mInflater = LayoutInflater.from(context);
- this.mGridView = mGridView;
- this.hasHeaderIdList = hasHeaderIdList;
- }
-
-
- @Override
- public int getCount() {
- return hasHeaderIdList.size();
- }
-
- @Override
- public Object getItem(int position) {
- return hasHeaderIdList.get(position);
- }
-
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder mViewHolder;
- if (convertView == null) {
- mViewHolder = new ViewHolder();
- convertView = mInflater.inflate(R.layout.grid_item, parent, false);
- mViewHolder.mImageView = (MyImageView) convertView
- .findViewById(R.id.grid_item);
- convertView.setTag(mViewHolder);
-
-
- mViewHolder.mImageView.setOnMeasureListener(new OnMeasureListener() {
-
- @Override
- public void onMeasureSize(int width, int height) {
- mPoint.set(width, height);
- }
- });
-
- } else {
- mViewHolder = (ViewHolder) convertView.getTag();
- }
-
- String path = hasHeaderIdList.get(position).getPath();
- mViewHolder.mImageView.setTag(path);
-
- Bitmap bitmap = NativeImageLoader.getInstance().loadNativeImage(path, mPoint,
- new NativeImageCallBack() {
-
- @Override
- public void onImageLoader(Bitmap bitmap, String path) {
- ImageView mImageView = (ImageView) mGridView
- .findViewWithTag(path);
- if (bitmap != null && mImageView != null) {
- mImageView.setImageBitmap(bitmap);
- }
- }
- });
-
- if (bitmap != null) {
- mViewHolder.mImageView.setImageBitmap(bitmap);
- } else {
- mViewHolder.mImageView.setImageResource(R.drawable.friends_sends_pictures_no);
- }
-
- return convertView;
- }
-
-
- @Override
- public View getHeaderView(int position, View convertView, ViewGroup parent) {
- HeaderViewHolder mHeaderHolder;
-
- if (convertView == null) {
- mHeaderHolder = new HeaderViewHolder();
- convertView = mInflater.inflate(R.layout.header, parent, false);
- mHeaderHolder.mTextView = (TextView) convertView
- .findViewById(R.id.header);
- convertView.setTag(mHeaderHolder);
- } else {
- mHeaderHolder = (HeaderViewHolder) convertView.getTag();
- }
- mHeaderHolder.mTextView.setText(hasHeaderIdList.get(position).getTime());
-
- return convertView;
- }
-
-
- @Override
- public long getHeaderId(int position) {
- return hasHeaderIdList.get(position).getHeaderId();
- }
-
-
- public static class ViewHolder {
- public MyImageView mImageView;
- }
-
- public static class HeaderViewHolder {
- public TextView mTextView;
- }
-
-
-
- }
除了要继承BaseAdapter之外还需要实现StickyGridHeadersSimpleAdapter接口,继承BaseAdapter需要实现getCount(),getItem(int position), getItemId(int position),getView(int position, View convertView, ViewGroup parent)这四个方法,这几个方法的实现跟我们平常实现的方式一样,主要是看一下getView()方法,我们将每个item的图片路径设置Tag到该ImageView上面,然后利用NativeImageLoader来加载本地图片,在这里使用的ImageView依然是自定义的MyImageView,该自定义ImageView主要实现当MyImageView测量完毕之后,就会将测量的宽和高回调到onMeasureSize()中,然后我们可以根据MyImageView的大小来裁剪图片
另外我们需要实现StickyGridHeadersSimpleAdapter接口的getHeaderId(int position)和getHeaderView(int position, View convertView, ViewGroup parent),getHeaderId(int position)方法返回每个Item的headerId,getHeaderView()方法是生成sections和headers的,如果某个item的headerId跟他下一个item的HeaderId不同,则会调用getHeaderView方法生成一个sections用来区分不同的组,还会根据firstVisibleItem的headerId来生成一个位于顶部的headers,所以如何生成每个Item的headerId才是关键,生成headerId的方法在MainActivity中
- package com.example.stickyheadergridview;
-
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.ListIterator;
- import java.util.Map;
- import java.util.TimeZone;
-
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.widget.GridView;
-
- import com.example.stickyheadergridview.ImageScanner.ScanCompleteCallBack;
-
- public class MainActivity extends Activity {
- private ProgressDialog mProgressDialog;
-
- private ImageScanner mScanner;
- private GridView mGridView;
-
- private List<GridItem> nonHeaderIdList = new ArrayList<GridItem>();
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- mGridView = (GridView) findViewById(R.id.asset_grid);
- mScanner = new ImageScanner(this);
-
- mScanner.scanImages(new ScanCompleteCallBack() {
- {
- mProgressDialog = ProgressDialog.show(MainActivity.this, null, "正在加载...");
- }
-
- @Override
- public void scanComplete(Cursor cursor) {
-
- mProgressDialog.dismiss();
-
- if(cursor == null){
- return;
- }
-
- while (cursor.moveToNext()) {
-
- String path = cursor.getString(cursor
- .getColumnIndex(MediaStore.Images.Media.DATA));
-
- long times = cursor.getLong(cursor
- .getColumnIndex(MediaStore.Images.Media.DATE_ADDED));
-
- GridItem mGridItem = new GridItem(path, paserTimeToYMD(times, "yyyy年MM月dd日"));
- nonHeaderIdList.add(mGridItem);
-
- }
- cursor.close();
-
-
- List<GridItem> hasHeaderIdList = generateHeaderId(nonHeaderIdList);
-
- Collections.sort(hasHeaderIdList, new YMDComparator());
- mGridView.setAdapter(new StickyGridAdapter(MainActivity.this, hasHeaderIdList, mGridView));
-
- }
- });
- }
-
-
-
- private List<GridItem> generateHeaderId(List<GridItem> nonHeaderIdList) {
- Map<String, Integer> mHeaderIdMap = new HashMap<String, Integer>();
- int mHeaderId = 1;
- List<GridItem> hasHeaderIdList;
-
- for(ListIterator<GridItem> it = nonHeaderIdList.listIterator(); it.hasNext();){
- GridItem mGridItem = it.next();
- String ymd = mGridItem.getTime();
- if(!mHeaderIdMap.containsKey(ymd)){
- mGridItem.setHeaderId(mHeaderId);
- mHeaderIdMap.put(ymd, mHeaderId);
- mHeaderId ++;
- }else{
- mGridItem.setHeaderId(mHeaderIdMap.get(ymd));
- }
- }
- hasHeaderIdList = nonHeaderIdList;
-
- return hasHeaderIdList;
- }
-
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
-
- NativeImageLoader.getInstance().trimMemCache();
- }
-
-
-
- public static String paserTimeToYMD(long time, String pattern ) {
- System.setProperty("user.timezone", "Asia/Shanghai");
- TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
- TimeZone.setDefault(tz);
- SimpleDateFormat format = new SimpleDateFormat(pattern);
- return format.format(new Date(time * 1000L));
- }
-
- }
主界面的代码主要是组装StickyGridHeadersGridView的数据,我们将扫描出来的图片的路径,时间的毫秒数解析成年月日的格式封装到GridItem中,然后将GridItem加入到List中,此时每个Item还没有生成headerId,我们需要调用generateHeaderId(),该方法主要是将同一天加入的系统的图片生成相同的HeaderId,这样子同一天加入的图片就在一个组中,当然你要改成同一个月的图片在一起,修改paserTimeToYMD()方法的第二个参数就行了,当Activity finish之后,我们利用NativeImageLoader.getInstance().trimMemCache()释放内存,当然我们还需要对GridView的数据进行排序,比如说headerId相同的item不连续,headerId相同的item就会生成多个sections(即多个分组),所以我们要利用YMDComparator使得在同一天加入的图片在一起,YMDComparator的代码如下
- package com.example.stickyheadergridview;
-
- import java.util.Comparator;
-
- public class YMDComparator implements Comparator<GridItem> {
-
- @Override
- public int compare(GridItem o1, GridItem o2) {
- return o1.getTime().compareTo(o2.getTime());
- }
-
- }
当然这篇文章不使用YMDComparator也是可以的,因为我在利用ContentProvider获取图片的时候,就是根据加入系统的时间排序的,排序只是针对一般的数据来说的。
接下来我们运行下程序看看效果如何
今天的文章就到这里结束了,感谢大家的观看,上面还有一个类和一些资源文件没有贴出来,大家有兴趣研究下就直接下载项目源码,记住采用LruCache缓存图片的时候,cacheSize不要设置得过大,不然产生OOM的概率就更大些,我利用上面的程序测试显示600多张图片来回滑动,没有产生OOM,有问题不明白的同学可以在下面留言!
Android 使用开源库StickyGridHeaders来实现带sections和headers的GridView显示本地图片效果
标签:
原文地址:http://www.cnblogs.com/android-blogs/p/4844588.html