Android ConstraintLayout 实现子视图平移隐藏动画
Android ConstraintLayout 是一种强大的布局管理器,可以实现各种复杂的布局效果。在 ConstraintLayout 中,可以使用属性动画来实现子视图的平移隐藏效果。下面是实现步骤:
- 在布局文件中添加 ConstraintLayout 和需要平移隐藏的子视图。
- 在代码中获取 ConstraintLayout 和子视图的引用,并创建属性动画。
- 在需要触发平移隐藏效果的时候,启动属性动画。
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"></androidx.constraintlayout.widget.ConstraintLayout>
<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" />
ConstraintLayout layout = findViewById(R.id.layout);
TextView textView = findViewById(R.id.textView);
ObjectAnimator animator = ObjectAnimator.ofFloat(textView, 'translationX', 0, -textView.getWidth());
animator.setDuration(1000);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animator.start();
}
});
这样就可以实现子视图的平移隐藏效果了。需要注意的是,平移的距离应该是子视图的宽度,这样才能完全隐藏子视图。
原文地址: https://www.cveoy.top/t/topic/kKqu 著作权归作者所有。请勿转载和采集!