看了一些别人的代码,然后自己修改了一下,现在把代码贴出来
首先是界面布局文件
android:spacing="50pt",设置图片之间间距
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:spacing="50pt" >
</Gallery>
</LinearLayout>
程序代码如下
PhotoActivity代码
package com.example.photo;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import com.example.flight_control_001.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.SpinnerAdapter;
public class PhotoActivity extends Activity {
private static final String PIC_PATH = "/mnt/sdcard/Flight photo/";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
Gallery g=(Gallery)findViewById(R.id.gallery);
//添加一个ImageAdapter并设置给Gallery对象
g.setAdapter( new ImageAdapter(this,getSD()));
//设置一个itemclickListener事件
g.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
private List<String> getSD() {
// TODO Auto-generated method stub
//设置目前所在路径
List<String> it=new ArrayList<String>();
File f=new File(PIC_PATH);
File[] files=f.listFiles();
//将所有文件添加到ArrayList中
for(int i=0;i<files.length;i++){
File file=files[i];
if(getImageFile(file.getPath()))
it.add(file.getPath());
}
return it;
}
private boolean getImageFile(String fName) {
// TODO Auto-generated method stub
boolean re;
//取得扩展名
String end=fName.substring(fName.lastIndexOf(".")+1, fName.length()).toLowerCase();
//根据扩展名的类型决定MimeType
if(end.equals("jpeg")){
re=true;
}else {
re=false;
}
return re;
}
}
新建一个ImageAdapter类
package com.example.photo;
import java.util.List;
import com.example.flight_control_001.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
//声明变量
int mGalleryItemBackgroud;
private Context mContext;
private List<String> lis;
//ImageAdapter的构造器
public ImageAdapter(Context c, List<String> li){
mContext=c;
lis=li;
/*//使用在res/values/attrs.xml中的<declare-styleable>定义的Gallery属性
TypedArray a=ImageAdapter.this.obtainStyledAttributes(R.styleable.Gallery);
//取得Gallery属性的Index id
mGalleryItemBackgroud=a.getResourceId(R.styleable.Gallery_android_galleryItemBackgroud,0);
//让对象的styleable属性能够反复使用
a.recycle();*/
}
//重写的方法getCount,返回图片数目
@Override
public int getCount() {
// TODO Auto-generated method stub
return lis.size();
}
//重写的方法getItem,返回position
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
//重写的方法getItemId,返回position
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//重写的方法getView,返回一View对象
@SuppressWarnings("deprecation")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//产生ImageView对象
ImageView i=new ImageView(mContext);
//设置图片给imageView对象
Bitmap bm=BitmapFactory.decodeFile(lis.get(position).toString());
i.setImageBitmap(bm);
//重写设置图片的宽、高
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
//重新设置layout的宽、高
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//使Gallery图片自适应屏幕分辨率,以免图片bound超出屏幕范围
i.setAdjustViewBounds(true);
//设置Gallery背景图
i.setBackgroundResource(mGalleryItemBackgroud);
//返回imageView对象
return i;
}
}
这就完成了