要做的事情,就是最多选择三张图片,在用户选择以后右上角会出来一个小X用来删除选中的图片,着里最多就只能选择上传三张。所以就选择了使用直接三个imgview 当上传一张的时候露出第二个添加图片,添加成功以后现在右上角的X 当上传第二张的时候要露出第三个添加图片,添加成功以后现在右上角的X 当点其中任意一个X以后,要自动缩进,这样就涉及到每一个X的处理方式有要稍微有点不一样 // 获得图片 try { assert data != null; imageUri = data.getData(); //该uri就是照片文件夹对应的uri Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); //获取选中图片的base64 //图片转base64 imgBase64 = ImageUtil.imageToBase64(bit); //如果list里面不存在图片就最加,如果已经存在了就替换 if(baseList.size()<1){ baseList.add(imgBase64); }else { baseList.set(0,imgBase64); } vBinding.iv1.setImageBitmap(bit); vBinding.ivDelete1.setVisibility(View.VISIBLE); vBinding.iv2.setVisibility(View.VISIBLE); } catch (Exception e) { e.printStackTrace(); }
上面是从相册或者相机获取图片后的处理方法,都保存在了baseList里面,第二个和第三个和这个一样的,把判断修改一下就好
点击图片右上角的X的时候的处理,需要按照下标删掉图片
/** * 在图片删除以后自定补位 * @param baseList */ private void imgInit(List baseList){ if(!UtilWant.isNull(baseList)){ switch (baseList.size()){ case 0: vBinding.iv2.setVisibility(View.GONE); vBinding.iv3.setVisibility(View.GONE); vBinding.iv1.setVisibility(View.VISIBLE); vBinding.iv1.setImageResource(R.mipmap.icon_add_img);
vBinding.ivDelete1.setVisibility(View.GONE);
vBinding.ivDelete2.setVisibility(View.GONE);
vBinding.ivDelete3.setVisibility(View.GONE);
break;
case 1:
vBinding.iv1.setImageBitmap(ImageUtil.stringToBitmap(baseList.get(0)));
vBinding.iv2.setImageResource(R.mipmap.icon_add_img);
vBinding.iv2.setVisibility(View.VISIBLE);
vBinding.iv3.setVisibility(View.GONE);
vBinding.ivDelete2.setVisibility(View.GONE);
vBinding.ivDelete3.setVisibility(View.GONE);
break;
case 2:
vBinding.iv1.setImageBitmap(ImageUtil.stringToBitmap(baseList.get(0)));
vBinding.iv2.setImageBitmap(ImageUtil.stringToBitmap(baseList.get(1)));
vBinding.iv3.setImageResource(R.mipmap.icon_add_img);
vBinding.iv3.setVisibility(View.VISIBLE);
vBinding.iv1.setVisibility(View.VISIBLE);
vBinding.iv2.setVisibility(View.VISIBLE);
vBinding.ivDelete1.setVisibility(View.VISIBLE);
vBinding.ivDelete2.setVisibility(View.VISIBLE);
vBinding.ivDelete3.setVisibility(View.GONE);
break;
case 3:
break;
}
}else {
vBinding.iv2.setVisibility(View.GONE);
vBinding.iv3.setVisibility(View.GONE);
vBinding.iv1.setVisibility(View.VISIBLE);
vBinding.iv1.setImageResource(R.mipmap.icon_add_img);
vBinding.ivDelete1.setVisibility(View.GONE);
vBinding.ivDelete2.setVisibility(View.GONE);
vBinding.ivDelete3.setVisibility(View.GONE);
}
}
好了完事了,然后把baseList传给后台就可以了 代码写之前多想,想明白了写起来就胸有成竹了!
加油打工人!!!
|