|
| 1 | +package app.rive.runtime.example |
| 2 | + |
| 3 | +import android.graphics.Color |
| 4 | +import android.graphics.drawable.GradientDrawable |
| 5 | +import android.os.Bundle |
| 6 | +import android.util.Log |
| 7 | +import android.view.Gravity |
| 8 | +import android.view.View |
| 9 | +import android.widget.FrameLayout |
| 10 | +import android.widget.LinearLayout |
| 11 | +import android.widget.TextView |
| 12 | +import androidx.activity.ComponentActivity |
| 13 | +import app.rive.runtime.kotlin.RiveAnimationView |
| 14 | +import app.rive.runtime.kotlin.core.File |
| 15 | +import app.rive.runtime.kotlin.core.Fit |
| 16 | + |
| 17 | +/** |
| 18 | + * Reproducer for the Fit.LAYOUT race condition. |
| 19 | + * |
| 20 | + * Launch via: adb shell am start -n app.rive.runtime.example/.FitLayoutReproActivity |
| 21 | + * Then force stop and relaunch repeatedly to observe intermittent failure. |
| 22 | + */ |
| 23 | +class FitLayoutReproActivity : ComponentActivity() { |
| 24 | + companion object { |
| 25 | + private const val TAG = "FitLayoutRepro" |
| 26 | + } |
| 27 | + |
| 28 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 29 | + super.onCreate(savedInstanceState) |
| 30 | + |
| 31 | + val density = resources.displayMetrics.density |
| 32 | + |
| 33 | + val root = LinearLayout(this).apply { |
| 34 | + orientation = LinearLayout.VERTICAL |
| 35 | + layoutParams = LinearLayout.LayoutParams( |
| 36 | + LinearLayout.LayoutParams.MATCH_PARENT, |
| 37 | + LinearLayout.LayoutParams.MATCH_PARENT |
| 38 | + ) |
| 39 | + setBackgroundColor(Color.parseColor("#1a1a2e")) |
| 40 | + setPadding(0, (48 * density).toInt(), 0, 0) |
| 41 | + } |
| 42 | + |
| 43 | + val statusText = TextView(this).apply { |
| 44 | + textSize = 16f |
| 45 | + setTextColor(Color.WHITE) |
| 46 | + setBackgroundColor(Color.argb(180, 0, 0, 0)) |
| 47 | + setPadding(24, 20, 24, 20) |
| 48 | + text = "Loading…" |
| 49 | + gravity = Gravity.CENTER |
| 50 | + } |
| 51 | + |
| 52 | + val riveBytes = resources.openRawResource(R.raw.layout_test).readBytes() |
| 53 | + val riveFile = File(riveBytes) |
| 54 | + |
| 55 | + val border = GradientDrawable().apply { |
| 56 | + setStroke((2 * density).toInt(), Color.parseColor("#666666")) |
| 57 | + setColor(Color.TRANSPARENT) |
| 58 | + } |
| 59 | + |
| 60 | + val riveContainer = FrameLayout(this).apply { |
| 61 | + foreground = border |
| 62 | + } |
| 63 | + |
| 64 | + val riveView = RiveAnimationView(this) |
| 65 | + riveView.layoutParams = FrameLayout.LayoutParams( |
| 66 | + FrameLayout.LayoutParams.MATCH_PARENT, |
| 67 | + (200 * density).toInt() |
| 68 | + ) |
| 69 | + riveContainer.addView(riveView) |
| 70 | + |
| 71 | + // Call setRiveFile IMMEDIATELY, before the view has been measured (still 0×0) |
| 72 | + riveView.setRiveFile( |
| 73 | + riveFile, |
| 74 | + fit = Fit.LAYOUT, |
| 75 | + autoplay = true, |
| 76 | + ) |
| 77 | + |
| 78 | + root.addView(statusText, LinearLayout.LayoutParams( |
| 79 | + LinearLayout.LayoutParams.MATCH_PARENT, |
| 80 | + LinearLayout.LayoutParams.WRAP_CONTENT |
| 81 | + )) |
| 82 | + |
| 83 | + root.addView(riveContainer, LinearLayout.LayoutParams( |
| 84 | + LinearLayout.LayoutParams.MATCH_PARENT, |
| 85 | + LinearLayout.LayoutParams.WRAP_CONTENT |
| 86 | + )) |
| 87 | + |
| 88 | + // Visual comparison: two bars showing expected vs actual artboard width |
| 89 | + val barContainer = LinearLayout(this).apply { |
| 90 | + orientation = LinearLayout.VERTICAL |
| 91 | + setPadding((16 * density).toInt(), (24 * density).toInt(), (16 * density).toInt(), 0) |
| 92 | + } |
| 93 | + |
| 94 | + val expectedLabel = TextView(this).apply { |
| 95 | + text = "Expected artboard width (= view width):" |
| 96 | + textSize = 12f |
| 97 | + setTextColor(Color.parseColor("#aaaaaa")) |
| 98 | + } |
| 99 | + barContainer.addView(expectedLabel) |
| 100 | + |
| 101 | + val expectedBar = View(this).apply { |
| 102 | + setBackgroundColor(Color.parseColor("#2e7d32")) |
| 103 | + } |
| 104 | + barContainer.addView(expectedBar, LinearLayout.LayoutParams(0, (24 * density).toInt()).apply { |
| 105 | + topMargin = (4 * density).toInt() |
| 106 | + }) |
| 107 | + |
| 108 | + val actualLabel = TextView(this).apply { |
| 109 | + text = "Actual artboard width:" |
| 110 | + textSize = 12f |
| 111 | + setTextColor(Color.parseColor("#aaaaaa")) |
| 112 | + setPadding(0, (12 * density).toInt(), 0, 0) |
| 113 | + } |
| 114 | + barContainer.addView(actualLabel) |
| 115 | + |
| 116 | + val actualBar = View(this).apply { |
| 117 | + setBackgroundColor(Color.parseColor("#c62828")) |
| 118 | + } |
| 119 | + barContainer.addView(actualBar, LinearLayout.LayoutParams(0, (24 * density).toInt()).apply { |
| 120 | + topMargin = (4 * density).toInt() |
| 121 | + }) |
| 122 | + |
| 123 | + val ratioText = TextView(this).apply { |
| 124 | + textSize = 13f |
| 125 | + setTextColor(Color.WHITE) |
| 126 | + setPadding(0, (12 * density).toInt(), 0, 0) |
| 127 | + gravity = Gravity.CENTER |
| 128 | + } |
| 129 | + barContainer.addView(ratioText) |
| 130 | + |
| 131 | + root.addView(barContainer, LinearLayout.LayoutParams( |
| 132 | + LinearLayout.LayoutParams.MATCH_PARENT, |
| 133 | + LinearLayout.LayoutParams.WRAP_CONTENT |
| 134 | + )) |
| 135 | + |
| 136 | + val infoText = TextView(this).apply { |
| 137 | + textSize = 11f |
| 138 | + setTextColor(Color.parseColor("#666666")) |
| 139 | + setPadding(24, (24 * density).toInt(), 24, 16) |
| 140 | + text = "layout_test.riv | Fit.LAYOUT\nForce stop & relaunch to test" |
| 141 | + gravity = Gravity.CENTER |
| 142 | + } |
| 143 | + root.addView(infoText) |
| 144 | + |
| 145 | + setContentView(root) |
| 146 | + |
| 147 | + riveView.addOnLayoutChangeListener { _, left, top, right, bottom, _, _, _, _ -> |
| 148 | + val viewWidthPx = right - left |
| 149 | + |
| 150 | + riveView.postDelayed({ |
| 151 | + val artboard = riveView.controller.activeArtboard ?: return@postDelayed |
| 152 | + val abW = artboard.width |
| 153 | + val abH = artboard.height |
| 154 | + val expectedW = viewWidthPx / density |
| 155 | + val expectedH = (bottom - top) / density |
| 156 | + val ok = kotlin.math.abs(abW - expectedW) < 2f |
| 157 | + |
| 158 | + val msg = if (ok) { |
| 159 | + "✓ Artboard %.0f dp = View %.0f dp".format(abW, expectedW) |
| 160 | + } else { |
| 161 | + "✗ Artboard %.0f dp ≠ View %.0f dp (%.1fx too wide!)".format( |
| 162 | + abW, expectedW, abW / expectedW) |
| 163 | + } |
| 164 | + Log.d(TAG, msg) |
| 165 | + statusText.text = msg |
| 166 | + statusText.setBackgroundColor( |
| 167 | + if (ok) Color.parseColor("#2e7d32") else Color.parseColor("#c62828") |
| 168 | + ) |
| 169 | + |
| 170 | + // Scale both bars relative to the container width |
| 171 | + val containerWidth = barContainer.width - barContainer.paddingLeft - barContainer.paddingRight |
| 172 | + val maxArtboard = maxOf(abW, expectedW) |
| 173 | + |
| 174 | + val expectedBarWidth = (containerWidth * (expectedW / maxArtboard)).toInt() |
| 175 | + val actualBarWidth = (containerWidth * (abW / maxArtboard)).toInt() |
| 176 | + |
| 177 | + expectedBar.layoutParams = expectedBar.layoutParams.apply { width = expectedBarWidth } |
| 178 | + actualBar.layoutParams = actualBar.layoutParams.apply { width = actualBarWidth } |
| 179 | + expectedBar.requestLayout() |
| 180 | + actualBar.requestLayout() |
| 181 | + |
| 182 | + if (ok) { |
| 183 | + actualBar.setBackgroundColor(Color.parseColor("#2e7d32")) |
| 184 | + ratioText.text = "Widths match ✓" |
| 185 | + ratioText.setTextColor(Color.parseColor("#4caf50")) |
| 186 | + } else { |
| 187 | + actualBar.setBackgroundColor(Color.parseColor("#c62828")) |
| 188 | + ratioText.text = "Artboard is %.1f× wider than the view!".format(abW / expectedW) |
| 189 | + ratioText.setTextColor(Color.parseColor("#ef5350")) |
| 190 | + } |
| 191 | + }, 500) |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments