遍历指定目录下的图片文件 显示在gridview
gridview布局
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="90dp" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center" />
item配件布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" > <ImageView android:id="@+id/image" android:layout_width="100dp" android:layout_height="150dp" android:layout_gravity="center" android:scaleType="fitXY" android:padding="4dp" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center_horizontal" /> </LinearLayout>
主Activity
package com.su.testgrid3; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.GridView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class TestGrid3Activity extends Activity { private GridView gridView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String[] titles = FileUtil.getImageNames("/mnt/sdcard/k/"); String[] imagePaths = new String[titles.length]; for (int i = 0; i < titles.length; i++) { imagePaths[i]="/mnt/sdcard/k/"+titles[i]; } gridView = (GridView) findViewById(R.id.gridview); PictureAdapter adapter = new PictureAdapter(titles, imagePaths, this); gridView.setAdapter(adapter); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(TestGrid3Activity.this, "你点击了" + (position + 1), Toast.LENGTH_SHORT).show(); } }); gridView.setOnItemLongClickListener(new OnItemLongClickListener(){ public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(TestGrid3Activity.this, "你长按了" + (position + 1), Toast.LENGTH_SHORT).show(); return true; /* * 这里需要true * 因为:OnItemLongClick事件中:down事件返回值标记此次事 * 件是否为点击事件(返回false,是点击事件;返回true,不记为点击事件), * 而up事件标记此次事件结束时间,也就是判断是否为长按。 */ } }); } }
适配器
// 2012-4-18下午07:46:57 package com.su.testgrid3; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.graphics.BitmapFactory; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; //自定义适配器 class PictureAdapter extends BaseAdapter { private LayoutInflater inflater; private List<Picture> pictures; public PictureAdapter(String[] titles, String[] images, Context context) { super(); pictures = new ArrayList<Picture>(); inflater = LayoutInflater.from(context); for (int i = 0; i < images.length; i++) { Picture picture = new Picture(titles[i], images[i]); pictures.add(picture); } } @Override public int getCount() { if (null != pictures) { return pictures.size(); } else { return 0; } } @Override public Object getItem(int position) { return pictures.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { convertView = inflater.inflate(R.layout.picture_item, null); viewHolder = new ViewHolder(); viewHolder.title = (TextView) convertView.findViewById(R.id.title); viewHolder.image = (ImageView) convertView.findViewById(R.id.image); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.title.setText(pictures.get(position).getTitle()); viewHolder.image.setImageBitmap(BitmapFactory.decodeFile(pictures.get( position).getImageId())); return convertView; } } class ViewHolder { public TextView title; public ImageView image; }
picture的javabean
// 2012-4-18下午07:47:44 package com.su.testgrid3; class Picture { private String title; private String imageId; public Picture() { super(); } public Picture(String title, String imageId) { super(); this.title = title; this.imageId = imageId; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getImageId() { return imageId; } public void setImageId(String imageId) { this.imageId = imageId; } }
获取图片文件的名字数组的操作类
// 2012-4-18下午07:49:44 package com.su.testgrid3; import java.io.File; public class FileUtil { 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; } } }
相关推荐
chenjinlong 2020-01-29
vickyjfr 2013-08-27
luoj 2011-08-17
huohu00 2015-04-22
omguare 2012-05-07
SuperMenyII 2019-06-26
AngelicaA 2011-05-02
AABBbaby 2018-07-27
DrZOU 2017-12-21
wuqiong 2017-04-10
Lzn0 2017-03-27
fengyeezju 2015-12-25
superxlcr 2015-05-27
rookieding 2015-04-22
familynt 2015-04-06
mishaki 2015-02-13
江南以北 2014-12-28