From fc7dde0344e0b038f3dfadd851eb5da94fc0b47c Mon Sep 17 00:00:00 2001 From: Hung <> Date: Mon, 24 Oct 2022 11:45:28 +0700 Subject: [PATCH] fix(grid): GC Issue --- .../imageloader/DefaultImageLoader.kt | 2 ++ .../features/recyclers/RecyclerViewManager.kt | 9 ++--- .../esafirm/imagepicker/helper/ViewUtils.kt | 35 +++++++++++++++---- .../esafirm/sample/GrayscaleImageLoader.kt | 2 ++ 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/imagepicker/src/main/java/com/esafirm/imagepicker/features/imageloader/DefaultImageLoader.kt b/imagepicker/src/main/java/com/esafirm/imagepicker/features/imageloader/DefaultImageLoader.kt index d7931692..ccdaef7a 100644 --- a/imagepicker/src/main/java/com/esafirm/imagepicker/features/imageloader/DefaultImageLoader.kt +++ b/imagepicker/src/main/java/com/esafirm/imagepicker/features/imageloader/DefaultImageLoader.kt @@ -5,12 +5,14 @@ import com.bumptech.glide.Glide import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions import com.bumptech.glide.request.RequestOptions import com.esafirm.imagepicker.R +import com.esafirm.imagepicker.helper.getImageSize import com.esafirm.imagepicker.model.Image class DefaultImageLoader : ImageLoader { override fun loadImage(image: Image, imageView: ImageView, imageType: ImageType) { Glide.with(imageView.context) .load(image.uri) + .override(imageView.context.getImageSize()) .apply(RequestOptions .placeholderOf(if (imageType == ImageType.FOLDER) R.drawable.ef_folder_placeholder else R.drawable.ef_image_placeholder) .error(if (imageType == ImageType.FOLDER) R.drawable.ef_folder_placeholder else R.drawable.ef_image_placeholder) diff --git a/imagepicker/src/main/java/com/esafirm/imagepicker/features/recyclers/RecyclerViewManager.kt b/imagepicker/src/main/java/com/esafirm/imagepicker/features/recyclers/RecyclerViewManager.kt index 94122a74..6a8664cd 100644 --- a/imagepicker/src/main/java/com/esafirm/imagepicker/features/recyclers/RecyclerViewManager.kt +++ b/imagepicker/src/main/java/com/esafirm/imagepicker/features/recyclers/RecyclerViewManager.kt @@ -9,12 +9,9 @@ import androidx.recyclerview.widget.RecyclerView import com.esafirm.imagepicker.R import com.esafirm.imagepicker.adapter.FolderPickerAdapter import com.esafirm.imagepicker.adapter.ImagePickerAdapter -import com.esafirm.imagepicker.features.ImagePickerComponentsHolder -import com.esafirm.imagepicker.features.ImagePickerConfig -import com.esafirm.imagepicker.features.ImagePickerMode -import com.esafirm.imagepicker.features.IpCons -import com.esafirm.imagepicker.features.ReturnMode +import com.esafirm.imagepicker.features.* import com.esafirm.imagepicker.helper.ConfigUtils +import com.esafirm.imagepicker.helper.getRecyclerViewColumn import com.esafirm.imagepicker.listeners.OnFolderClickListener import com.esafirm.imagepicker.listeners.OnImageClickListener import com.esafirm.imagepicker.listeners.OnImageSelectedListener @@ -56,7 +53,7 @@ class RecyclerViewManager( * Set item size, column size base on the screen orientation */ fun changeOrientation(orientation: Int) { - imageColumns = if (orientation == Configuration.ORIENTATION_PORTRAIT) 3 else 5 + imageColumns = context.getRecyclerViewColumn() folderColumns = if (orientation == Configuration.ORIENTATION_PORTRAIT) 2 else 4 val shouldShowFolder = config.isFolderMode && isDisplayingFolderView val columns = if (shouldShowFolder) folderColumns else imageColumns diff --git a/imagepicker/src/main/java/com/esafirm/imagepicker/helper/ViewUtils.kt b/imagepicker/src/main/java/com/esafirm/imagepicker/helper/ViewUtils.kt index cd2a6bb5..b3477fa4 100644 --- a/imagepicker/src/main/java/com/esafirm/imagepicker/helper/ViewUtils.kt +++ b/imagepicker/src/main/java/com/esafirm/imagepicker/helper/ViewUtils.kt @@ -1,21 +1,42 @@ package com.esafirm.imagepicker.helper import android.content.Context +import android.content.res.Configuration import android.graphics.drawable.Drawable import android.os.Build import android.view.View import androidx.core.content.ContextCompat import com.esafirm.imagepicker.R + +const val GRID_3 = 3 +const val GRID_5 = 5 + object ViewUtils { fun getArrowIcon(context: Context): Drawable? { - val backResourceId: Int = if (Build.VERSION.SDK_INT >= 17 && context.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL) { - // For right-to-left layouts, pick the drawable that points to the right (forward). - R.drawable.ef_ic_arrow_forward - } else { - // For left-to-right layouts, pick the drawable that points to the left (back). - R.drawable.ef_ic_arrow_back - } + val backResourceId: Int = + if (Build.VERSION.SDK_INT >= 17 && context.resources.configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL) { + // For right-to-left layouts, pick the drawable that points to the right (forward). + R.drawable.ef_ic_arrow_forward + } else { + // For left-to-right layouts, pick the drawable that points to the left (back). + R.drawable.ef_ic_arrow_back + } return ContextCompat.getDrawable(context.applicationContext, backResourceId) } +} + +fun Context.getImageSize(): Int { + return this.resources.displayMetrics.widthPixels / getRecyclerViewColumn() +} + +fun Context.getRecyclerViewColumn(): Int { + val orientation = this.resources.configuration.orientation + return if (orientation == Configuration.ORIENTATION_LANDSCAPE) { + // In landscape + GRID_5 + } else { + // In portrait + GRID_3 + } } \ No newline at end of file diff --git a/sample/src/main/java/com/esafirm/sample/GrayscaleImageLoader.kt b/sample/src/main/java/com/esafirm/sample/GrayscaleImageLoader.kt index 33d7efc5..d2dc4509 100644 --- a/sample/src/main/java/com/esafirm/sample/GrayscaleImageLoader.kt +++ b/sample/src/main/java/com/esafirm/sample/GrayscaleImageLoader.kt @@ -6,6 +6,7 @@ import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions import com.bumptech.glide.request.RequestOptions import com.esafirm.imagepicker.features.imageloader.ImageLoader import com.esafirm.imagepicker.features.imageloader.ImageType +import com.esafirm.imagepicker.helper.getImageSize import com.esafirm.imagepicker.model.Image class GrayscaleImageLoader : ImageLoader { @@ -13,6 +14,7 @@ class GrayscaleImageLoader : ImageLoader { Glide.with(imageView) .asBitmap() .load(image.path) + .override(imageView.context.getImageSize()) .transition(BitmapTransitionOptions.withCrossFade()) .apply(REQUEST_OPTIONS) .into(imageView)