首先,在XML布局文件中使用ConstraintLayout布局,例如:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

然后,在Java代码中,可以使用属性动画实现子视图平移隐藏效果,例如:

private void hideTextView() {
    TextView textView = findViewById(R.id.textView);
    ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "translationX", 0f, -textView.getWidth());
    animator.setDuration(500);
    animator.start();
}

这个方法会将TextView从当前位置平移到屏幕左侧,并在500毫秒内完成。如果需要显示TextView,则可以使用类似的代码:

private void showTextView() {
    TextView textView = findViewById(R.id.textView);
    ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "translationX", -textView.getWidth(), 0f);
    animator.setDuration(500);
    animator.start();
}
``

原文地址: http://www.cveoy.top/t/topic/doKi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录