From 4fedc0557111786a19ce42e0284ea7d2624d00e7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 28 Oct 2017 13:27:50 +0200 Subject: [PATCH] use GifDrawables at thumbnails too --- .../gallery/activities/MainActivity.kt | 10 +++- .../gallery/activities/MediaActivity.kt | 12 +++-- .../gallery/adapters/DirectoryAdapter.kt | 11 ++-- .../gallery/adapters/MediaAdapter.kt | 28 +++++----- .../gallery/extensions/activity.kt | 52 ++++++------------- app/src/main/res/layout/pager_photo_item.xml | 3 +- 6 files changed, 58 insertions(+), 58 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index 6dfa6ef7a..108f34310 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -125,11 +125,17 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { super.onResume() config.isThirdPartyIntent = false if (mStoredAnimateGifs != config.animateGifs) { - directories_grid.adapter?.notifyDataSetChanged() + (directories_grid.adapter as? DirectoryAdapter)?.apply { + animateGifs = config.animateGifs + notifyDataSetChanged() + } } if (mStoredCropThumbnails != config.cropThumbnails) { - directories_grid.adapter?.notifyDataSetChanged() + (directories_grid.adapter as? DirectoryAdapter)?.apply { + cropThumbnails = config.cropThumbnails + notifyDataSetChanged() + } } if (mStoredShowMediaCount != config.showMediaCount) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index facb606a7..ca3df1126 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -89,12 +89,18 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { override fun onResume() { super.onResume() - if (mShowAll && mStoredAnimateGifs != config.animateGifs) { - media_grid.adapter?.notifyDataSetChanged() + if (mStoredAnimateGifs != config.animateGifs) { + (media_grid.adapter as? MediaAdapter)?.apply { + animateGifs = config.animateGifs + notifyDataSetChanged() + } } if (mStoredCropThumbnails != config.cropThumbnails) { - media_grid.adapter?.notifyDataSetChanged() + (media_grid.adapter as? MediaAdapter)?.apply { + cropThumbnails = config.cropThumbnails + notifyDataSetChanged() + } } if (mStoredScrollHorizontally != config.scrollHorizontally) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index 7f36dfd26..ed836f91d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -30,11 +30,13 @@ import java.util.* class DirectoryAdapter(val activity: SimpleActivity, var dirs: MutableList, val listener: DirOperationsListener?, val isPickIntent: Boolean, val itemClick: (Directory) -> Unit) : RecyclerView.Adapter() { - val config = activity.config + private val config = activity.config var actMode: ActionMode? = null var primaryColor = config.primaryColor var scrollVertically = !config.scrollHorizontally var showMediaCount = config.showMediaCount + var animateGifs = config.animateGifs + var cropThumbnails = config.cropThumbnails private val multiSelector = MultiSelector() private val isListViewType = config.viewTypeFolders == VIEW_TYPE_LIST @@ -353,7 +355,7 @@ class DirectoryAdapter(val activity: SimpleActivity, var dirs: MutableList (Unit)) : SwappingHolder(view, MultiSelector()) { - fun bindView(directory: Directory, isPinned: Boolean, scrollVertically: Boolean, isListView: Boolean, textColor: Int, showMediaCount: Boolean): View { + fun bindView(directory: Directory, isPinned: Boolean, scrollVertically: Boolean, isListView: Boolean, textColor: Int, showMediaCount: Boolean, + animateGifs: Boolean, cropThumbnails: Boolean): View { itemView.apply { dir_name.text = directory.name dir_path?.text = "${directory.path.substringBeforeLast("/")}/" photo_cnt.text = directory.mediaCnt.toString() - activity.loadImage(directory.tmb, dir_thumbnail, scrollVertically) + activity.loadImage(directory.tmb, dir_thumbnail, scrollVertically, animateGifs, cropThumbnails) dir_pin.beVisibleIf(isPinned) dir_sd_card.beVisibleIf(activity.isPathOnSD(directory.path)) photo_cnt.beVisibleIf(showMediaCount) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt index 6ede29e0d..82e9d9fae 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -28,18 +28,21 @@ import java.util.* class MediaAdapter(val activity: SimpleActivity, var media: MutableList, val listener: MediaOperationsListener?, val isPickIntent: Boolean, val allowMultiplePicks: Boolean, val itemClick: (Medium) -> Unit) : RecyclerView.Adapter() { - val multiSelector = MultiSelector() - val config = activity.config - val isListViewType = config.viewTypeFiles == VIEW_TYPE_LIST - var skipConfirmationDialog = false - + private val config = activity.config var actMode: ActionMode? = null - var itemViews = SparseArray() - val selectedPositions = HashSet() var primaryColor = config.primaryColor - var textColor = config.textColor - var displayFilenames = config.displayFileNames var scrollVertically = !config.scrollHorizontally + var animateGifs = config.animateGifs + var cropThumbnails = config.cropThumbnails + + private val multiSelector = MultiSelector() + private val isListViewType = config.viewTypeFiles == VIEW_TYPE_LIST + private var skipConfirmationDialog = false + + private var itemViews = SparseArray() + private val selectedPositions = HashSet() + private var textColor = config.textColor + private var displayFilenames = config.displayFileNames fun toggleItemSelection(select: Boolean, pos: Int) { if (select) { @@ -284,7 +287,7 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList, } override fun onBindViewHolder(holder: ViewHolder, position: Int) { - itemViews.put(position, holder.bindView(media[position], displayFilenames, scrollVertically, isListViewType, textColor)) + itemViews.put(position, holder.bindView(media[position], displayFilenames, scrollVertically, isListViewType, textColor, animateGifs, cropThumbnails)) toggleItemSelection(selectedPositions.contains(position), position) holder.itemView.tag = holder } @@ -355,12 +358,13 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList, val multiSelector: MultiSelector, val listener: MediaOperationsListener?, val allowMultiplePicks: Boolean, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) { - fun bindView(medium: Medium, displayFilenames: Boolean, scrollVertically: Boolean, isListViewType: Boolean, textColor: Int): View { + fun bindView(medium: Medium, displayFilenames: Boolean, scrollVertically: Boolean, isListViewType: Boolean, textColor: Int, + animateGifs: Boolean, cropThumbnails: Boolean): View { itemView.apply { play_outline.visibility = if (medium.video) View.VISIBLE else View.GONE photo_name.beVisibleIf(displayFilenames || isListViewType) photo_name.text = medium.name - activity.loadImage(medium.path, medium_thumbnail, scrollVertically) + activity.loadImage(medium.path, medium_thumbnail, scrollVertically, animateGifs, cropThumbnails) if (isListViewType) { photo_name.setTextColor(textColor) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt index 7d76be8a3..c7fd3b5e9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/activity.kt @@ -6,6 +6,7 @@ import android.net.Uri import android.provider.MediaStore import android.support.v7.app.AppCompatActivity import android.view.View +import android.widget.ImageView import com.bumptech.glide.Glide import com.bumptech.glide.load.DecodeFormat import com.bumptech.glide.load.engine.DiskCacheStrategy @@ -22,6 +23,7 @@ import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.views.MySquareImageView +import pl.droidsonroids.gif.GifDrawable import java.io.File import java.util.* @@ -199,24 +201,28 @@ fun SimpleActivity.toggleFileVisibility(oldFile: File, hide: Boolean, callback: } } -fun Activity.loadImage(path: String, target: MySquareImageView, verticalScroll: Boolean) { +fun Activity.loadImage(path: String, target: MySquareImageView, verticalScroll: Boolean, animateGifs: Boolean, cropThumbnails: Boolean) { target.isVerticalScrolling = verticalScroll if (path.isImageFast() || path.isVideoFast()) { if (path.isPng()) { - loadPng(path, target) + loadPng(path, target, cropThumbnails) } else { - loadJpg(path, target) + loadJpg(path, target, cropThumbnails) } } else if (path.isGif()) { - if (config.animateGifs) { - loadAnimatedGif(path, target) + val gifDrawable = GifDrawable(path) + target.setImageDrawable(gifDrawable) + if (animateGifs) { + gifDrawable.start() } else { - loadStaticGif(path, target) + gifDrawable.stop() } + + target.scaleType = if (cropThumbnails) ImageView.ScaleType.CENTER_CROP else ImageView.ScaleType.FIT_CENTER } } -fun Activity.loadPng(path: String, target: MySquareImageView) { +fun Activity.loadPng(path: String, target: MySquareImageView, cropThumbnails: Boolean) { val options = RequestOptions() .signature(path.getFileSignature()) .diskCacheStrategy(DiskCacheStrategy.RESOURCE) @@ -226,11 +232,11 @@ fun Activity.loadPng(path: String, target: MySquareImageView) { .asBitmap() .load(path) - if (config.cropThumbnails) options.centerCrop() else options.fitCenter() + if (cropThumbnails) options.centerCrop() else options.fitCenter() builder.apply(options).into(target) } -fun Activity.loadJpg(path: String, target: MySquareImageView) { +fun Activity.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Boolean) { val options = RequestOptions() .signature(path.getFileSignature()) .diskCacheStrategy(DiskCacheStrategy.RESOURCE) @@ -238,36 +244,10 @@ fun Activity.loadJpg(path: String, target: MySquareImageView) { val builder = Glide.with(applicationContext) .load(path) - if (config.cropThumbnails) options.centerCrop() else options.fitCenter() + if (cropThumbnails) options.centerCrop() else options.fitCenter() builder.apply(options).transition(DrawableTransitionOptions.withCrossFade()).into(target) } -fun Activity.loadAnimatedGif(path: String, target: MySquareImageView) { - val options = RequestOptions() - .signature(path.getFileSignature()) - .diskCacheStrategy(DiskCacheStrategy.NONE) - - val builder = Glide.with(applicationContext) - .asGif() - .load(path) - - if (config.cropThumbnails) options.centerCrop() else options.fitCenter() - builder.apply(options).transition(DrawableTransitionOptions.withCrossFade()).into(target) -} - -fun Activity.loadStaticGif(path: String, target: MySquareImageView) { - val options = RequestOptions() - .signature(path.getFileSignature()) - .diskCacheStrategy(DiskCacheStrategy.DATA) - - val builder = Glide.with(applicationContext) - .asBitmap() - .load(path) - - if (config.cropThumbnails) options.centerCrop() else options.fitCenter() - builder.apply(options).into(target) -} - fun Activity.getCachedDirectories(): ArrayList { val token = object : TypeToken>() {}.type return Gson().fromJson>(config.directories, token) ?: ArrayList(1) diff --git a/app/src/main/res/layout/pager_photo_item.xml b/app/src/main/res/layout/pager_photo_item.xml index 152c24684..6e2f333c8 100644 --- a/app/src/main/res/layout/pager_photo_item.xml +++ b/app/src/main/res/layout/pager_photo_item.xml @@ -9,7 +9,8 @@ + android:layout_height="match_parent" + android:scaleType="fitCenter"/>