package com.test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;public classToolsActivity extends Activity {
Gallery g= null;
List it = null;//遍历符合条件的列表
public String actionUrl = null;//图片的数量
private int count_drawble = 0;//当前正在播放的图片ID
private int cur_index = 0;private boolean isalive = true;//线程循环运行的条件//更新条件
private static int MSG_UPDATE = 1;//SD卡路径
private final String SD_PATH =android.os.Environment
.getExternalStorageDirectory().getAbsolutePath();public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_photo);
g=(Gallery) findViewById(R.id.mygallery);//添加一个ImageAdapter并设置给Gallery对象
g.setAdapter(new ImageAdapter(this, getSD()));
count_drawble= new ImageAdapter(this, getSD()).getCount();//设置一个itemclickListener并Toast被单击图片的位置
g.setOnItemClickListener(newOnItemClickListener() {public void onItemClick(AdapterView>parent, View v,int position, longid) {
Toast.makeText(ToolsActivity.this,"序列:" + (position + 1) + "\n路径:" + it.get(position),
Toast.LENGTH_LONG).show();
}
});//利用线程来更新 当前欲显示的图片id, 调用handler来选中当前图片
new Thread(newRunnable() {
@Overridepublic voidrun() {//TODO Auto-generated method stub
while(isalive) {
cur_index= cur_index % count_drawble; //图片区间[0,count_drawable)//msg.arg1 = cur_index
Message msg =mhandler.obtainMessage(MSG_UPDATE, cur_index,0);
mhandler.sendMessage(msg);//更新时间间隔为 2s
try{
Thread.sleep(2000);
}catch(InterruptedException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
cur_index++; //放置在Thread.sleep(2000)//;防止mhandler处理消息的同步性,导致cur_index>=count_drawble
}
}
}).start();
}//通过handler来更新主界面//mgallery.setSelection(positon),选中第position的图片,然后调用OnItemSelectedListener监听改变图像
private Handler mhandler = newHandler() {public voidhandleMessage(Message msg) {if (msg.what ==MSG_UPDATE) {
g.setSelection(msg.arg1);//UI Thread直接更改图片 ,不利用Gallery.OnItemSelectedListener监听更改//imgSwitcher.setBackgroundResource(imgAdapter.getResId(msg.arg1));
}
}
};//遍历SD卡中某一路径下指定类型的图片
private ListgetSD() {
it= new ArrayList();
File f= new File(SD_PATH + "//" + "pictures");
File[] files=f.listFiles();for (int i = 0; i < files.length; i++) {
File file=files[i];if(getImageFile(file.getPath()))
it.add(file.getPath());
}returnit;
}//指定遍历文件类型
privateboolean getImageFile(String fName) {
boolean re;
String end=fName
.substring(fName.lastIndexOf(".") + 1, fName.length())
.toLowerCase();if (end.equals("jpg") || end.equals("gif") || end.equals("png")|| end.equals("jpeg") || end.equals("bmp")) {
re= true;
}else{
re= false;
}returnre;
}//改写BaseAdapter自定义一ImageAdapter class
public classImageAdapter extends BaseAdapter {//要是是图片的背景ID
intmGalleryItemBackground;privateContext mContext;private Listlis;public ImageAdapter(Context c, Listli) {
mContext=c;
lis=li;//使用在res/values/aatrs.xml中的定义
TypedArray a =obtainStyledAttributes(R.styleable.Gallery);//取得Gallery属性
mGalleryItemBackground =a.getResourceId(
R.styleable.Gallery_android_galleryItemBackground,0);//让对象的styleable属性能够反复使用
a.recycle();
}//重写的方法,返回图片数目
public intgetCount() {returnlis.size();
}//重写的方法,返回图片的数组id
public Object getItem(intposition) {returnposition;
}public long getItemId(intposition) {returnposition;
}//重写的方法,返回一View对象
public View getView(intposition, View convertView, ViewGroup parent) {//产生ImageView对象
ImageView i = newImageView(mContext);//设置图片给ImageView对象
Bitmap bm = BitmapFactory.decodeFile(lis.get(position).toString());
i.setImageBitmap(bm);//重新设置图片的宽度
i.setScaleType(ImageView.ScaleType.CENTER);//重新设置Layout的宽度
i.setLayoutParams(new Gallery.LayoutParams(1024, 768));//设置Callery背景图
i.setBackgroundResource(mGalleryItemBackground);returni;
}
}
}