Gallery的使用之三

           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 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();
		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 getSD() {

		List it = new ArrayList();

		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 lis;

		public ImageAdapter(Context c, List 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



    

    

     
 

styleable.xml





    


 

 在mainfist.xml

  
    

 

 

 

 

你可能感兴趣的:(Android控件)