Android: Zhihu open source image selection Library

Before starting the text, enjoy the effect of this image selector

Does it feel quite simple and beautiful? Anyway, I think so.

Gradle:

repositories {
    jcenter()
}

dependencies {
    compile 'com.zhihu.android:matisse:0.4.3'
}

It should be noted that the image selection library uses glass or Picasso as the image loading engine

If you use glide as your image loading engine, please add the rules mentioned in glide's readme and add additional rules:

-dontwarn com.squareup.picasso.**

If you use Picasso as your picture loading engine, please add the rules mentioned in Picasso's readme and add additional rules:

-dontwarn com.bumptech.glide.**

Before formally writing code, there are two related permissions to apply for:

android.permission.READ_EXTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE

Invalid Sign

We can start matisseactivity in normal activity or fragment

Matisse.from(MainActivity.this)
        .choose(MimeType.ofAll(), false) // 选择 mime 的类型
        .countable(true)
        .maxSelectable(9) // 图片选择的最多数量
        .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
        .thumbnailScale(0.85f) // 缩略图的比例
        .imageEngine(new GlideEngine()) // 使用的图片加载引擎
        .forResult(REQUEST_CODE_CHOOSE); // 设置作为标记的请求码

Callback the result in onactivityresult() in the activity or fragment that starts the picture selector

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

Matisse supports the following MIME types

By default, all images and videos will be displayed in Matisse, and you can't limit the optional MIME types by the following methods:

However, you can let Matisse display only one media type if

By default, there is a check mark in the upper right corner of the thumbnail so that you can select not only one image

The number of automatic growth uses countable (true) to display a number starting from 1

Maximum number of digits use maxselectable (int maxselectable) to limit the maximum number of digits that can be selected

Use restrictorientation * (@ screenorientation int orientation) to set the orientation required for image selection and preview activities.

If you want a fixed span count, use spancount (int spancount). When the direction changes, the range count will remain unchanged.

If you want to flexibly adapt to the grid size of different screens, use spancount (int spancount), which is not necessarily applied because the picture grid should fill the view container. The size of the measured picture grid will be as close to this value as possible.

Use thumbnail scale (float scale) to set the scale of the thumbnail bitmap relative to the view size, and it should be a floating-point value in (0.0, 1.0).

There are two built-in themes in Matisse:

When Matisse is started, the theme (@ styleres int themeid) method is called to use one of the themes

Matisse.from(MainActivity.this)
    ...
    .theme(R.style.Matisse_Zhihu | R.style.Matisse_Dracula)
    .forResult(REQUEST_CODE_CHOOSE);

You can customize the look of Matisse by deriving custom themes from two built-in themes, even their parents

These attributes (defined in attrs.xml) can be modified:

If you see here and think the article is well written, give it a praise? If you think there is something worth improving, please leave me a message. We will carefully inquire and correct the deficiencies. thank you.

I hope you can forward, share and follow me after reading this, and update the technical dry goods in the future. Thank you for your support!

Invalid Sign

Android architects have a long way to go. Let's encourage each other!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>