1.导入
compile 'com.github.LuckSiege.PictureSelector:picture_library:v2.2.3'
2.适配器代码
package com.sunnet.shipcargo.adapter;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.DateUtils;
import com.luck.picture.lib.tools.StringUtils;
import com.sunnet.shipcargo.R;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* author:luck
* project:PictureSelector
* package:com.luck.pictureselector.adapter
* email:893855882@qq.com
* data:16/7/27
*/
public class GridImageAdapter extends
RecyclerView.Adapter<GridImageAdapter.ViewHolder> {
public static final int TYPE_CAMERA = 1;
public static final int TYPE_PICTURE = 2;
private LayoutInflater mInflater;
private List<LocalMedia> list = new ArrayList<>();
private int selectMax = 9;
private Context context;
/**
* 点击添加图片跳转
*/
private onAddPicClickListener mOnAddPicClickListener;
public interface onAddPicClickListener {
void onAddPicClick();
}
public GridImageAdapter(Context context, onAddPicClickListener mOnAddPicClickListener) {
this.context = context;
mInflater = LayoutInflater.from(context);
this.mOnAddPicClickListener = mOnAddPicClickListener;
}
public void setSelectMax(int selectMax) {
this.selectMax = selectMax;
}
public void setList(List<LocalMedia> list) {
this.list = list;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView mImg;
LinearLayout ll_del;
TextView tv_duration;
public ViewHolder(View view) {
super(view);
mImg = (ImageView) view.findViewById(R.id.fiv);
ll_del = (LinearLayout) view.findViewById(R.id.ll_del);
tv_duration = (TextView) view.findViewById(R.id.tv_duration);
}
}
@Override
public int getItemCount() {
if (list.size() < selectMax) {
return list.size() + 1;
} else {
return list.size();
}
}
@Override
public int getItemViewType(int position) {
if (isShowAddItem(position)) {
return TYPE_CAMERA;
} else {
return TYPE_PICTURE;
}
}
/**
* 创建ViewHolder
*/
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = mInflater.inflate(R.layout.adapter_image_select,
viewGroup, false);
final ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
private boolean isShowAddItem(int position) {
int size = list.size() == 0 ? 0 : list.size();
return position == size;
}
/**
* 设置值
*/
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
//少于8张,显示继续添加的图标
if (getItemViewType(position) == TYPE_CAMERA) {
viewHolder.mImg.setImageResource(R.mipmap.img_select_image_add);
viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnAddPicClickListener.onAddPicClick();
}
});
viewHolder.ll_del.setVisibility(View.INVISIBLE);
} else {
viewHolder.ll_del.setVisibility(View.VISIBLE);
viewHolder.ll_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int index = viewHolder.getAdapterPosition();
// 这里有时会返回-1造成数据下标越界,具体可参考getAdapterPosition()源码,
// 通过源码分析应该是bindViewHolder()暂未绘制完成导致,知道原因的也可联系我~感谢
if (index != RecyclerView.NO_POSITION) {
list.remove(index);
notifyItemRemoved(index);
notifyItemRangeChanged(index, list.size());
}
}
});
LocalMedia media = list.get(position);
int mimeType = media.getMimeType();
String path = "";
if (media.isCut() && !media.isCompressed()) {
// 裁剪过
path = media.getCutPath();
} else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
// 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
path = media.getCompressPath();
} else {
// 原图
path = media.getPath();
}
// 图片
if (media.isCompressed()) {
Log.i("compress image result:", new File(media.getCompressPath()).length() / 1024 + "k");
Log.i("压缩地址::", media.getCompressPath());
}
Log.i("原图地址::", media.getPath());
int pictureType = PictureMimeType.isPictureType(media.getPictureType());
if (media.isCut()) {
Log.i("裁剪地址::", media.getCutPath());
}
long duration = media.getDuration();
viewHolder.tv_duration.setVisibility(pictureType == PictureConfig.TYPE_VIDEO
? View.VISIBLE : View.GONE);
if (mimeType == PictureMimeType.ofAudio()) {
viewHolder.tv_duration.setVisibility(View.VISIBLE);
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.picture_audio);
StringUtils.modifyTextViewDrawable(viewHolder.tv_duration, drawable, 0);
} else {
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.video_icon);
StringUtils.modifyTextViewDrawable(viewHolder.tv_duration, drawable, 0);
}
viewHolder.tv_duration.setText(DateUtils.timeParse(duration));
if (mimeType == PictureMimeType.ofAudio()) {
viewHolder.mImg.setImageResource(R.drawable.audio_placeholder);
} else {
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.color.color_f6)
.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(viewHolder.itemView.getContext())
.load(path)
.apply(options)
.into(viewHolder.mImg);
}
//itemView 的点击事件
if (mItemClickListener != null) {
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int adapterPosition = viewHolder.getAdapterPosition();
mItemClickListener.onItemClick(adapterPosition, v);
}
});
}
}
}
protected OnItemClickListener mItemClickListener;
public interface OnItemClickListener {
void onItemClick(int position, View v);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mItemClickListener = listener;
}
}
3.Avticity代码
package com.sunnet.shipcargo.activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.sunnet.shipcargo.R
import com.sunnet.shipcargo.adapter.GridImageAdapter
import com.sunnet.shipcargo.view.FullyGridLayoutManager
import kotlinx.android.synthetic.main.activity_add_goods.*
import java.util.ArrayList
class AddGoodsActivity : BaseActivity() {
override fun getContentView(): Int = R.layout.activity_add_goods
private var selectImage:List<LocalMedia> = ArrayList()
private var adapterImage: GridImageAdapter?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setTitle("发布商品")
rcy_add_goods.layoutManager = FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false)
adapterImage = GridImageAdapter(this, onAddPicClickListener)
adapterImage?.setList(selectImage)
adapterImage?.setSelectMax(5)
rcy_add_goods.adapter = adapterImage
adapterImage?.setOnItemClickListener { position, v ->
if (selectImage.isNotEmpty()) {
val media = selectImage[position]
val pictureType = media.pictureType
val mediaType = PictureMimeType.pictureToVideo(pictureType)
when (mediaType) {
1->{
// 预览图片 可自定长按保存路径
PictureSelector.create(this@AddGoodsActivity).themeStyle(R.style.picture_default_style).openExternalPreview(position, selectImage)
}
2->{
// 预览视频
PictureSelector.create(this@AddGoodsActivity).externalPictureVideo(media.path)
}
3->{
// 预览音频
PictureSelector.create(this@AddGoodsActivity).externalPictureAudio(media.path)
}
}
}
}
btn_add_ship_submit.setOnClickListener { getSubmit() }
}
fun getSubmit() = when{
txt_add_goods_type.text.toString().isEmpty()->toast("请选择货物种类")
txt_add_goods_name.text.toString().isEmpty()->toast("请填写货物名称")
txt_add_goods_norms.text.toString().isEmpty()->toast("请填写货物规则")
selectImage.isEmpty() ->toast("请上传货物图片")
txt_add_goods_remark.text.toString().isEmpty()->toast("请填写商品描述")
else->{
loading.show()
val map=HashMap<String,String>()
map["store_id"]=""
map["goods_type"]=""
map["goods_name"]=txt_add_goods_name.text.toString()
map["goods_standard"]=txt_add_goods_norms.text.toString()
map["goods_picture"]=""
map["goods_price"]=if (txt_add_goods_price.text.toString().isEmpty()) "面谈" else txt_add_goods_price.text.toString()
map["goods_describe"]=txt_add_goods_remark.text.toString()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
when (requestCode) {
PictureConfig.CHOOSE_REQUEST->{
// 图片选择结果回调
selectImage = PictureSelector.obtainMultipleResult(data)
// 例如 LocalMedia 里面返回三种path
// 1.media.getPath(); 为原图path
// 2.media.getCutPath();为裁剪后path,需判断media.isCut();是否为true
// 3.media.getCompressPath();为压缩后path,需判断media.isCompressed();是否为true
// 如果裁剪并压缩了,已取压缩路径为准,因为是先裁剪后压缩的
adapterImage?.setList(selectImage)
adapterImage?.notifyDataSetChanged()
}
}
}
}
private val onAddPicClickListener= GridImageAdapter.onAddPicClickListener {
// 进入相册 以下是例子:不需要的api可以不写
PictureSelector.create(this)
.openGallery(PictureMimeType.ofImage())// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
.maxSelectNum(5)// 最大图片选择数量
.minSelectNum(1)// 最小选择数量
.imageSpanCount(4)// 每行显示个数
.selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选
.previewImage(true)// 是否可预览图片
.isCamera(true)// 是否显示拍照按钮
.isZoomAnim(true)// 图片列表点击 缩放效果 默认true
.enableCrop(true)// 是否裁剪
.synOrAsy(true)//同步true或异步false 压缩 默认同步
.glideOverride(160, 160)// glide 加载宽高,越小图片列表越流畅,但会影响列表图片浏览的清晰度
.withAspectRatio(1,1)// 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
.hideBottomControls(true)// 是否显示uCrop工具栏,默认不显示
.freeStyleCropEnabled(true)// 裁剪框是否可拖拽
.showCropGrid(true)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false
.openClickSound(false)// 是否开启点击声音
.selectionMedia(selectImage)// 是否传入已选图片
//.isDragFrame(false)// 是否可拖动裁剪框(固定)
// .videoMaxSecond(15)
// .videoMinSecond(10)
//.previewEggs(false)// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中)
//.cropCompressQuality(90)// 裁剪压缩质量 默认100
.minimumCompressSize(100)// 小于100kb的图片不压缩
//.cropWH()// 裁剪宽高比,设置如果大于图片本身宽高则无效
//.rotateEnabled(true) // 裁剪是否可旋转图片
//.scaleEnabled(true)// 裁剪是否可放大缩小图片
//.videoQuality()// 视频录制质量 0 or 1
//.videoSecond()//显示多少秒以内的视频or音频也可适用
//.recordVideoSecond()//录制视频秒数 默认60s
.forResult(PictureConfig.CHOOSE_REQUEST)//结果回调onActivityResult code
}
}
4.xml界面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sunnet.shipcargo.activity.AddGoodsActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp">
<TextView
android:id="@+id/txt_add_goods_type_title"
android:layout_width="80dp"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:text="种 类"
android:textColor="#0F0F0F" />
<TextView
android:id="@+id/txt_add_goods_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:background="@null"
android:hint="选择货物种类"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/txt_add_goods_type_title" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#ECECEC"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp">
<TextView
android:id="@+id/txt_add_goods_name_title"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="货 名"
android:textColor="#0F0F0F" />
<TextView
android:id="@+id/txt_add_goods_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:background="@null"
android:hint="填写货名"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/txt_add_goods_name_title" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#ECECEC"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp">
<TextView
android:id="@+id/txt_add_goods_norms_title"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="规 格"
android:textColor="#0F0F0F" />
<TextView
android:id="@+id/txt_add_goods_norms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:background="@null"
android:hint="填写规格"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/txt_add_goods_norms_title" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#ECECEC"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rcy_add_goods"
android:layout_margin="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#ECECEC"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp">
<TextView
android:id="@+id/txt_add_goods_price_title"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="价 格"
android:textColor="#0F0F0F" />
<TextView
android:id="@+id/txt_add_goods_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:background="@null"
android:hint="填写价格,不填默认为面谈"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/txt_add_goods_price_title" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#ECECEC"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp">
<TextView
android:id="@+id/txt_add_goods_remark_title"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="描 述"
android:textColor="#0F0F0F" />
<TextView
android:id="@+id/txt_add_goods_remark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14dp"
android:background="@null"
android:hint="填写详情描述"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/txt_add_goods_remark_title" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#ECECEC"/>
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/btn_add_ship_submit"
android:layout_width="match_parent"
android:layout_height="42dp"
android:textColor="@color/white"
android:layout_alignParentBottom="true"
android:text="发布"
android:textSize="16dp"
android:layout_margin="8dp"
android:background="@drawable/bg_circle_2_solid_lan"/>
</RelativeLayout>
网友评论