From 44674ef47f2365fe66fc84420720505c713067c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Thu, 14 Sep 2023 13:58:15 +0200 Subject: [PATCH] Convert all drawables into bitmap drawables for faster loading --- .../launcher/activities/MainActivity.kt | 5 ++-- .../launcher/adapters/LaunchersAdapter.kt | 30 ++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt index bea73de..b52b41f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -29,6 +29,7 @@ import android.view.accessibility.AccessibilityNodeInfo import android.view.animation.DecelerateInterpolator import android.widget.PopupMenu import androidx.core.graphics.drawable.toBitmap +import androidx.core.graphics.drawable.toDrawable import androidx.core.view.GestureDetectorCompat import androidx.core.view.WindowCompat import androidx.core.view.isVisible @@ -845,13 +846,13 @@ class MainActivity : SimpleActivity(), FlingListener { val label = info.loadLabel(packageManager).toString() val drawable = info.loadIcon(packageManager) ?: getDrawableForPackageName(packageName) ?: continue val placeholderColor = calculateAverageColor(drawable.toBitmap()) - allApps.add(AppLauncher(null, label, packageName, activityName, 0, placeholderColor, drawable)) + allApps.add(AppLauncher(null, label, packageName, activityName, 0, placeholderColor, drawable.toBitmap().toDrawable(resources))) } // add Simple Launchers settings as an app val drawable = getDrawableForPackageName(packageName) val placeholderColor = calculateAverageColor(drawable!!.toBitmap()) - val launcherSettings = AppLauncher(null, getString(R.string.launcher_settings), packageName, "", 0, placeholderColor, drawable) + val launcherSettings = AppLauncher(null, getString(R.string.launcher_settings), packageName, "", 0, placeholderColor, drawable.toBitmap().toDrawable(resources)) allApps.add(launcherSettings) launchersDB.insertAll(allApps) return allApps diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/LaunchersAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/LaunchersAdapter.kt index ece3d76..db98c0d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/LaunchersAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/adapters/LaunchersAdapter.kt @@ -1,5 +1,6 @@ package com.simplemobiletools.launcher.adapters +import android.graphics.drawable.Drawable import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -7,7 +8,9 @@ import androidx.recyclerview.widget.RecyclerView import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions +import com.bumptech.glide.request.target.DrawableImageViewTarget import com.bumptech.glide.request.transition.DrawableCrossFadeFactory +import com.bumptech.glide.request.transition.Transition import com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor import com.simplemobiletools.commons.extensions.getProperTextColor @@ -109,15 +112,26 @@ class LaunchersAdapter( binding.launcherLabel.setTextColor(textColor) binding.launcherIcon.setPadding(iconPadding, iconPadding, iconPadding, 0) - val factory = DrawableCrossFadeFactory.Builder(150).setCrossFadeEnabled(true).build() - val placeholderDrawable = activity.resources.getColoredDrawableWithColor(R.drawable.placeholder_drawable, launcher.thumbnailColor) + // Once all images are loaded and crossfades are done, directly set drawables + if (launcher.drawable != null && binding.launcherIcon.tag == true) { + binding.launcherIcon.setImageDrawable(launcher.drawable) + } else { + val factory = DrawableCrossFadeFactory.Builder(150).setCrossFadeEnabled(true).build() + val placeholderDrawable = activity.resources.getColoredDrawableWithColor(R.drawable.placeholder_drawable, launcher.thumbnailColor) - Glide.with(activity) - .load(launcher.drawable) - .placeholder(placeholderDrawable) - .diskCacheStrategy(DiskCacheStrategy.RESOURCE) - .transition(DrawableTransitionOptions.withCrossFade(factory)) - .into(binding.launcherIcon) + Glide.with(activity) + .load(launcher.drawable) + .placeholder(placeholderDrawable) + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) + .transition(DrawableTransitionOptions.withCrossFade(factory)) + .into(object : DrawableImageViewTarget(binding.launcherIcon) { + override fun onResourceReady(resource: Drawable, transition: Transition?) { + super.onResourceReady(resource, transition) + // Set tag to true to mark that crossfade was already done on this view + view.tag = true + } + }) + } setOnClickListener { itemClick(launcher) } setOnLongClickListener { view ->