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.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

View file

@ -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,6 +112,10 @@ class LaunchersAdapter(
binding.launcherLabel.setTextColor(textColor)
binding.launcherIcon.setPadding(iconPadding, iconPadding, iconPadding, 0)
// 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)
@ -117,7 +124,14 @@ class LaunchersAdapter(
.placeholder(placeholderDrawable)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.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) }
setOnLongClickListener { view ->