知乎开源图片选择框架Matisse的使用

知乎开源图片选择框架Matisse的使用

    • 添加依赖
    • 自定义图片加载引擎
    • 混淆Glide
    • 使用
    • 接收回调结果

添加依赖

	implementation 'com.zhihu.android:matisse:0.5.2-beta5'
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

自定义图片加载引擎

    public class MyGlideEngine implements ImageEngine {

        @Override
        public void loadThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri) {
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(placeholder)//这里可自己添加占位图
                    .override(resize, resize);
            Glide.with(context)
                    .asBitmap()  // some .jpeg files are actually gif
                    .load(uri)
                    .apply(options)
                    .into(imageView);
        }

        @Override
        public void loadGifThumbnail(Context context, int resize, Drawable placeholder, ImageView imageView, Uri uri) {
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(placeholder)//这里可自己添加占位图
                    .override(resize, resize);
            Glide.with(context)
                    .asGif()  // some .jpeg files are actually gif
                    .load(uri)
                    .apply(options)
                    .into(imageView);
        }


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

        @Override
        public void loadGifImage(Context context, int resizeX, int resizeY, ImageView imageView, Uri uri) {
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .override(resizeX, resizeY);
            Glide.with(context)
                    .asGif()  // some .jpeg files are actually gif
                    .load(uri)
                    .apply(options)
                    .into(imageView);
        }

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

混淆Glide

-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}

# for DexGuard only
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule
-dontwarn com.squareup.picasso.**

使用

 Matisse.from(MainActivity.this)
                .choose(MimeType.ofImage())
                .countable(true)
                .maxSelectable(9)

                .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                .thumbnailScale(0.85f)
                .imageEngine(new MyGlideEngine())
                .forResult(REQUEST_CODE_CHOOSE);

接收回调结果

 @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
            List mSelected = Matisse.obtainResult(data);
            String s = getPAth(mSelected.get(0));

            Log.i("Path", s);
        }

    }

你可能感兴趣的:(Android开发)