【问题标题】:Is it possible to hide a view until the animation is finished or while it is running?是否可以在动画完成或运行时隐藏视图?
【发布时间】:2021-07-09 13:19:10
【问题描述】:

我正在尝试使用 TransitionManager.beginDelayedTransition 显示和隐藏动画线性布局,这是我得到的结果:

当我使视图可见时它工作正常,但是当隐藏的视图立即消失然后动画发生时,是否可以延迟隐藏?

我附上示例代码:

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

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

        binding.btn.setOnClickListener {
            val transition = ChangeBounds()
            transition.addTarget(binding.ly1)
            transition.addTarget(binding.ly2)
            TransitionManager.beginDelayedTransition(
                binding.root, transition
            )
            binding.ly1.visibility = if (binding.ly1.isVisible) View.GONE else View.VISIBLE
        }
    }
}

    <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"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/ly1"
        android:layout_width="0dp"
        android:layout_height="35dp"
        android:background="@color/black"
        android:orientation="vertical"
        android:visibility="gone"
        app:layout_constraintBottom_toTopOf="@+id/ly2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:id="@+id/ly2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/teal_200"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/ly1">

        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android android-studio kotlin android-transitions


    【解决方案1】:

    您可以将Animation.AnimationListener 与 3 种方法一起使用,您可以在此过程中的任何时间点做任何事情:

    animation.setAnimationListener(new Animation.AnimationListener(){
    
        @Override
        public void onAnimationStart(Animation arg0) {
        }           
    
        @Override
        public void onAnimationRepeat(Animation arg0) {
        }           
    
        @Override
        public void onAnimationEnd(Animation arg0) {
        }
    });
    

    这是documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      相关资源
      最近更新 更多