International practice, first address LPhotoPicker

LPhotoPicker

This is a nice, pure image selection framework, with good support for Kotlin, also available in Java. No clipping, no compression, no permission management, no redundant third-party library, only for the purest use, so that you are more flexible with other clipping, compression library combination. If you like frosted glass and if you want maximum customization, this library is not to be missed.

If image cropping is required, it is recommended that uCrop open source libraries be combined with uCrop

Questions and suggestions are welcome in Issues

preview

Emulator bangs

Demo Download Address

apk

To obtain

Add repository from Build. gradle repositories:

allprojects {
	repositories{... maven { url'https://jitpack.io'}}}Copy the code

Add to dependencies:

For the latest version, go to Github for LPhotoPicker

dependencies {
	
	implementation 'com. Making. Limuyang2: LPhotoPicker: version number'
}
Copy the code

Add the following configuration to build.gradle:

android {
    compileSdkVersion 27DefaultConfig {...........................// Add the following two lines of code
        renderscriptTargetApi 27  // Version must be the same as compileSdkVersion
        renderscriptSupportModeEnabled true}}Copy the code

use

Before using, remember to obtain permission! ‘Manifest. Permission. WRITE_EXTERNAL_STORAGE’, ‘because friends permissions framework used in their respective project is different, so the library integration again will look very fat.

The following uses Kotlin as an example, but Java is basically the same and is not listed separately

The following options can be added as required:

val intent = LPhotoPickerActivity.IntentBuilder(this)
               .maxChooseCount(3) // Maximum number of multiple selections
               .columnsNumber(4) // Display images in several columns
               .imageType(LPPImageType.ofAll()) // The type of image to display (webp/PNG/GIF/JPG)
               .pauseOnScroll(false) // Whether to pause image loading while sliding
               .isSingleChoose(false) // Single mode
               .imageEngine(LGlideEngine()) // Add a custom image loading engine (Glide loading engine already built in the library, if you do not need to customize, do not add this sentence)
               .theme(theme) / / theme
               .selectedPhotos(ArrayList<String>()) // An array of selected images
               .build()

startActivityForResult(intent, CHOOSE_PHOTO_REQUEST)
Copy the code

Receive data in activityonActivityResult

override fun onActivityResult(requestCode: Int, resultCode: Int.data: Intent?). {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                CHOOSE_PHOTO_REQUEST -> {
                    val selectedPhotos = LPhotoPickerActivity.getSelectedPhotos(data)}}}Copy the code

Custom picture loading engine frame

Glide loading engine has been prepared in the library, if you need to customize other picture loading framework, you can refer to LGlideEngine rewrite. The LImageEngine interface can be inherited by rewriting the method (Glide as an example) :

class LGlideEngine : LImageEngine {

    private val glideOptions by lazy { RequestOptions().centerCrop() }

    override fun load(context: Context, imageView: ImageView, path: String? .@DrawableRes placeholderRes: Int, resizeX: Int, resizeY: Int) {
        Glide.with(context)
                .load(path)
                .apply(glideOptions.placeholder(placeholderRes).override(resizeX, resizeY))
                .into(imageView)
    }

    // When pauseOnScroll is open, the following two must be written, otherwise "pauseOnScroll" does not take effect
    // Load paused
    override fun pause(context: Context) {
        Glide.with(context).pauseRequests()
    }

    // Resume loading
    override fun resume(context: Context) {
        Glide.with(context).resumeRequestsRecursive()
    }
}
Copy the code

Change the topic

Simple Settings, only need to be fixed once

Rewrite the following in the style text (the style name must be LPhotoTheme) :

<style name="LPhotoTheme" parent="LPPBaseTheme">
	<! -- Page general properties (image selection, image preview shared) -->
	<item name="l_pp_toolBar_height">56dp</item>
	<item name="l_pp_toolBar_backIcon">@drawable/ic_l_pp_back_android</item><! -- Back to button resources -->
	<item name="l_pp_toolBar_title_size">16sp</item><! --toolBar font size -->
	<item name="l_pp_bottomBar_height">50dp</item><! -- Height of bottom bar -->

	<! Image selection page properties -->
	<item name="l_pp_status_bar_color">@color/colorPrimary</item><! Status bar color -->
	<item name="l_pp_toolBar_background">@color/colorPrimary</item><! - the toolBar color -- -- >
	<item name="l_pp_picker_activity_bg">#F9F9F9</item><! -- Activity background color -->
	<item name="l_pp_toolBar_title_color">#f2f2f2</item><! --(Image selection page) Top toolBar font color -->
	<item name="l_pp_picker_bottomBar_background">#96ffffff</item><! -- Bottom bar color, if ground-glass effect is needed, color plus transparency -->
	<item name="l_pp_picker_bottomBar_enabled_text_color"># 333333</item><! -- The color of the bottom button when enabled -->
	<item name="l_pp_picker_bottomBar_unEnabled_text_color">#acacac</item><! -- Color of bottom bar button closed -->

	<! -- Image line width -->
	<item name="l_pp_picker_segmenting_line_width">2dp</item>

	<! Circular selection box style (image selection, image preview shared unless otherwise specified) -->
	<item name="l_pp_checkBox_color_tick" format="color">#fff</item><! -- Check color -->
	<item name="l_pp_checkBox_duration" format="integer">100</item><! -- Animation time -->
	<item name="l_pp_checkBox_color_checked" format="color">#fa5d5d</item><! -- Select the background color -->
	<item name="l_pp_checkBox_stroke_width" format="dimension">2dp</item><! -- (preview page only) circle width -->
	<item name="l_pp_checkBox_tick_width" format="dimension">2dp</item><! -- Width of the line -->
</style>
Copy the code

Setting the theme dynamically, requiring multiple style switches (e.g. in the case of theme switches)

As above, define attributes in XM first, but style names can be arbitrary:

<style name="MyDarkTheme" parent="LPPBaseTheme">... Property content is the same as above</style>
Copy the code

Call the theme() method in your code to set the XML theme:

LPhotoPickerActivity. IntentBuilder (this)... .theme(R.style.MyDarkTheme) .build()Copy the code

confusion

All contents of this library, except Glide, can be confused, has been tested. Add the following Glide confusion rule :(if you have customized other picture loading libraries, please add the confusion content by yourself)

#glide 4.x-keep class com.bumptech.glide.Glide { *; } -keep public class * implements com.bumptech.glide.module.GlideModule -keep public class * extends com.bumptech.glide.module.AppGlideModule -keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {* * []$VALUES;
  public *;
}
Copy the code

Appendix: Use uCrop together

Add the uCrop library first, and then receive the data in onActivityResult and pass it to uCrop

override fun onActivityResult(requestCode: Int, resultCode: Int.data: Intent?). {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                CHOOSE_PHOTO_REQUEST -> {
                    val selectedPhotos = LPhotoPickerActivity.getSelectedPhotos(data)
                    if(selectedPhotos.size == 1) {
                        // Use UCrop to crop the image
                        val outUri = Uri.fromFile(File(cacheDir, "${System.currentTimeMillis()}.jpg"))
                        UCrop.of(Uri.fromFile(File(selectedPhotos[0])), outUri)
                            .withAspectRatio(1f, 1f)
                            .withMaxResultSize(800.800)
                            .start(this)}}// Accept uCrop parameters
                UCrop.REQUEST_CROP   -> {
                    data? .let {val resultUri = UCrop.getOutput(data)
                        Log.d("UCrop.REQUEST_CROP", resultUri.toString())
                        Glide.with(this).load(resultUri).into(imgView)
                    }
                }
        }
}
Copy the code

Use the steps are very simple, specific use can refer to demo MainActivity

License

2018 limuyang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except inThe 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 toin 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.
Copy the code