Android together to see the open source Image selection Library

Source: Internet
Author: User

Objective

In the actual development, the picture selector has always been an essential part, whether it is the setting of the QQ avatar, or send a forced circle of friends, all to use the picture selector to the user to choose the image they want to upload, recently know open source a picture selection library Matisse simply beautiful cry, and completely follow Android Interactive Design specification, is really very conscience.

Before starting the text, take a look at the effect of this image selector

is not feeling quite concise good-looking, anyway I think so.

First, basic use 1, import the corresponding dependent library

Gradle:

repositories {    jcenter()}dependencies {    compile ‘com.zhihu.android:matisse:0.4.3‘}

One thing to note is that this image selection library is using Glide or Picasso as the image loading engine

If you use Glide as your image loading engine, add the rules on Glide's README and add additional rules:

-dontwarn com.squareup.picasso.**

If you use Picasso as your image loading engine, add the rules on Picasso's README and add additional rules:

-dontwarn com.bumptech.glide.**
2. Start Matisse

Before the code is formally written, there are two related permissions that need to be applied first:

android.permission.READ_EXTERNAL_STORAGEandroid.permission.WRITE_EXTERNAL_STORAGE

So, if your APP is running on Android 6.0+, you need to process the runtime permissions before the next step.

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); // 设置作为标记的请求码
3, receive the results of the selection

Callback handling of results from Onactivityresult () in Activity or Fragment in the start picture selector

List<Uri> mSelected;@Overrideprotected 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);    }}
Second, the common API Mime type

Matisse supports the following MIME types

Mime Type File Extensions Media Type
image/jpeg jpg, jpeg image
image/png png Image
image/gif gif image
imagex-ms-bmp bmp image
video/mpeg mpeg, MPG video
video/mp4 mp4, M4V video
video/quicktime mov video
video/3gpp 3gp, 3GPP video
video/3gpp2 3g2, 3gpp2 video
Video/x-matroska mkv video
video/webm webm video
video/mp2ts ts video
Video/avi avi video

By default, all images and videos are displayed in Matisse, and you cannot limit the MIME types that you can choose by using the following methods:

    • Mimetype.ofall ()
    • Mimetype.of (MimeType type, MimeType ... rest)
    • Mimetype.ofimage ()
    • Mimetype.ofvideo ()

But you can let Matisse show only one type of media, if

    • When started, call theshowSingleMediaType(true)
    • Select only pictures or videos
Number

Default condition
In the top right corner of the thumbnail there is a check mark so that you can not only select an image

Number of auto-growth
Use countable(true) to display a number starting at 1

Maximum number of digits
Use maxSelectable(int maxSelectable) to limit the maximum number of selectable

Direction

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

Specifications of the grid

If you want the fixed span count to be used spanCount(int spanCount) , the range count will remain the same when the direction changes.

If you want flexibility in adapting to the grid size of different screens, use spanCount(int spanCount) this value, 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 as possible to that value.

Thumbnail zoom

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

Iii. Themes Built-in themes

There are two built-in themes in Matisse:

    • Zhihu (bright blue theme)
    • Dracula (Black Theme)

When starting Matisse, call the theme (@styleRes int Themeid) method to use one of the topics

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

Derive custom themes from two built-in themes, even their parents, and you can customize the appearance of the Matisse

These properties (as defined in Attrs.xml) can be modified:

Properties function
Colorprimary The color of the app bar
Colorprimarydark Darker variants of the status bar
Toolbar:toolbar The style of toolbar
Album.dropdown.title.color The color of the drop-down list in the album under album name
Album.dropdown.count.color toolbar element color, element includes navigation icon, selected album title and drop-down arrow icon on right
Album.thumbnail.placeholder Placeholder for album thumbnails
Album.emptyview To draw an empty view of a picture
Album.emptyView.textColor Text color of Blank view
Item.placeholder Placeholder color or drawable of the media grid
Page.bg The background color or drawable of the Activity or Fragment page
BottomToolbar.preview.textColor Background color or drawable of the toolbar at the bottom
BottomToolbar.apply.textColor Preview button text color on the toolbar at the bottom
Listpopupwindwostyle Drop-down menu style for album list
Capture.textcolor The text color of the capture grid that may appear at the top

The above is the whole content of this article, if you feel that writing can also, hope to come a wave of attention and like.

Guess you like it.
    • Android a very simple, elegant diary APP
    • Android can make you less detours of dry cleaning
    • Android Roll up sleeves, package yourself dialogfragment
    • Teach you how to make a good APP from scratch

Android together to see the open source Image selection Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.