1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| package com.pfe.galleryExample;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.pfe.galleryExample.R;
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.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.AdapterView.OnItemClickListener;
public class GalleryExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
/* Find the gallery defined in the main.xml
* Apply a new (custom) ImageAdapter to it. */
Gallery g = (Gallery) findViewById(R.id.gallery) ;
g.setAdapter(new ImageAdapter(this, ReadSDCard()));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View v, int position, long id) {
}
});
}
private List<String> ReadSDCard()
{
List<String> tFileList = new ArrayList<String>();
//It have to be matched with the directory in SDCard
File f = new File("/sdcard");
File[] files=f.listFiles();
for(int i=0; i<files.length; i++)
{
File file = files[i];
/*It's assumed that all file in the path
are in supported type*/
tFileList.add(file.getPath());
}
return tFileList;
}
public class ImageAdapter extends BaseAdapter {
/** The parent context */
private Context myContext;
/** All images to be displayed.
* Put some images to project-folder:
* '/res/drawable/uvw.xyz' .*/
/*private int[] myImageIds = {
R.drawable.image_1,
R.drawable.image_2,
R.drawable.image_3,
R.drawable.image_4
}; */
int mGalleryItemBackground;
//private Context mContext;
private List<String> FileList;
/** Simple Constructor saving the 'parent' context. */
public ImageAdapter(Context c, List<String> fList) {
this.myContext = c;
FileList = fList;
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground,0);
a.recycle();
}
/** Returns the amount of images we have defined. */
//public int getCount() { return this.myImageIds.length; }
public int getCount() { return FileList.size(); }
/* Use the array-Positions as unique IDs */
public Object getItem(int position) { return position; }
public long getItemId(int position) { return position; }
/** Returns a new ImageView to
* be displayed, depending on
* the position passed. */
public View getView(int position, View convertView, ViewGroup parent) {
/*ImageView i = new ImageView(this.myContext);
i.setImageResource(this.myImageIds[position]);
/* Image should be scaled as width/height are set. */
// i.setScaleType(ImageView.ScaleType.FIT_XY);
/* Set the Width/Height of the ImageView. */
/* i.setLayoutParams(new Gallery.LayoutParams(150, 150));
return i; */
ImageView i = new ImageView(myContext);
Bitmap bm = BitmapFactory.decodeFile(
FileList.get(position).toString());
i.setImageBitmap(bm);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
////
public TypedArray obtainStyledAttributes(int theme) {
// TODO Auto-generated method stub
return null;
}
////
/** Returns the size (0.0f to 1.0f) of the views
* depending on the 'offset' to the center. */
public float getScale(boolean focused, int offset) {
/* Formula: 1 / (2 ^ offset) */
return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
}
}
} |
Partager