Matisse为我们提供了一个非常稳定了图片视频选择框架
依赖:
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);
- 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>