关键代码一个adapter

package com.su.houses.utils;import android.content.Context;import android.content.res.TypedArray;import android.net.Uri;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import com.su.houses.R;public class ImageAdapter extends BaseAdapter {private Context mContext;public String picpath;private String[] myImageNames;public ImageAdapter(Context c, String path) {mContext = c;picpath = path;myImageNames = FileUtil.getImageNames(picpath);}public int getCount() {return myImageNames.length;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn position;}public long getItemId(int position) {// TODO Auto-generated method stubreturn position;}public View getView(int position, View convertView, ViewGroup parent) {ImageView imageView = new ImageView(mContext);imageView.setImageURI(Uri.parse(picpath + "/" + myImageNames[position]));imageView.setLayoutParams(new Gallery.LayoutParams(800, 600));imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);return imageView;}}

然后其中的一些方法

public static String[] getImageNames(String folderPath) {File file01 = new File(folderPath);String[] files01 = file01.list();int imageFileNums = 0;for (int i = 0; i < files01.length; i++) {File file02 = new File(folderPath + "/" + files01[i]);if (!file02.isDirectory()) {if (isImageFile(file02.getName())) {imageFileNums++;}}}String[] files02 = new String[imageFileNums];int j = 0;for (int i = 0; i < files01.length; i++) {File file02 = new File(folderPath + "/" + files01[i]);if (!file02.isDirectory()) {if (isImageFile(file02.getName())) {files02[j] = file02.getName();j++;}}}return files02;}private static boolean isImageFile(String fileName) {String fileEnd = fileName.substring(fileName.lastIndexOf(".") + 1,fileName.length());if (fileEnd.equalsIgnoreCase("jpg")) {return true;} else if (fileEnd.equalsIgnoreCase("png")) {return true;} else if (fileEnd.equalsIgnoreCase("bmp")) {return true;} else {return false;}}
然后在实例化adapter在界面的gallery显示

Gallery g = (Gallery) findViewById(R.id.house_gallery);g.setAdapter(new ImageAdapter(this,"/mnt/sdcard/k/a/"));
也可以添加listener

g.setOnItemSelectedListener(new OnItemSelectedListener(){// 选择图片的事件public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id){// 获取当前图片文件名currentImageName = myImageNames[position];// 获取当前图片路径currentImagePath = currentFolderPath + "/" + currentImageName;// 设置imageSwitcher的显示图片   }public void onNothingSelected(AdapterView<?> arg0){// TODO Auto-generated method stub}

更多相关文章

  1. android 判断 网络 类型
  2. android 单击 切换图片 --- 注意图片大小
  3. [置顶] Android(安卓)通过经纬度获取地理位置信息
  4. android设置一个图片为全屏大小
  5. Android(安卓)使用decodeFile方法加载手机磁盘中的图片文件
  6. android 生成圆角和带倒影图片
  7. 编程回忆之Android回忆(Android应用参数的获取)
  8. android获取系统当前年月日时分秒的时间
  9. android中调用相册里面的图片并返回

随机推荐

  1. android获取gps坐标
  2. android studio3.5.2打包,解决建议使用 "k
  3. [Android Pro] Android fastboot刷机和获
  4. android Theme使用四
  5. 仿照利用android系统源码资源文件,修改See
  6. Android 邮件发送(一键发送, 163邮箱)
  7. Android 文件读写 + sdcard + 文件的属性
  8. Android 静默安装
  9. android-单元测试(Android JUnit Test)
  10. Android系统权限和root权限