Я изо всех сил пытаюсь понять, почему не происходит анимация от верхней части View1 до нижней части View1. При анимации он начинает анимироваться снизу, и я не знаю, почему.
Это мой макет.
....
<View
android:id="@+id/view_reader"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="@drawable/ic_view_finder"
app:layout_constraintBottom_toBottomOf="@+id/surfaceView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/scannerBar"
android:layout_width="280dp"
android:layout_height="2dp"
android:background="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="@+id/view_reader"
app:layout_constraintEnd_toEndOf="@+id/view_reader"
app:layout_constraintStart_toStartOf="@+id/view_reader"
app:layout_constraintTop_toTopOf="@+id/view_reader"
app:layout_constraintVertical_bias="0.0" />
...
Итак, я хочу, чтобы этот scannerBar
начинал анимацию с верхней части view_reader и начинал снова, когда он доходит до нижней части view_reader.
И я сделал свою анимацию следующим образом:
val vto: ViewTreeObserver = binding.scannerBar.viewTreeObserver
val listener = ViewTreeObserver.OnGlobalLayoutListener {
val destinationScanView = (binding.viewReader.y +
binding.viewReader.height)
ObjectAnimator.ofFloat(
binding.scannerBar, "translationY",
binding.viewReader.y,
destinationScanView
).apply {
repeatMode = ValueAnimator.REVERSE
repeatCount = ValueAnimator.INFINITE
interpolator = AccelerateDecelerateInterpolator()
duration = 3000
}.start()
}
vto.addOnGlobalLayoutListener(listener)
Что мне не хватает? Есть ли другой способ анимировать вид проще, чем этот?
РЕДАКТИРОВАТЬ
Простой вопрос: как анимировать с помощью translationY scanBar (View) верхнюю часть view_reader (View) до нижней части view_reader (View)?
С MotionLayout проще?