我們知道,進入百度圖片後,輸入一個關鍵字後,首先看到的是很多縮略圖,當我們點擊某張縮略圖時,我們就可以進入到大圖顯示頁面,在
大圖顯示頁面,中包含了一個圖片畫廊,同時當前大圖為剛剛我們點擊的那張圖片。現在我們看看在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;
- }
- }