我们知道,进入百度图片后,输入一个关键字后,首先看到的是很多缩略图,当我们点击某张缩略图时,我们就可以进入到大图显示页面,在
大图显示页面,中包含了一个图片画廊,同时当前大图为刚刚我们点击的那张图片。现在我们看看在Android中如何实现类似的效果:
首先,我们需要有一个控件来显示缩略图,这里没有什么比GridView更加合适了。
配置文件如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <GridView android:id="@+id/view_photos" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="10dp" android:columnWidth="100dp" android:numColumns="auto_fit" android:horizontalSpacing="5dp" android:verticalSpacing="5dp" android:listSelector="@drawable/frame_select" /> </LinearLayout>对于GridView中每一项是一张缩略图,我们需要继承BaseAdapter,实现自己的一个GridImageAdapter,代码:
package com.liner.manager; import java.util.List; import com.liner.manager.adapter.GridImageAdapter; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.ImageButton; import android.widget.AdapterView.OnItemClickListener; public class GalleryActivity extends Activity{ private ImageButton currentImage; private Gallery gallery; private int[] thumbIds; private int currentPos; private Bitmap currentBitmap; private List<Bitmap> bitmapCache; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.gallery); currentImage = (ImageButton)this.findViewById(R.id.image_current); gallery = (Gallery)this.findViewById(R.id.image_gallery); gallery.setOnItemClickListener(galleryItemClickListener); init(); } private OnItemClickListener galleryItemClickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> p, View v, int position, long id) { // 点击事件 showCurrentImage(position); } }; private void init(){ thumbIds = this.getIntent().getIntArrayExtra("thumbIds"); currentPos = this.getIntent().getIntExtra("currentPos",0); //galleryIds = this.getThumbnailIds(currentPos); //当前的gallery里的图片信息 bitmapCache = BitmapUtils.queryThumbnailListByIds(this, thumbIds); GridImageAdapter adapter = new GridImageAdapter(this.getApplication(), bitmapCache); gallery.setAdapter(adapter); gallery.setSelection(currentPos); showCurrentImage(currentPos); } private void showCurrentImage(int position){ if(currentBitmap != null){ currentBitmap.recycle(); } currentBitmap = BitmapUtils.queryImageByThumbnailId(GalleryActivity.this, thumbIds[position]); if(currentBitmap != null){ currentImage.setImageBitmap(currentBitmap); }else{ //什么都不做 } //releaseBitmaps(); } /** * 将Gallery当前可见的显示之前的3张,后3张缓存起来,其余的释放掉,这样是为了放置内存不够用 * 之所以前三张后三张,是为了Gallery可以滑动的更加顺畅 */ private void releaseBitmaps(){ int start = gallery.getFirstVisiblePosition()-3; //缓存的起始位置 int end = gallery.getLastVisiblePosition()+3; //缓存的结束位置 Bitmap delBitmap; for(int i=0; i<start; i++){ delBitmap = bitmapCache.get(i); if(delBitmap != null){ bitmapCache.remove(i); delBitmap.recycle(); } } for(int i=end+1; i<bitmapCache.size(); i++){ delBitmap = bitmapCache.get(i); if(delBitmap != null){ bitmapCache.remove(i); delBitmap.recycle(); } } } /** * 获取当前位置的前三个Id和后三个Id * @param position * @return */ private Integer[] getThumbnailIds(int position){ Integer[] ids = new Integer[]{0,0,0,0,0,0,0}; int currPos = 0; //关于这里的处理,比较复杂 for(int i=3; i>0; i--){ if(position - i >= 0){ currPos = 3-i; ids[currPos] = thumbIds[position-i]; } } ids[++currPos] = thumbIds[position]; //当前Id //currGallerySelection = currPos; //这样右边剩下的位置数就是7-currPos-1 for(int i=1; i<=6-currPos;i++){ if(position+i < thumbIds.length){ ids[currPos+i] = thumbIds[position+i]; } } return ids; } }