Improve spoiler performance by reducing number of particles and frame rate.

This commit is contained in:
Cody Henthorne
2023-06-14 10:36:43 -04:00
committed by GitHub
parent 305edf1928
commit 332c4ca26e
2 changed files with 13 additions and 17 deletions

View File

@@ -27,13 +27,14 @@ object SpoilerPaint {
*/ */
var shader: BitmapShader? = null var shader: BitmapShader? = null
private val SIZE = if (Util.isLowMemory(ApplicationDependencies.getApplication())) 100.dp else 200.dp private val WIDTH = if (Util.isLowMemory(ApplicationDependencies.getApplication())) 50.dp else 100.dp
private val PARTICLES_PER_PIXEL = if (Util.isLowMemory(ApplicationDependencies.getApplication())) 0.002f else 0.005f private val HEIGHT = if (Util.isLowMemory(ApplicationDependencies.getApplication())) 10.dp else 20.dp
private val PARTICLES_PER_PIXEL = if (Util.isLowMemory(ApplicationDependencies.getApplication())) 0.002f else 0.004f
private var shaderBitmap: Bitmap = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888) private var shaderBitmap: Bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ALPHA_8)
private var bufferBitmap: Bitmap = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888) private var bufferBitmap: Bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ALPHA_8)
private val bounds: Rect = Rect(0, 0, SIZE, SIZE) private val bounds: Rect = Rect(0, 0, WIDTH, HEIGHT)
private val paddedBounds: Rect private val paddedBounds: Rect
private val alphaStrength = arrayOf(0.9f, 0.7f, 0.5f) private val alphaStrength = arrayOf(0.9f, 0.7f, 0.5f)
@@ -67,16 +68,16 @@ object SpoilerPaint {
} }
/** /**
* Invoke every time before you need to use the [paint]. * Invoke every time before you need to use the [shader].
*/ */
@MainThread @MainThread
fun update() { fun update() {
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
var dt = now - lastDrawTime var dt = now - lastDrawTime
if (dt < 32) { if (dt < 48) {
return return
} else if (dt > 48) { } else if (dt > 64) {
dt = 32 dt = 48
} }
lastDrawTime = now lastDrawTime = now

View File

@@ -1,13 +1,12 @@
package org.thoughtcrime.securesms.components.spoiler package org.thoughtcrime.securesms.components.spoiler
import android.animation.ValueAnimator import android.animation.TimeAnimator
import android.graphics.Canvas import android.graphics.Canvas
import android.text.Annotation import android.text.Annotation
import android.text.Layout import android.text.Layout
import android.text.Spanned import android.text.Spanned
import android.view.View import android.view.View
import android.view.View.OnAttachStateChangeListener import android.view.View.OnAttachStateChangeListener
import android.view.animation.LinearInterpolator
import android.widget.TextView import android.widget.TextView
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.DefaultLifecycleObserver
@@ -34,15 +33,11 @@ class SpoilerRendererDelegate @JvmOverloads constructor(
private val cachedAnnotations = HashMap<Int, Map<Annotation, SpoilerClickableSpan?>>() private val cachedAnnotations = HashMap<Int, Map<Annotation, SpoilerClickableSpan?>>()
private val cachedMeasurements = HashMap<Int, SpanMeasurements>() private val cachedMeasurements = HashMap<Int, SpanMeasurements>()
private val animator = ValueAnimator.ofInt(0, 100).apply { private val animator = TimeAnimator().apply {
duration = 1000 setTimeListener { _, _, _ ->
interpolator = LinearInterpolator()
addUpdateListener {
SpoilerPaint.update() SpoilerPaint.update()
view.invalidate() view.invalidate()
} }
repeatCount = ValueAnimator.INFINITE
repeatMode = ValueAnimator.REVERSE
} }
init { init {