首页 > 其他分享 >图片选择器:ImageSelector

图片选择器:ImageSelector

时间:2023-03-25 23:07:56浏览次数:65  
标签:ImageSelector 选择器 context new path RecyclerView public 图片

简介:

Android自定义相册,实现了拍照、图片选择(单选/多选)、ImageLoader无绑定 任由开发者选择。GitHub网址:https://github.com/YancyYe/GalleryPick

优点:

  • UI重改
  • 所有功能可配置
  • 解决OOM情况
  • 图片多选、单选
  • 支持裁剪功能

示例:演示ImageSelector的简单用法。

第一步:为项目加入Gradle支持:

compile 'com.yancy.imageselector:imageselector:1.3.3'
compile 'com.android.support:design:24.1.1'  //本示例使用RecyclerView展示图片
compile 'com.github.bumptech.glide:glide:3.7.0' //本示例使用Glide加载图片

第二步:在功能清单文件中加入网络访问权限:

<!-- 从sdcard中读取数据的权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- 往sdcard中写入数据的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

第三步:创建 图片加载器 (可以按照喜好使用不同的第三方图片加载框架,本示例程序使用Glide)

public class GlideImageLoader implements ImageLoader {
    @Override
    public void displayImage(Context context, String path, ImageView imageView) {
        Glide.with(context)
                .load(path)
                .placeholder(R.mipmap.imageselector_photo)
                .centerCrop()
                .into(imageView);
    }
}

第四步:首先在主布局文件中提供两个onClick值分别为fun1、fun2的Button控件,依次实现单击选择单张图片和多张图片的功能;然后再在主布局文件中提供一个id值为recycler的RecyclerView的控件用来显示用户最终选择的图片。

第五步:首先为RecyclerView的每一项创建布局文件,将其命名为recyclerview_item.xml的布局文件,然后在该布局文件中放置一个id值为image的ImageView,最后为RecyclerView自定义适配器,代码如下:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private Context context;
    private LayoutInflater mLayoutInflater;
    private List<String> result;
    public MyAdapter(Context context, List<String> result) {
        mLayoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.result = result;
    }
    @Override
    public int getItemCount() {
        return result.size();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLayoutInflater.inflate(R.layout.recyclerview_item, null));
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Glide.with(context)
                .load(result.get(position))
                .centerCrop()
                .into(holder.image);
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
        }
    }
}

第六步:修改MainActivity类的代码如下图所示:

public class MainActivity extends AppCompatActivity {
    public static final int REQUEST_CODE = 1000;
    private MyAdapter adapter;
    private ArrayList<String> path = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recycler = (RecyclerView) super.findViewById(R.id.recycler);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        recycler.setLayoutManager(gridLayoutManager);
        adapter = new MyAdapter(this, path);
        recycler.setAdapter(adapter);
    }
    public void fun1(View view) {//单选
        ImageConfig imageConfig = new ImageConfig.Builder(new GlideImageLoader())
                .steepToolBarColor(getResources().getColor(R.color.blue))
                .titleBgColor(getResources().getColor(R.color.blue))
                .titleSubmitTextColor(getResources().getColor(R.color.white))
                .titleTextColor(getResources().getColor(R.color.white))
                .crop(1, 2, 800, 800)// 截图默认配置:关闭 
                .singleSelect()  // 开启单选   (默认为多选)
                .filePath("/ImageSelector/Pictures") // 拍照后存放的图片路径
                .showCamera() // 开启拍照功能 (默认关闭)
                .requestCode(REQUEST_CODE)
                .build();
        ImageSelector.open(MainActivity.this, imageConfig);   // 开启图片选择器
    }
    public void fun2(View view) {//多选
        ImageConfig imageConfig = new ImageConfig.Builder(new GlideImageLoader())//缓存库
                .steepToolBarColor(getResources().getColor(R.color.blue))// 状态栏颜色 
                .titleBgColor(getResources().getColor(R.color.blue))// 标题的背景颜色 
                .titleSubmitTextColor(Color.WHITE)//提交按钮字体颜色 
                .titleTextColor(getResources().getColor(R.color.white)) // 标题颜色 
                .singleSelect() // 开启多选   (默认为多选)  (单选 为 singleSelect)
                .crop() //// (截图默认配置:关闭    比例 1:1    输出分辨率  500*500)
                .mutiSelectMaxSize(9)  // 多选时的最大数量   (默认 9 张)
                .pathList(path)// 已选择的图片路径
                .filePath("/ImageSelector/Pictures") // 拍照后存放的图片路径
                .showCamera()// 开启拍照功能 (默认开启)
                .requestCode(REQUEST_CODE)
                .build();
        ImageSelector.open(MainActivity.this, imageConfig);   // 开启图片选择器
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) {
            List<String> pathList = 
		data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
            for (String path : pathList)
                System.out.println(path);
            path.clear();
            path.addAll(pathList);
            adapter.notifyDataSetChanged();
        }
    }
}

结果: image.png

标签:ImageSelector,选择器,context,new,path,RecyclerView,public,图片
From: https://blog.51cto.com/lianghecai/6149645

相关文章

  • SpringBoot 将PDF转成图片或World
    SpringBoot是一款非常流行的JavaWeb开发框架,可以用来构建各种Web应用程序。在本篇博客中,我们将介绍如何使用SpringBoot将PDF转换成图片或其他文件的方法。准备工......
  • Asp.net图片文件上传
    1.获取文件的名称和文件的后缀名引用了System.IO,用Path.GetFileNamehe()取得文件名和Path.GetExtension获取文件的后缀2.对上传的文件进行了重命名采用Guid全局......
  • 收集C#常用类:对图片的处理操作
    usingSystem;usingSystem.Collections;usingSystem.IO;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.Drawing.Drawing2D;namespace******{......
  • Bitmap读取本地高分辨率图片报内存不足的解决方案
    1#regiongetThumImage生成缩略图2///<summary>3///生成缩略图4///</summary>5///<paramname="sourceFile">原始图......
  • CSS选择器(包含CSS3新增的伪类和属性选择器等)
    选择器详见https://developer.mozilla.org/zh-CN/docs/Learn/CSS/Building_blocks/SelectorsCSS语法规则由两个主要的部分构成:选择器,以及一条或多条声明(样式)全局选择器......
  • Vue3+element-plus图片上传剪裁组件
    userAvatar.vue<template><divclass="user-info-head"@click="editCropper()"><img:src="options.img"title="点击上传头像"class="img-circleimg-lg"/>......
  • Vue3+element-plus图片上传剪裁组件
    userAvatar.vue<template><divclass="user-info-head"@click="editCropper()"><img:src="options.img"title="点击上传头像"class="img-circleimg-lg"/>......
  • CSS04.选择器
    选择器选择器作用:帮助你精准的选中想要的元素\找到特定的HTML页面元素1.简单选择器1.1ID选择器(JavaScript使用)(不推荐使用)选中的是对应id值的元素id选择器使用#进行标......
  • Python中实现获取所有微信好友的头像并拼接成一张图片
    场景实现扫码登录微信并获取所有好友的昵称以及头像,并将所有头像拼接成一张图片。实现新建文件夹weixinImage文件夹下新建文件weixinImge.py#-*-coding:utf-8-*-fromw......
  • 获取富文本中的图片
    1、获取所有图片路径functionfn(str){vardata=[];str.replace(/<img[^>]*src=['"]([^'"]+)[^>]*>/g,function(match,capture){......