Gallery实现从指定目录读取文件 画廊似展示图片
GalleryPictureShowActivity
package com.lenovo.halo.gallery;
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.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery.LayoutParams;
public class GalleryPictureShowActivity extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
private static final String TAG = "MyLauncher";
private List<String> ImageList;
private String[] list;
private ImageSwitcher mSwitcher;
int current = 0;
String photoURL;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// //去掉状态栏,且显示自己程序名称
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.pictureshow);
ImageList = new ArrayList<String>();
ImageList = getSD();
list = ImageList.toArray(new String[ImageList.size()]);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
mSwitcher.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Toast.makeText(BgPictureShowActivity.this, "点击了",
// 1).show();
}
});
Gallery g = (Gallery) findViewById(R.id.mygallery);
g.setAdapter(new ImageAdapter(this, getSD()));
g.setOnItemSelectedListener(this);
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(GalleryPictureShowActivity.this, position+"",
1).show();
}
});
}
private List<String> getSD() {
List<String> it = new ArrayList<String>();
String path = Environment.getExternalStorageDirectory() + "";
Log.e("path", "+++++++++++" + path);
File f = new File(path);
// File f = new File("/sdcard/");
File[] files = f.listFiles();
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) {
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;
}
return re;
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private List<String> lis;
public ImageAdapter(Context c, List<String> li) {
mContext = c;
lis = li;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return lis.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
Bitmap bm = BitmapFactory.decodeFile(lis.get(position).toString());
i.setImageBitmap(bm);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(136, 88));
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
photoURL = list[position];
mSwitcher.setImageURI(Uri.parse(photoURL));
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return i;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
ImageView i = new ImageView(this);
if (event.getAction() == KeyEvent.ACTION_UP) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
current++;
if (current == ImageList.size()) {
current = 0;
}
mSwitcher.setImageURI(Uri.parse(photoURL));
default:
break;
}
}
return super.onKeyDown(keyCode, event);
}
}
定义的XML
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#55000000"
android:orientation="vertical"
tools:context="com.lenovo.halo.gallery.MainActivity" >
<ImageSwitcher
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="350dip"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<Gallery
android:id="@+id/mygallery"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:spacing="16dp" />
</RelativeLayout>
styleable.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery" >
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
在mainfist.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />