I'm struggling trying to figure it out why is not animating from the top of the View1 to the bottom of the View1. When animating it start to animate from the bottom and I don't know why.
This is my layout.
....
<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" />
...
So I want this scannerBar
start animating from the top of the view_reader and start again when it arrives to the bottom of the view_reader.
And I did my animation as follows :
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)
What I'm missing? Is there any other way to animate a view easier than this?
EDIT
Simple question: How to animate with translationY scannerBar (View) from the top of view_reader(View) to bottom of view_reader(View)?
Is it easier with MotionLayout?