Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow software rendering to be invalidated when dynamic properties ch…
…ange (#2034) The test case failed to re-render before but works now.
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...m/airbnb/lottie/snapshots/tests/SoftwareRenderingDynamicPropertiesInvalidationTestCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.airbnb.lottie.snapshots.tests | ||
|
||
import android.graphics.Canvas | ||
import android.graphics.Color | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.FrameLayout | ||
import android.widget.ImageView | ||
import com.airbnb.lottie.LottieCompositionFactory | ||
import com.airbnb.lottie.LottieProperty | ||
import com.airbnb.lottie.RenderMode | ||
import com.airbnb.lottie.model.KeyPath | ||
import com.airbnb.lottie.snapshots.R | ||
import com.airbnb.lottie.snapshots.SnapshotTestCase | ||
import com.airbnb.lottie.snapshots.SnapshotTestCaseContext | ||
import com.airbnb.lottie.value.LottieFrameInfo | ||
import com.airbnb.lottie.value.LottieValueCallback | ||
|
||
/** | ||
* When using software rendering, Lottie caches its internal render bitmap if the animation changes. | ||
* However, if a dynamic property changes in a LottieValueCallback, the consumer must call LottieAnimationView.invalidate() | ||
* or LottieDrawable.invalidateSelf() to invalidate the drawing cache. | ||
*/ | ||
class SoftwareRenderingDynamicPropertiesInvalidationTestCase : SnapshotTestCase { | ||
override suspend fun SnapshotTestCaseContext.run() { | ||
val animationView = animationViewPool.acquire() | ||
val composition = LottieCompositionFactory.fromRawResSync(context, R.raw.heart).value!! | ||
animationView.setComposition(composition) | ||
animationView.renderMode = RenderMode.SOFTWARE | ||
animationView.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) | ||
animationView.scaleType = ImageView.ScaleType.FIT_CENTER | ||
val widthSpec = View.MeasureSpec.makeMeasureSpec( | ||
context.resources.displayMetrics.widthPixels, | ||
View.MeasureSpec.EXACTLY, | ||
) | ||
val heightSpec = View.MeasureSpec.makeMeasureSpec( | ||
context.resources.displayMetrics.heightPixels, | ||
View.MeasureSpec.EXACTLY, | ||
) | ||
val animationViewContainer = animationView.parent as ViewGroup | ||
animationViewContainer.measure(widthSpec, heightSpec) | ||
animationViewContainer.layout(0, 0, animationViewContainer.measuredWidth, animationViewContainer.measuredHeight) | ||
val canvas = Canvas() | ||
|
||
var color = Color.GREEN | ||
animationView.addValueCallback(KeyPath("**", "Fill 1"), LottieProperty.COLOR, object : LottieValueCallback<Int>() { | ||
override fun getValue(frameInfo: LottieFrameInfo<Int>?): Int { | ||
return color | ||
} | ||
}) | ||
|
||
var bitmap = bitmapPool.acquire(animationView.width, animationView.height) | ||
canvas.setBitmap(bitmap) | ||
animationView.draw(canvas) | ||
snapshotter.record(bitmap, "Heart Software Dynamic Property", "Green") | ||
bitmapPool.release(bitmap) | ||
|
||
bitmap = bitmapPool.acquire(animationView.width, animationView.height) | ||
canvas.setBitmap(bitmap) | ||
color = Color.BLUE | ||
animationView.invalidate() | ||
animationView.draw(canvas) | ||
snapshotter.record(bitmap, "Heart Software Dynamic Property", "Blue") | ||
bitmapPool.release(bitmap) | ||
|
||
animationViewPool.release(animationView) | ||
} | ||
} |