Convert all drawables into bitmap drawables for faster loading

This commit is contained in:
Ensar Sarajčić 2023-09-14 13:58:15 +02:00
parent 9e9aae015a
commit 44674ef47f
2 changed files with 25 additions and 10 deletions

View File

@ -29,6 +29,7 @@ import android.view.accessibility.AccessibilityNodeInfo
import android.view.animation.DecelerateInterpolator import android.view.animation.DecelerateInterpolator
import android.widget.PopupMenu import android.widget.PopupMenu
import androidx.core.graphics.drawable.toBitmap import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.drawable.toDrawable
import androidx.core.view.GestureDetectorCompat import androidx.core.view.GestureDetectorCompat
import androidx.core.view.WindowCompat import androidx.core.view.WindowCompat
import androidx.core.view.isVisible import androidx.core.view.isVisible
@ -845,13 +846,13 @@ class MainActivity : SimpleActivity(), FlingListener {
val label = info.loadLabel(packageManager).toString() val label = info.loadLabel(packageManager).toString()
val drawable = info.loadIcon(packageManager) ?: getDrawableForPackageName(packageName) ?: continue val drawable = info.loadIcon(packageManager) ?: getDrawableForPackageName(packageName) ?: continue
val placeholderColor = calculateAverageColor(drawable.toBitmap()) 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 // add Simple Launchers settings as an app
val drawable = getDrawableForPackageName(packageName) val drawable = getDrawableForPackageName(packageName)
val placeholderColor = calculateAverageColor(drawable!!.toBitmap()) 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) allApps.add(launcherSettings)
launchersDB.insertAll(allApps) launchersDB.insertAll(allApps)
return allApps return allApps

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.launcher.adapters package com.simplemobiletools.launcher.adapters
import android.graphics.drawable.Drawable
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -7,7 +8,9 @@ import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions 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.DrawableCrossFadeFactory
import com.bumptech.glide.request.transition.Transition
import com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller import com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
import com.simplemobiletools.commons.extensions.getProperTextColor import com.simplemobiletools.commons.extensions.getProperTextColor
@ -109,15 +112,26 @@ class LaunchersAdapter(
binding.launcherLabel.setTextColor(textColor) binding.launcherLabel.setTextColor(textColor)
binding.launcherIcon.setPadding(iconPadding, iconPadding, iconPadding, 0) binding.launcherIcon.setPadding(iconPadding, iconPadding, iconPadding, 0)
val factory = DrawableCrossFadeFactory.Builder(150).setCrossFadeEnabled(true).build() // Once all images are loaded and crossfades are done, directly set drawables
val placeholderDrawable = activity.resources.getColoredDrawableWithColor(R.drawable.placeholder_drawable, launcher.thumbnailColor) 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) Glide.with(activity)
.load(launcher.drawable) .load(launcher.drawable)
.placeholder(placeholderDrawable) .placeholder(placeholderDrawable)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE) .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.transition(DrawableTransitionOptions.withCrossFade(factory)) .transition(DrawableTransitionOptions.withCrossFade(factory))
.into(binding.launcherIcon) .into(object : DrawableImageViewTarget(binding.launcherIcon) {
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
super.onResourceReady(resource, transition)
// Set tag to true to mark that crossfade was already done on this view
view.tag = true
}
})
}
setOnClickListener { itemClick(launcher) } setOnClickListener { itemClick(launcher) }
setOnLongClickListener { view -> setOnLongClickListener { view ->