Android应用开发之电子相册

在iphone手机上用手指拖动图片移动,这功能很Cool,咱Android也不能含糊,用Gallery类就可以实现这个功能。

今天我就做了个小小的电子相册:

假设你已经新建好了项目。

首先我们事先准备好的图片存放在drawable文件夹下,然后新建一个接口:

  1. public interface ImageResource {  
  2.    //用一个Integer数组保存图像资源   
  3.    Integer[] dImageID = {  
  4.       R.drawable.sample_0,  
  5.       R.drawable.sample_1,  
  6.       R.drawable.sample_2,  
  7.       R.drawable.sample_3,  
  8.       R.drawable.sample_4,  
  9.       R.drawable.sample_5,  
  10.       R.drawable.sample_6,  
  11.       R.drawable.sample_7,  
  12.    };  
  13. }  

这个接口里有一个int型的数组保存了咱drawable下的图片,这样方便以后修改。

要用Gallery实现这个功能,先要有一个容器来存放Gallery要显示的图片,我使用的是一个继承自BaseAdapter的ImageAdapter。这个电子相册分两部分,上部分是用户可以拖动图像列表,下部分是用户选中上面图像后更大的显示出来。


负责上部分显示的ImageView是imageAll,负责下部分显示的是imageOne,代码如下:

  1. import android.app.Activity;  
  2. import android.content.Context;  
  3. import android.content.res.TypedArray;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.BaseAdapter;  
  8. import android.widget.ImageView;  
  9. import android.widget.Gallery;  
  10.     
  11. public class HelloGallery extends Activity {  
  12.     //这个ImageView是用来显示单个图像   
  13.     private ImageView imageOne;  
  14.     //这个ImageView是用来显示所有图像   
  15.     private ImageView imageAll;  
  16.       
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.           
  22.         imageOne = (ImageView)findViewById(R.id.imageView);  
  23.           
  24.         //新建一个Gallery类,这是是实现拖动效果的关键   
  25.         //Gallery is 一个用来水平卷动显示对象的视图   
  26.         Gallery gallery = (Gallery)findViewById(R.id.gallery);  
  27.           
  28.         //ImageAdapter就是gallery要显示的对象   
  29.         gallery.setAdapter(new ImageAdapter(this));  
  30.           
  31.     }  
  32.     //实现ImageResource接口获得我自己创建的图像资源   
  33.     class ImageAdapter extends BaseAdapter implements ImageResource{  
  34.     //每一个gallery中图像的背景资源   
  35.     private int galleryItemBackground;  
  36.     private Context context;  
  37.    
  38.     public ImageAdapter(Context context) {  
  39.         this.context = context;  
  40.          //这里实现的功能就是上半部分每个图像的那个背景框   
  41. //对应的xml文件就是attr.xml   
  42.          TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);  
  43.          galleryItemBackground = a.getResourceId(  
  44.                      R.styleable.Gallery1_android_galleryItemBackground, 0);  
  45.              a.recycle();  
  46.     }  
  47.    
  48.        public int getCount() {  
  49.            return dImageID.length;  
  50.        }  
  51.    
  52.        public Object getItem(int position) {  
  53.            return position;  
  54.        }  
  55.    
  56.        //这个方法获得是呈现在用户面前的图像下标   
  57.        public long getItemId(int position) {  
  58.            //将此索引的图像设为imageOne显示           imageOne.setImageResource(ImageAdapter.dImageID[position]);   
  59.            imageOne.setScaleType(ImageView.ScaleType.FIT_CENTER);  
  60.            return position;  
  61.        }  
  62.    
  63.        //这个方法返回的ImageView就是实现拖动效果的图像   
  64.        public View getView(int position, View convertView, ViewGroup parent) {  
  65.            imageAll= new ImageView(context);  
  66.            //设置图像资源   
  67.            imageAll.setImageResource(dImageID[position]);  
  68.            //设置imageAll视图为120×120   
  69.            imageAll.setLayoutParams(new Gallery.LayoutParams(120,120));  
  70.            //设置图像相对于视图的比例,FIT_XY表示充满X和Y轴   
  71.            imageAll.setScaleType(ImageView.ScaleType.FIT_XY);  
  72.            //设置imageAll中每一个Item的背景资源   
  73.            imageAll.setBackgroundResource(galleryItemBackground);  
  74.            return imageAll;  
  75.        }  
  76.      }  
  77. }  

布局文件如下:

Mail.xml:

  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. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12. <Gallery android:id="@+id/gallery"   
  13.     android:layout_width="fill_parent"   
  14.     android:layout_height="wrap_content">  
  15. </Gallery>  
  16.    
  17. <ImageView android:id="@+id/imageView"  
  18.     android:layout_height="fill_parent"  
  19.     android:layout_width="fill_parent">  
  20. </ImageView>  
  21. </LinearLayout>  

下面就是上文中提到的attr.xml:

Attr.xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="Gallery1">  
  4.         <attr name="android:galleryItemBackground" />  
  5.     </declare-styleable>  
  6.      <declare-styleable name="LabelView">  
  7.         <attr name="text" format="string" />  
  8.         <attr name="textColor" format="color" />  
  9.         <attr name="textSize" format="dimension" />  
  10.     </declare-styleable>  
  11. </resources>  

你可能感兴趣的:(Android应用开发之电子相册)