EX07_05.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:4k
- package irdc.ex07_05;
- 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.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 EX07_05 extends Activity
- {
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Gallery g = (Gallery) findViewById(R.id.mygallery);
-
- /*添加一ImageAdapter并设置给Gallery对象*/
- g.setAdapter(new ImageAdapter(this,getSD()));
- /*设置一个itemclickListener事件*/
- g.setOnItemClickListener(new OnItemClickListener()
- {
- public void onItemClick(AdapterView<?> parent,
- View v, int position, long id)
- {
-
- }
- });
- }
-
- private List<String> getSD()
- {
- /* 设置目前所在路径 */
- List<String> it=new ArrayList<String>();
- File f=new File("/sdcard/");
- 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)
- {
- boolean re;
-
- /* 取得扩展名 */
- String end=fName.substring(fName.lastIndexOf(".")+1,
- fName.length()).toLowerCase();
-
- /* 依附档名的类型决定MimeType */
- if(end.equals("jpg")||end.equals("gif")||end.equals("png")
- ||end.equals("jpeg")||end.equals("bmp"))
- {
- re=true;
- }
- else
- {
- re=false;
- }
- return re;
- }
- /*改写BaseAdapter自定义一ImageAdapter class*/
- public class ImageAdapter extends BaseAdapter
- {
- /*声明变量*/
- int mGalleryItemBackground;
- 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 = obtainStyledAttributes(R.styleable.Gallery);
- /*取得Gallery属性的Index id*/
- mGalleryItemBackground = a.getResourceId(
- R.styleable.Gallery_android_galleryItemBackground, 0);
- /*让对象的styleable属性能够反复使用*/
- a.recycle();
- }
-
- /* 覆盖的方法getCount,返回图片数目*/
- public int getCount()
- {
- return lis.size();
- }
-
- /* 覆盖的方法getItem,返回position*/
- public Object getItem(int position)
- {
- return position;
- }
-
- /*一定要覆盖的方法getItemId,返回position*/
- public long getItemId(int position)
- {
- return position;
- }
-
- /* 覆盖的方法getView,返回一View对象*/
- public View getView(int position, View convertView,
- ViewGroup parent)
- {
- /*产生ImageView对象*/
- ImageView i = new ImageView(mContext);
- /*设置图片给imageView对象*/
- Bitmap bm = BitmapFactory.decodeFile(lis.
- get(position).toString());
- i.setImageBitmap(bm);
- /*重新设置图片的宽高*/
- i.setScaleType(ImageView.ScaleType.FIT_XY);
- /*重新设置Layout的宽高*/
- i.setLayoutParams(new Gallery.LayoutParams(136, 88));
- /*设置Gallery背景图*/
- i.setBackgroundResource(mGalleryItemBackground);
- /*返回imageView对象*/
- return i;
- }
- }
- }