标签:
知识点
1. 网格视图
2. 读取文件
MainAcitvity
package com.test.gridview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this)); gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show(); } }); } }
ImageAdapter
package com.test.gridview; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import java.util.List; import java.util.Map; /** * Created by 1 on 2016/4/17. */ public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return imagePath.size(); } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } // create a new ImageView for each item referenced by the Adapter public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { // if it‘s not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView; } Bitmap bitmap = BitmapFactory.decodeFile(imagePath.get(position).get(FileUtils.IMAGE_PATH)); imageView.setImageBitmap(bitmap); return imageView; } private List<Map<String, String>> imagePath = FileUtils.getFiles(FileUtils.SDPATH); }
FileUtils
package com.test.gridview; import android.os.Environment; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by 1 on 2016/4/17. */ public class FileUtils { public static String[] imageFormatSet = {"jpg", "png", "gif"}; public static String SDPATH = Environment.getExternalStorageDirectory() + "/00Meeting/"; public static String IMAGE_NAME = "imageName"; public static String IMAGE_PATH = "imagePath"; /** * 判断是否是图片文件 * * @param path * @return */ public static boolean isImageFile(String path) { for (String format : imageFormatSet) { if (path.endsWith(format)) { return true; } } return false; } public static List<Map<String, String>> getFiles(String path) { List<Map<String, String>> pathList = new ArrayList<>(); File file = new File(path); File[] files = file.listFiles(); for (File f : files) { if (f.isDirectory()) { getFiles(f.getAbsolutePath()); } else { if (isImageFile(f.getPath())) { Map<String, String> map = new HashMap<>(); map.put(FileUtils.IMAGE_NAME, f.getName()); map.put(FileUtils.IMAGE_PATH, f.getPath()); pathList.add(map); } } } return pathList; } }
标签:
原文地址:http://www.cnblogs.com/newlangwen/p/5401184.html