最近实现功能是发表图集,多张图片并且给每张图片添加描述,我们最新需求是最多可添加27张图片,实现效果图如下:
图集标题,图集正文,然后就是一个RecycleView,每个item中 图片+描述,点击选择图片,每添加一张图片就是添加一行item。逻辑很简单,下面直接上代码:
先是选择图片可以是图库图片也可以是拍照:
R.layout.layout.popuwindow
<com.zhy.autolayout.AutoLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_view_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mid_transparent_black"
android:gravity="bottom"
android:orientation="vertical"
android:padding="26px">
<com.zhy.autolayout.AutoLinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/popup_button_shape"
android:orientation="vertical">
<TextView
android:id="@+id/tv_pop_album"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="36px"
android:text="@string/xiangcexuanqu"
android:textColor="@color/btn_login_normal"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/divider_list" />
<TextView
android:id="@+id/tv_pop_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1px"
android:gravity="center"
android:padding="36px"
android:text="@string/xiangjipaishe"
android:textColor="@color/btn_login_normal"
android:textStyle="bold" />
</com.zhy.autolayout.AutoLinearLayout>
<TextView
android:id="@+id/tv_pop_cancle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:background="@drawable/popup_button_shape"
android:gravity="center"
android:padding="36px"
android:text="@string/cancel"
android:textColor="@color/gray"
android:textStyle="bold" />
</com.zhy.autolayout.AutoLinearLayout>
//点击添加图片
private void addPic() {
if (imageList.size() >= 27) {
Toast.makeText(ReleaseAtlasActivity.this, "最多上传27张", Toast.LENGTH_SHORT).show();
return;
}
View contentView = LayoutInflater.from(this).inflate(R.layout.layout_popupwindow, null);
View linearViewGroup = contentView.findViewById(R.id.linear_view_group);
View tvCamera = contentView.findViewById(R.id.tv_pop_camera);
View tvAlbum = contentView.findViewById(R.id.tv_pop_album);
View tvCancle = contentView.findViewById(R.id.tv_pop_cancle);
linearViewGroup.setOnClickListener(this);
tvCamera.setOnClickListener(this);
tvAlbum.setOnClickListener(this);
tvCancle.setOnClickListener(this);
mPopPic = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mPopPic.showAtLocation(findViewById(R.id.activity_release), Gravity.BOTTOM, 0, 0);
mPopPic.setOutsideTouchable(true);
mPopPic.setFocusable(false);
}
点击事件实现上传图片或拍照:
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_pop_cancle:
mPopPic.dismiss();
break;
case R.id.tv_pop_album://点击相册
Intent intent = null;
Res.init(this);
if (Bimp.tempSelectBitmap.size() > 0) {
Bimp.tempSelectBitmap.clear();
}
PublicWay.num = 27 - imageList.size();
// startActivityForResult(intent, GET_PHOTO_FROM_ALBUM);
ImageSelectorUtils.openPhoto(this, GET_PHOTO_FROM_ALBUM, false, 27 - imageList.size());
mPopPic.dismiss();
break;
case R.id.tv_pop_camera://相机
mTempTime = System.currentTimeMillis() + "";
String path = getSDCardPath();
// File file = new File(path + "/temp.jpg");
cropFile = new File("/mnt/sdcard/" + mTempTime + ".jpg");//将要保存图片的路径
imageUri = Uri.fromFile(cropFile);
// cropFile = new File(getSDCardPath() + "/" + mTempTime + ".jpg");
// imageCropUri = Uri.fromFile(cropFile);
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, RESULT_CAMERA_ONLY);
mPopPic.dismiss();
break;
}
}
if (requestCode == GET_PHOTO_FROM_ALBUM && data != null) { ArrayList<String> listExtra = data.getStringArrayListExtra(ImageSelectorUtils.SELECT_RESULT); for (int i = 0; i < listExtra.size(); i++) { String tempTime = System.currentTimeMillis() + ""; File file = new File("/mnt/sdcard/" + tempTime + ".jpg");//将要保存图片的路径 try { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); BitmapFactory.decodeFile(listExtra.get(i)).compress(Bitmap.CompressFormat.JPEG, 100, bos); LGImgCompressor.getInstance(this).withListener(this). starCompress(Uri.fromFile(file).toString(), 1200, 3000, 100); // imageList.add(new AtlasBean(file.getAbsolutePath(), "")); bos.flush(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } // myRecyclerAdapter.notifyDataSetChanged(); }
case RESULT_CAMERA_ONLY: LGImgCompressor.getInstance(this).withListener(this). starCompress(Uri.fromFile(cropFile).toString(), 1200, 3000, 100); // imageList.add(new AtlasBean(cropFile.getPath(), "")); // myRecyclerAdapter.notifyDataSetChanged(); break;
这是上传图片的基本完成,其余代码因为涉及到的比较多,代码量等原因,我会选择以压缩包的形式上传。