话不多说先上效果图
image.png首先我们先分析布局: 下面的图片略缩图一看就想出来可以用RecycleView、GridView等等各种控件都可以实现。个人感觉还是用GridView比较简单些。
上代码 先上布局文件
整个xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/select_images"
android:padding="@dimen/sp_10"
android:layout_width="match_parent"
android:background="#FFFFFF"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:textColor="#000000"
android:textSize="@dimen/sp_16"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="图片"/>
<TextView
android:textColor="#000000"
android:textSize="@dimen/sp_16"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="已添加0"/>
<!-- 有个图标要替换下 -->
<TextView
android:textColor="#283643"
android:textSize="@dimen/sp_12"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:drawablePadding="@dimen/sp_10"
android:gravity="center"
android:layout_weight="1"
android:text=""
android:drawableRight="@drawable/more_iocn"
android:layout_height="wrap_content"/>
</LinearLayout>
<!-- 网格显示图片 行列间距5dp 每列宽度90dp -->
<GridView
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:numColumns="5"
android:columnWidth="90dp"
android:gravity="center">
</GridView>
</LinearLayout>
</LinearLayout>
每个item的xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/sp_100"
android:layout_height="@dimen/sp_100">
<ImageView
android:layout_margin="10dp"
android:id="@+id/iv_img"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ImageView
android:id="@+id/iv_delete"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:src="@android:drawable/ic_delete"/>
</RelativeLayout>
MainActivity类
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
GridView gridView1; //网格显示缩略图
LinearLayout selectImages; // 选择图片
private SimpleAdapter simpleAdapter; //适配器
public static final int REQUEST_CODE_SELECTED = 1004; // 打开相册的请求码
private List<HashMap<String, Object>> imageList = new ArrayList<HashMap<String, Object>>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave); // 这里填自己的布局文件名
gridView1 = findViewById(R.id.gridView1);
selectImages = findViewById(R.id.select_images);
// GridView每项右上角删除图片点击事件
gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView deleteImg = view.findViewById(R.id.iv_delete);
deleteImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageList.remove(position);
simpleAdapter.notifyDataSetChanged();
}
});
}
});
selectImages.setOnClickListener(this); // 选择图片点击事件
}
@Override
public void onClick(View v) {
// 动态获取权限
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) { //权限还没有授予,需要在这里写申请权限的代码
// 第二个参数是一个字符串数组,里面是你需要申请的权限 可以设置申请多个权限
// 最后一个参数是标志你这次申请的权限,该常量在onRequestPermissionsResult中使用到
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_SELECTED);
} else { //权限已经被授予,在这里直接写要执行的相应方法即可
choosePhoto();
}
}
private void choosePhoto() {
Intent intentToPickPic = new Intent(Intent.ACTION_PICK, null);
// 如果限制上传到服务器的图片类型时可以直接写如:"image/jpeg 、 image/png等的类型" 所有类型则写 "image/*"
intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/jpeg");
startActivityForResult(intentToPickPic, REQUEST_CODE_SELECTED);
}
//当选择照片完成时会回调到onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
ArrayList<ImageItem> pickedImages = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
switch (requestCode) {
case REQUEST_CODE_SELECTED: {
// 获取图片
try {
//该uri是上一个Activity返回的
Uri imageUri = data.getData();
if (imageUri != null) {
Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
Log.i("bit", String.valueOf(bit));
simpleAdapter = new SimpleAdapter(this, imageList, R.layout.image_grid_item, new String[]{"iv_img"}, new int[]{R.id.iv_img});
simpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
if (view instanceof ImageView && data instanceof Bitmap) {
ImageView i = (ImageView) view;
i.setImageBitmap((Bitmap) data);
}
return true;
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
break;
}
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
喜欢的话点个赞吧!
网友评论