GIT地址
首先遇到的就是一个坑,Matisse采用picasso或者Glide图片引擎,但是如果引用库版本与源码中不匹配就会产生无法调用引擎的问题,以正式版0.4.3为例,glide版本为3.7
Matisse.from(MainActivity.this)
.choose(MimeType.allOf())
.countable(true)
.maxSelectable(9)
.addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
.gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
.captureStrategy( new CaptureStrategy(true,"com.zhihu.matisse.sample.fileprovider"))
.restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
.thumbnailScale(0.85f)
.imageEngine(new GlideEngine())
.forResult(REQUEST_CODE_CHOOSE);
这是官方的引用代码,其中choose在0.5.0-beta3 可以选择OfVedio或者OfImage,但是并不是在展示的时候就对本地文件进行筛选,而是在选择的时候,提示文件类型不符。即其判断是加在选择内容之后的点击事件中的,按照文件后缀来辨别是否文件类型符合要求。
官方引用中还有一个GifSizeFilter方法,这个方法不能直接调用,而需要本地创建这个类
class GifSizeFilter extends Filter {
private int mMinWidth;
private int mMinHeight;
private int mMaxSize;
GifSizeFilter(int minWidth, int minHeight, int maxSizeInBytes) {
mMinWidth = minWidth;
mMinHeight = minHeight;
mMaxSize = maxSizeInBytes;
}
@Override
public Set<MimeType> constraintTypes() {
return new HashSet<MimeType>() {{
add(MimeType.GIF);
}};
}
@Override
public IncapableCause filter(Context context, Item item) {
if (!needFiltering(context, item))
return null;
Point size = PhotoMetadataUtils.getBitmapBound(context.getContentResolver(), item.getContentUri());
if (size.x < mMinWidth || size.y < mMinHeight || item.size > mMaxSize) {
return new IncapableCause(IncapableCause.DIALOG, context.getString(R.string.error_gif, mMinWidth,
String.valueOf(PhotoMetadataUtils.getSizeInMB(mMaxSize))));
}
return null;
}
}
官方的回调
List<Uri> mSelected;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
mSelected = Matisse.obtainResult(data);
Log.d("Matisse", "mSelected: " + mSelected);
}
}
captureStrategy参数是在SDK》6.0的时候需要使用的,需要在Manifest中注册provide
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.zhihu.matisse.sample.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths_public"></meta-data>
</provider>
其中file_paths_public 需要创建xml文件夹,加入该文件
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2017 Zhihu Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<paths>
<external-path
name="my_images"
path="Pictures"/>
</paths>
还有一个string
<string name="error_gif">长宽不小于 %1$dpx,且大小不超过 %2$sM。</string>
该框架还有一个坑是在调用前并不会进行安卓6.0以上权限的判断,需要手动加入权限申请代码后调用,否则直接挂掉。 Build.VERSION.SDK_INT > 23 关于权限申请此处不提
如果需要改变主题 可以加入.theme来选择主题
R.style.Matisse_Zhihu (light mode)
R.style.Matisse_Dracula (dark mode)
这是官方提供的2个主题
总体来说还是一个可以使用的框架,优势的话应该就是漂亮了,其他好像没看出啥优势