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
private val SIZE = if (Util.isLowMemory(ApplicationDependencies.getApplication())) 100.dp else 200.dp
private val PARTICLES_PER_PIXEL = if (Util.isLowMemory(ApplicationDependencies.getApplication())) 0.002f else 0.005f
private val WIDTH = if (Util.isLowMemory(ApplicationDependencies.getApplication())) 50.dp else 100.dp
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 bufferBitmap: 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(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 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
fun update() {
val now = System.currentTimeMillis()
var dt = now - lastDrawTime
if (dt < 32) {
if (dt < 48) {
return
} else if (dt > 48) {
dt = 32
} else if (dt > 64) {
dt = 48
}
lastDrawTime = now

View File

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