知乎图片选择框架Matisse的完整使用

本文详细介绍了Matisse图片选择框架的使用方法,包括依赖配置、代码调用及自定义图片加载引擎,展示了如何设置图片筛选条件,并通过Glide进行图片加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.youkuaiyun.com/qq_36043263/article/details/81707029

推荐:Android——实现点击校验登陆跳转(非侵入式)

Matisse为我们提供了一个非常稳定了图片视频选择框架

github地址

依赖:
compile 'com.zhihu.android:matisse:0.5.2-beta2’
implementation 'com.github.bumptech.glide:glide:4.7.1’
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1’

最基本的两个权限:

android.permission.READ_EXTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE

代码调用:

Matisse.from(this).choose(MimeType.ofImage(), false)
                .countable(true)
                .maxSelectable(1)
                .addFilter(new Filter() {
                    @Override
                    protected Set<MimeType> constraintTypes() {
                        return new HashSet<MimeType>() {{
                            add(MimeType.PNG);
                        }};
                    }
                @Override
                public IncapableCause filter(Context context, Item item) {
                    try {
                        InputStream inputStream = getContentResolver().openInputStream(item.getContentUri());
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = true;
                        BitmapFactory.decodeStream(inputStream, null, options);
                        int width = options.outWidth;
                        int height = options.outHeight;

                        if (width &gt;= 500)
                            return new IncapableCause("宽度超过500px");

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }


                    return null;
                }
            })
            .gridExpectedSize((int) getResources().getDimension(R.dimen.imageSelectDimen))
            .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
            .thumbnailScale(0.87f)
            .imageEngine(new GlideLoadEngine())
            .forResult(1);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

choose是选择的内容,countable()是否显示选中数量,maxSelectable()最大选择数,addFilter()添加一个过滤器,是在我们选择的类型上进一步过滤。gridExpectedSize()缩略图展示的大小,thumbnailScale(0.87f)缩略图的清晰程度(与内存占用有关)。imageEngine()是我们自定义加载图片框架。

Filter接口有两个方法,第一个方法返回需要过滤的数据类型,第二个方法决定是否过滤,过滤的话就return new IncapableCause(“宽度超过500px”); 填入过滤的原因即可。 在上述中我们过滤了宽度大于500的图片。

接收:


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                String path = Matisse.obtainPathResult(data).get(0);
                if (uri != null) {
                    Glide.with(this)
                            .asBitmap() // some .jpeg files are actually gif
                            .load(uri)
                            .apply(new RequestOptions() {{
	                            override(Target.SIZE_ORIGINAL);
                            }})
                            .into(imageView);
                } else
                    Toast.makeText(this, "uri为null", Toast.LENGTH_SHORT).show();
            }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

通过onActivityResult接收数据,当然解析全都交给了Matisse,通过 Matisse.obtainPathResult(data)即可得到选择的图片path,最后通过Glide加载即可。

这里写图片描述

官方提供了两种主题:
R.style.Matisse_Zhihu (light mode)
R.style.Matisse_Dracula (dark mode)

在上述加上 **.theme(R.style.Matisse_Dracula)**即可切换默认主题。

imageEngine()中我们自定义了一个GlideLoadEngine类,看它的实现:

public class GlideLoadEngine implements ImageEngine{
/**
 * Load thumbnail of a static image resource.
 *
 * @param context     Context
 * @param resize      Desired size of the origin image
 * @param placeholder Placeholder drawable when image is not loaded yet
 * @param imageView   ImageView widget
 * @param uri         Uri of the loaded image
 */
@Override
public void loadThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri) {
    Glide.with(context)
            .asBitmap() // some .jpeg files are actually gif
            .load(uri)
            .apply(new RequestOptions()
                    .override(resize, resize)
                    .placeholder(placeholder)
                    .centerCrop())
            .into(imageView);
}

@Override
public void loadGifThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView,
                             Uri uri) {
    Glide.with(context)
            .asBitmap() // some .jpeg files are actually gif
            .load(uri)
            .apply(new RequestOptions()
                    .override(resize, resize)
                    .placeholder(placeholder)
                    .centerCrop())
            .into(imageView);
}

@Override
public void loadImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) {
    Glide.with(context)
            .load(uri)
            .apply(new RequestOptions()
                    .override(resizeX, resizeY)
                    .priority(Priority.HIGH)
                    .fitCenter())
            .into(imageView);
}

@Override
public void loadGifImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) {
    Glide.with(context)
            .asGif()
            .load(uri)
            .apply(new RequestOptions()
                    .override(resizeX, resizeY)
                    .priority(Priority.HIGH)
                    .fitCenter())
            .into(imageView);
}

@Override
public boolean supportAnimatedGif() {
    return true;
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

loadThumbnail自然就是缩略图的加载方式,Matisse传入了相关参数直接使用即可。
loadImage就是加载大图模式。

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e9f16cbbc2.css" rel="stylesheet">
                </div>
</article>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值