Matisse的使用一

大佬們的博客

https://blog.csdn.net/qq_36043263/article/details/81707029

https://blog.csdn.net/u010356768/article/details/79026565

 

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

        }

    }
1
loadThumbnail自然就是縮略圖的加載方式,Matisse傳入了相關參數直接使用即可。 
loadImage就是加載大圖模式。
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章