本文实例为大家分享了AndroID实现图片选择器功能的具体代码,供大家参考,具体内容如下
图片选择功能用的是GitHub上的依赖库,网址
先来看下我运行的效果图如下所示:
该依赖库是AndroID平台上拍照/录像,图片/视频选择,编辑和压缩的一站式解决方案。
添加依赖,在app->build.gradle里面添加依赖
//图片/视频选择、预览、编辑与拍照implementation 'com.github.guoxiaoxing:phoenix:1.0.15'
初始化:
public class App extends Application { @OverrIDe public voID onCreate() { super.onCreate(); Phoenix.config() .imageLoader(new ImageLoader() { @OverrIDe public voID loadImage(Context mContext,ImageVIEw imageVIEw,String imagePath,int type) { GlIDe.with(mContext) .load(imagePath) .into(imageVIEw); } }); }}
开启功能:
private voID callUpSelecter(boolean isCamera,int type,int REQUEST_CODE) { if (TextUtils.isEmpty(fileID)) { Phoenix.with() .theme(PhoenixOption.theme_RED)// 主题 .fileType(MimeType.ofImage())//显示的文件类型图片、视频、图片和视频 .maxPickNumber(9)// 最大选择数量 .minPickNumber(0)// 最小选择数量 .spanCount(4)// 每行显示个数 .enablePrevIEw(true)// 是否开启预览 .enableCamera(isCamera)// 是否开启拍照 .enableAnimation(false)// 选择界面图片点击效果 .enableCompress(true)// 是否开启压缩 .compresspictureFilterSize(300)//多少kb以下的图片不压缩 .compressVIDeoFilterSize(2018)//多少kb以下的视频不压缩 .thumbnailHeight(160)// 选择界面图片高度 .thumbnailWIDth(160)// 选择界面图片宽度 .enableClickSound(false)// 是否开启点击声音 .pickedMediaList(add_Lists)// 已选图片数据 .vIDeoFilterTime(0)//显示多少秒以内的视频 .mediaFilterSize(0)//显示多少kb以下的图片/视频,默认为0,表示不限制 .start(UploadMorePicActivity.this,type,REQUEST_CODE); } }
获取结果:
@OverrIDe protected voID onActivityResult(int requestCode,int resultCode,Intent data) { super.onActivityResult(requestCode,resultCode,data); if (requestCode == 889 && resultCode == RESulT_OK) {//相册 //返回的数据 List<MediaEntity> result = Phoenix.result(data); Log.e("=====result==",result.toString()); add_Lists.clear(); add_Lists.addAll(result); adapter.updateItems(add_Lists); btnOkEnable(add_Lists); } else if (requestCode == 899 && resultCode == RESulT_OK) {//拍照 List<MediaEntity> result = Phoenix.result(data); add_Lists.addAll(result); adapter.updateItems(add_Lists); btnOkEnable(add_Lists); } }
recyclerVIEw的适配基类:
/** * recyclerVIEw适配器基类 */public abstract class BaseRecyclerAdapter<T> extends RecyclerVIEw.Adapter<RecyclerVIEw.VIEwHolder> { public OnRecyclerVIEwItemClickListener mOnItemClickListener = null; public OnRecyclerVIEwItemClickListenerData ListenerData; /** * 数据集合 */ protected List<T> mItems; protected Context mContext; /** * 构造函数 */ public BaseRecyclerAdapter(Context context) { this.mContext = context; mItems = new ArrayList<>(); } /** * 返回数据集 */ public List<T> getList() { return mItems; } /** * 添加单条数据 */ public voID addItem(T item) { if (item == null) return; mItems.add(mItems.size(),item); notifyItemInserted(mItems.size()); } /** * 追加数据集合 */ public voID addItems(List<T> items) { if (items == null) return; this.mItems.addAll(items); notifyDataSetChanged(); } public boolean containsAll(List<T> items) { return mItems.containsAll(items); } /** * 更新指定行数据 */ public voID updateItem(T tasks,int position) { if (tasks == null) return; mItems.set(position,tasks); notifyItemChanged(position); } /** * 更新全部数据 */ public voID updateItems(List<T> items) { if (items == null) return; this.mItems.clear(); this.mItems.addAll(items); notifyDataSetChanged(); } /** * 移除指定行数据 */ public voID removeItem(int index) { mItems.remove(index); notifyItemRemoved(index); notifyItemRangeChanged((index - 1) >= 0 ? index - 1 : 0,mItems.size()); } public voID getVIEw(int position,RecyclerVIEw.VIEwHolder vIEwHolder,T item) { } /** * 返回指定行数据 */ public T getItem(int location) { if (mItems == null || mItems.isEmpty()) { return null; } else { return mItems.get(location); } } /** * 返回数据集合总数 */ @OverrIDe public int getItemCount() { return mItems == null ? 0 : mItems.size(); } /** * 清除全部数据 */ public voID clear() { mItems.clear(); notifyDataSetChanged(); } public static interface OnRecyclerVIEwItemClickListener { voID onItemClick(VIEw vIEw,String data); } public voID setonItemClickListener(OnRecyclerVIEwItemClickListener Listener) { this.mOnItemClickListener = Listener; } public interface OnRecyclerVIEwItemClickListenerData<T> { voID onItemClickData(VIEw vIEw,T data); } public voID setonItemClickListenerData(OnRecyclerVIEwItemClickListenerData Listener) { ListenerData = Listener; }}
显示选择的图片的适配器:
/** * Created by YuShuangPing on 2018/7/16. */ public class UpLoadMorePicAdapter extends BaseRecyclerAdapter<MediaEntity> { /** * 构造函数 * * @param context */ public UpLoadMorePicAdapter(Context context) { super(context); } @OverrIDe public RecyclerVIEw.VIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) { VIEw vIEw= LayoutInflater.from(mContext).inflate(R.layout.item_upload_more_pic_refresh,parent,false); return new VIEwHolder(vIEw); } @OverrIDe public voID onBindVIEwHolder(RecyclerVIEw.VIEwHolder holder,final int position) { if (holder instanceof VIEwHolder){ final VIEwHolder vIEwHolder= (VIEwHolder) holder; if(mItems.size()<9&&position==mItems.size()){ GlIDe.with(mContext).load(R.mipmap.add_consumer).centerCrop().into(vIEwHolder.iv_pic); }else{ final MediaEntity mediaEntity=mItems.get(position); GlIDe.with(mContext).load(mediaEntity.getLocalPath()).centerCrop().into(vIEwHolder.iv_pic); } vIEwHolder.iv_pic.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if (ListenerData!=null){ ListenerData.onItemClickData(vIEwHolder.iv_pic,position); } } }); } } @OverrIDe public int getItemCount() { return mItems.size()<9? mItems.size()+1:9; } class VIEwHolder extends RecyclerVIEw.VIEwHolder{ private CustomHeightimageVIEw iv_pic; public VIEwHolder(VIEw vIEw) { super(vIEw); iv_pic= (CustomHeightimageVIEw) vIEw.findVIEwByID(R.ID.iv_pic); } }}
适配器布局文件:
R.layout.item_upload_more_pic_refresh
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal"> <com.sinosig.ygqd.Widget.CustomHeightimageVIEw androID:ID="@+ID/iv_pic" androID:layout_wIDth="230px" androID:layout_height="230px" androID:layout_marginBottom="28.8px" app:scale="1" /></linearLayout>
Activity中的代码:
public class UploadMorePicActivity extends AppCompatActivity{ private RecyclerVIEw rl_pic; private UpLoadMorePicAdapter adapter; private List<MediaEntity> add_Lists = new ArrayList<>(); @OverrIDe protected voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); rl_pic = (RecyclerVIEw) findVIEwByID(R.ID.rl_pic); adapter = new UpLoadMorePicAdapter(this); rl_pic.setLayoutManager(new GrIDLayoutManager(this,4)); rl_pic.setAdapter(adapter); adapter.updateItems(add_Lists); adapter.setonItemClickListenerData(new BaseRecyclerAdapter.OnRecyclerVIEwItemClickListenerData() { @OverrIDe public voID onItemClickData(VIEw vIEw,Object data) { int position = (int) data; if (position >= add_Lists.size()) { callUpSelecter(false,PhoenixOption.TYPE_PICK_MEDIA,889); } else { } } });}private voID callUpSelecter(boolean isCamera,REQUEST_CODE); } } @OverrIDe protected voID onActivityResult(int requestCode,result.toString()); add_Lists.clear(); add_Lists.addAll(result); adapter.updateItems(add_Lists); btnOkEnable(add_Lists); } }
自定义ImageVIEw:
public class CustomHeightimageVIEw extends androID.support.v7.Widget.AppCompatimageVIEw { float scale = 0; public CustomHeightimageVIEw(Context context) { super(context); } public CustomHeightimageVIEw(Context context,float scale) { super(context); this.scale = scale; } public CustomHeightimageVIEw(Context context,AttributeSet attrs) { super(context,attrs); TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.CustomHeightrelativeLayout); scale = a.getfloat(R.styleable.CustomHeightrelativeLayout_scale,-1f);// float scale = a. } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { if (scale == -1) { super.onMeasure(wIDthMeasureSpec,heightmeasureSpec); } else { super.onMeasure(wIDthMeasureSpec,MeasureSpec.makeMeasureSpec( (int) (MeasureSpec.getSize(wIDthMeasureSpec) * (scale)),MeasureSpec.getMode(wIDthMeasureSpec))); } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
您可能感兴趣的文章:Android自定义图片选择器简单版Android实现微信的图片选择器Android仿微信图片选择器ImageSelector使用详解Android仿微信朋友圈图片选择器Android图片选择器ImageEditContainerAndroid仿微信图片选择器Android 高仿QQ图片选择器Android图片选择器 丰富的配置选项浅谈谈Android 图片选择器 总结以上是内存溢出为你收集整理的Android实现图片选择器功能全部内容,希望文章能够帮你解决Android实现图片选择器功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)