第一步:引用GridView控件
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"/>
android:columnWidth:指定每列宽度
android:horizontalSpacing="10dp":水平方向的间距(列间距)
android:verticalSpacing="10dp":垂直方向的间距(行间距)
android:numColumns="auto_fit":指定每行有多少列,auto_fit表示根据屏幕自适应,也可以指定具体指,1、2.....
android:stretchMode:剩余空间拉伸模式,有四种选择none、columnWidth、spacingWidth、spacingWidthUniform
none:不拉伸
columnWidth:若有剩余空间,则拉伸列框挤掉空隙
spacingWidth:若有剩余控件,则列宽不变,把空间分配到每列间的空隙
spacingWidthUniform:若有剩余控件,则列宽不变,把空间分配到每列左右的空隙
第二步:读取内置SD卡图片文件夹内容
创建工具类FileUtil ,用来读取照片名称
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;
}
}
}
//返回SD路径
public class ConfigConstant {
public static String getSdDir(){
// 判断是否有内置SD卡
String sdDir = null;
boolean isSDcardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if (isSDcardExist) {
sdDir = Environment.getExternalStorageDirectory().getAbsolutePath();
} else {
sdDir = Environment.getRootDirectory().getAbsolutePath();
}
return sdDir + "/" ;
}
}
Activity调用
/**
* 无人机图片展示页面
*/
public class WuRenJiActivity extends BaseActivity {
@BindView(R.id.go_back)
LinearLayout goBack;//返回按钮
@BindView(R.id.toolbar_title)
TextView title;//标题设置
@BindView(R.id.grid_view)
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vehicle);
ButterKnife.bind(this);
title.setText("图片展示");
initView();
}
public void initView() {
String dir = ConfigConstant.getSdDir() + "dc/photo/";//照片所放的路径
String[] titles = FileUtil.getImageNames(dir);
String[] imagePaths = new String[titles.length];
for (int i = 0; i < titles.length; i++) {
imagePaths[i] = dir + titles[i];
}
gridView.setAdapter(new PictureAdapter(titles,imagePaths,this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("你点击了", "onItemClick: " + position);
}
});
}
@OnClick(R.id.go_back)
public void onClik(View v) {
finish();
}
}
适配器中图片展示代码调用
//把路径转换为bitmap显示
viewHolder.image.setImageBitmap(BitmapFactory.decodeFile(pictures.get(position).getImages()));
item_photo布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:padding="4dp"
android:scaleType="fitXY" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal" />
</LinearLayout>