/
ClipChildrenTestCase.kt
79 lines (71 loc) · 3 KB
/
ClipChildrenTestCase.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.airbnb.lottie.snapshots.tests
import android.graphics.Canvas
import android.view.LayoutInflater
import android.view.View
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import com.airbnb.lottie.LottieCompositionFactory
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.snapshots.SnapshotTestCase
import com.airbnb.lottie.snapshots.SnapshotTestCaseContext
import com.airbnb.lottie.snapshots.databinding.ClipChildrenBinding
import com.airbnb.lottie.snapshots.snapshotComposable
class ClipChildrenTestCase : SnapshotTestCase {
override suspend fun SnapshotTestCaseContext.run() {
val composition = LottieCompositionFactory.fromAssetSync(context, "Tests/BeyondBounds.json").value!!
snapshotComposable("Compose Clip Children", "Clip", renderHardwareAndSoftware = true) { renderMode ->
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.size(400.dp, 400.dp)
) {
LottieAnimation(
composition,
{ 0.7f },
contentScale = ContentScale.Crop,
renderMode = renderMode,
modifier = Modifier
.size(200.dp, 100.dp)
)
}
}
snapshotComposable("Compose Clip Children", "Dont Clip", renderHardwareAndSoftware = true) { renderMode ->
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.size(400.dp, 400.dp)
) {
LottieAnimation(
composition,
{ 0.7f },
contentScale = ContentScale.Crop,
renderMode = renderMode,
clipToCompositionBounds = false,
modifier = Modifier
.size(200.dp, 100.dp)
)
}
}
val binding = ClipChildrenBinding.inflate(LayoutInflater.from(context))
binding.animationView.setComposition(composition)
binding.root.measureAndLayout()
val bitmap1 = bitmapPool.acquire(binding.root.width, binding.root.height)
val canvas = Canvas(bitmap1)
binding.root.draw(canvas)
snapshotter.record(bitmap1, "Clip Children", "Clip")
val bitmap2 = bitmapPool.acquire(binding.root.width, binding.root.height)
canvas.setBitmap(bitmap2)
binding.animationView.clipToCompositionBounds = false
binding.root.draw(canvas)
snapshotter.record(bitmap2, "Clip Children", "Don't Clip")
}
private fun View.measureAndLayout() {
val spec = View.MeasureSpec.makeMeasureSpec(600, View.MeasureSpec.EXACTLY)
measure(spec, spec)
layout(0, 0, measuredWidth, measuredHeight)
}
}