Android多媒体教程:实现仿百度图片查看功能


我们知道,进入百度图片后,输入一个关键字后,首先看到的是很多缩略图,当我们点击某张缩略图时,我们就可以进入到大图显示页面,在

大图显示页面,中包含了一个图片画廊,同时当前大图为刚刚我们点击的那张图片。现在我们看看在Android中如何实现类似的效果:

首先,我们需要有一个控件来显示缩略图,这里没有什么比GridView更加合适了。

配置文件如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <GridView  
  8.         android:id="@+id/view_photos"   
  9.         android:layout_width="fill_parent"   
  10.         android:layout_height="fill_parent"   
  11.         android:layout_marginTop="10dp"   
  12.         android:columnWidth="100dp"   
  13.         android:numColumns="auto_fit"   
  14.         android:horizontalSpacing="5dp"   
  15.         android:verticalSpacing="5dp"   
  16.         android:listSelector="@drawable/frame_select" />  
  17. </LinearLayout>  

对于GridView中每一项是一张缩略图,我们需要继承BaseAdapter,实现自己的一个GridImageAdapter,代码:

  1. package com.liner.manager;  
  2. import java.util.List;  
  3. import com.liner.manager.adapter.GridImageAdapter;  
  4. import android.app.Activity;  
  5. import android.graphics.Bitmap;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.AdapterView;  
  9. import android.widget.Gallery;  
  10. import android.widget.ImageButton;  
  11. import android.widget.AdapterView.OnItemClickListener;  
  12. public class GalleryActivity extends Activity{  
  13.       
  14.     private ImageButton currentImage;  
  15.     private Gallery gallery;  
  16.       
  17.     private int[] thumbIds;  
  18.     private int currentPos;  
  19.       
  20.     private Bitmap currentBitmap;  
  21.       
  22.     private List<Bitmap> bitmapCache;  
  23.       
  24.     public void onCreate(Bundle savedInstanceState){  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.gallery);  
  27.           
  28.         currentImage = (ImageButton)this.findViewById(R.id.image_current);  
  29.         gallery = (Gallery)this.findViewById(R.id.image_gallery);  
  30.         gallery.setOnItemClickListener(galleryItemClickListener);  
  31.         init();  
  32.     }  
  33.       
  34.     private OnItemClickListener galleryItemClickListener = new OnItemClickListener() {  
  35.         @Override  
  36.         public void onItemClick(AdapterView<?> p, View v, int position,  
  37.                 long id) {  
  38.             // 点击事件  
  39.             showCurrentImage(position);  
  40.         }  
  41.     };  
  42.       
  43.     private void init(){  
  44.         thumbIds = this.getIntent().getIntArrayExtra("thumbIds");  
  45.         currentPos = this.getIntent().getIntExtra("currentPos",0);  
  46.         //galleryIds = this.getThumbnailIds(currentPos); //当前的gallery里的图片信息  
  47.         bitmapCache = BitmapUtils.queryThumbnailListByIds(this, thumbIds);  
  48.         GridImageAdapter adapter = new GridImageAdapter(this.getApplication(), bitmapCache);  
  49.         gallery.setAdapter(adapter);  
  50.         gallery.setSelection(currentPos);  
  51.           
  52.         showCurrentImage(currentPos);  
  53.           
  54.     }  
  55.       
  56.     private void showCurrentImage(int position){  
  57.           
  58.         if(currentBitmap != null){  
  59.             currentBitmap.recycle();  
  60.         }  
  61.           
  62.         currentBitmap = BitmapUtils.queryImageByThumbnailId(GalleryActivity.this, thumbIds[position]);  
  63.         if(currentBitmap != null){  
  64.             currentImage.setImageBitmap(currentBitmap);  
  65.         }else{  
  66.             //什么都不做  
  67.         }  
  68.           
  69.         //releaseBitmaps();       
  70.     }  
  71.       
  72.     /** 
  73.      * 将Gallery当前可见的显示之前的3张,后3张缓存起来,其余的释放掉,这样是为了放置内存不够用 
  74.      * 之所以前三张后三张,是为了Gallery可以滑动的更加顺畅 
  75.      */  
  76.     private void releaseBitmaps(){  
  77.         int start = gallery.getFirstVisiblePosition()-3//缓存的起始位置  
  78.         int end = gallery.getLastVisiblePosition()+3//缓存的结束位置  
  79.           
  80.         Bitmap delBitmap;  
  81.         for(int i=0; i<start; i++){  
  82.             delBitmap = bitmapCache.get(i);  
  83.             if(delBitmap != null){  
  84.                 bitmapCache.remove(i);  
  85.                 delBitmap.recycle();  
  86.             }  
  87.         }  
  88.         for(int i=end+1; i<bitmapCache.size(); i++){  
  89.             delBitmap = bitmapCache.get(i);  
  90.             if(delBitmap != null){  
  91.                 bitmapCache.remove(i);  
  92.                 delBitmap.recycle();  
  93.             }  
  94.         }  
  95.     }  
  96.       
  97.     /** 
  98.      * 获取当前位置的前三个Id和后三个Id 
  99.      * @param position 
  100.      * @return 
  101.      */  
  102.     private Integer[] getThumbnailIds(int position){  
  103.         Integer[] ids = new Integer[]{0,0,0,0,0,0,0};  
  104.         int currPos = 0;  
  105.         //关于这里的处理,比较复杂  
  106.         for(int i=3; i>0; i--){  
  107.             if(position - i >= 0){  
  108.                 currPos = 3-i;  
  109.                 ids[currPos] = thumbIds[position-i];  
  110.             }  
  111.         }  
  112.         ids[++currPos] = thumbIds[position]; //当前Id  
  113.         //currGallerySelection = currPos;  
  114.         //这样右边剩下的位置数就是7-currPos-1  
  115.         for(int i=1; i<=6-currPos;i++){  
  116.             if(position+i < thumbIds.length){  
  117.                 ids[currPos+i] = thumbIds[position+i];  
  118.             }  
  119.         }  
  120.           
  121.         return ids;  
  122.     }     
  123. }  
  • 1
  • 2
  • 3
  • 下一页

相关内容