Android随笔——Matisse的完整使用

版权声明:本文为博主原创文章,转载指明原处 https://blog.csdn.net/qq_36043263/article/details/81707029

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 >= 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);

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();
            }

        }

    }

通过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;
    }
}

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

猜你喜欢

转载自blog.csdn.net/qq_36043263/article/details/81707029
今日推荐