From d22685b5ca1d19d3ec4a986a19162320b5083061 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 17 Mar 2017 21:22:57 +0100 Subject: [PATCH] load media thumbnails with Picasso --- .../gallery/activities/MediaActivity.kt | 20 +++++++++++++ .../gallery/adapters/MediaAdapter.kt | 30 ++++++++++++------- 2 files changed, 40 insertions(+), 10 deletions(-) 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 879c26d3a..5beaeff18 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -3,10 +3,12 @@ package com.simplemobiletools.gallery.activities import android.app.Activity import android.app.WallpaperManager import android.content.Intent +import android.content.res.Configuration import android.graphics.Bitmap import android.net.Uri import android.os.Bundle import android.support.v7.widget.GridLayoutManager +import android.util.DisplayMetrics import android.util.Log import android.view.Menu import android.view.MenuItem @@ -46,6 +48,10 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private var mLoadedInitialPhotos = false private var mStoredAnimateGifs = true + companion object { + var thumbnailSize = 0 + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_media) @@ -55,6 +61,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { mIsGetAnyIntent = getBooleanExtra(GET_ANY_INTENT, false) } + updateThumbnailWidth() media_refresh_layout.setOnRefreshListener({ getMedia() }) mPath = intent.getStringExtra(DIRECTORY) mStoredAnimateGifs = config.animateGifs @@ -90,6 +97,17 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } } + override fun onConfigurationChanged(newConfig: Configuration?) { + super.onConfigurationChanged(newConfig) + updateThumbnailWidth() + } + + private fun updateThumbnailWidth() { + val metrics = DisplayMetrics() + windowManager.defaultDisplay.getMetrics(metrics) + thumbnailSize = metrics.widthPixels / config.mediaColumnCnt + } + private fun initializeGallery() { if (isDirEmpty()) return @@ -259,11 +277,13 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private fun increaseColumnCount() { config.mediaColumnCnt = ++(media_grid.layoutManager as GridLayoutManager).spanCount invalidateOptionsMenu() + updateThumbnailWidth() } private fun reduceColumnCount() { config.mediaColumnCnt = --(media_grid.layoutManager as GridLayoutManager).spanCount invalidateOptionsMenu() + updateThumbnailWidth() } override fun deleteFiles(files: ArrayList) { 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 68eb6085a..25f9f81e5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -9,7 +9,6 @@ import com.bignerdranch.android.multiselector.ModalMultiSelectorCallback import com.bignerdranch.android.multiselector.MultiSelector import com.bignerdranch.android.multiselector.SwappingHolder import com.bumptech.glide.Glide -import com.bumptech.glide.load.DecodeFormat import com.bumptech.glide.load.engine.DiskCacheStrategy import com.simplemobiletools.commons.asynctasks.CopyMoveTask import com.simplemobiletools.commons.dialogs.ConfirmationDialog @@ -18,10 +17,12 @@ import com.simplemobiletools.commons.dialogs.RenameItemDialog import com.simplemobiletools.commons.extensions.beVisibleIf import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.gallery.R +import com.simplemobiletools.gallery.activities.MediaActivity import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.dialogs.CopyDialog import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.models.Medium +import com.squareup.picasso.Picasso import kotlinx.android.synthetic.main.photo_video_item.view.* import kotlinx.android.synthetic.main.photo_video_tmb.view.* import java.io.File @@ -249,18 +250,27 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList, val path = medium.path if (medium.isGif()) { if (animateGifs) { - Glide.with(activity).load(path).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).placeholder(backgroundColor) - .centerCrop().crossFade().into(medium_thumbnail) + Glide.with(activity) + .load(path) + .asGif() + .diskCacheStrategy(DiskCacheStrategy.SOURCE) + .centerCrop() + .crossFade() + .into(medium_thumbnail) } else { - Glide.with(activity).load(path).asBitmap().diskCacheStrategy(DiskCacheStrategy.NONE).placeholder(backgroundColor) - .centerCrop().into(medium_thumbnail) + Glide.with(activity) + .load(path) + .asBitmap() + .diskCacheStrategy(DiskCacheStrategy.SOURCE) + .centerCrop() + .into(medium_thumbnail) } - } else if (medium.isPng()) { - Glide.with(activity).load(path).asBitmap().format(DecodeFormat.PREFER_ARGB_8888).diskCacheStrategy(DiskCacheStrategy.RESULT) - .placeholder(backgroundColor).centerCrop().into(medium_thumbnail) } else { - Glide.with(activity).load(path).diskCacheStrategy(DiskCacheStrategy.RESULT).placeholder(backgroundColor) - .centerCrop().crossFade().into(medium_thumbnail) + Picasso.with(activity) + .load("file:$path") + .resize(MediaActivity.thumbnailSize, MediaActivity.thumbnailSize) + .centerCrop() + .into(medium_thumbnail) } setOnClickListener { viewClicked(multiSelector, medium, pos) }