Как анимировать вертикальное смещение ограничения с помощью SpringAnimation - PullRequest
0 голосов
/ 01 апреля 2020

У меня есть два ImageViews, и оба они ограничены центром с родителем, затем я устанавливаю layout_constraintVertical_bias из 1 и 0 для каждого, поэтому один будет в самом верху, а другой - в самом низу экрана.

Моя главная цель - анимировать их обоих в центр, используя SpringAnimation , потому что я действительно хочу эффект анимации весны, но проблема в том, что я не могу найти способ оживить их в центр? Мне пришлось установить finalPosition, но он не работает на всех размерах экрана.

Есть ли способ анимировать layout_constraintVertical_bias значение с помощью SpringAnimation?

UI

enter image description here

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    tools:context=".MainActivity">


    <ImageView
        android:id="@+id/ImageView_Bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1"
        app:srcCompat="@drawable/ic_launcher_foreground" />


    <ImageView
        android:id="@+id/ImageView_Top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0"
        app:srcCompat="@drawable/ic_launcher_foreground" />

</androidx.constraintlayout.widget.ConstraintLayout>

Код

class SplashActivity : AppCompatActivity() {

    private lateinit var binding: ActivitySplashBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySplashBinding.inflate(layoutInflater)
        setContentView(binding.root)

        translateAnim(binding.ImageView_Top, 650).start()  
        translateAnim(binding.ImageView_Bottom, -780).start()
    }


    private fun translateAnim(view: View, value: Int): SpringAnimation {
        val animation = SpringAnimation(view, SpringAnimation.TRANSLATION_Y)
        val spring = SpringForce()
        spring.finalPosition = view.y + value
        spring.stiffness = STIFFNESS_VERY_LOW
        spring.dampingRatio = DAMPING_RATIO_MEDIUM_BOUNCY 
        animation.spring = spring
        return animation
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...