gpt4 book ai didi

android - 从顶部动画 AppBarLayout

转载 作者:行者123 更新时间:2023-11-29 17:28:29 26 4
gpt4 key购买 nike

我正在尝试从顶部为 AppBarLayout 设置动画,就像 this 中的工具栏一样视频。

这是我的布局

<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:id="@+id/abl"
android:layout_width="match_parent"
android:layout_height="152dp">

<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?colorPrimary"
app:titleEnabled="true"
app:expandedTitleTextAppearance="@style/Title.Expanded"
app:collapsedTitleTextAppearance="@style/Title.collapsed"
app:title="Title"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:layout_collapseMode="pin"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

</android.support.v7.widget.Toolbar>

</android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:behavior_overlapTop="48dp"/>

</android.support.design.widget.CoordinatorLayout>

我计划将 AppBarLayout 平移其高度的负数,然后将其设置为 0 的动画。

但由于某些原因 abl.setTranslationY(-height) 不工作 :(

编辑:如果在所有 View 布局后执行(例如,如果在按钮的点击监听器中执行,则有效),但在onCreateonResumeonCreateOptions

编辑 2:我需要在 Activity 开始时不应该有 appBarLayout 然后它应该从顶部进入。请参阅视频。

最佳答案

使用这个:

private static final float APPBAR_ELEVATION = 14f;

private void AnimateAppBar(final AppBarLayout appBar) {
appBar.animate()
.translationY(-appBar.getHeight())
.setInterpolator(new LinearInterpolator())
.setDuration(500)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
appBar.setElevation(0);
appBar.animate()
.translationY(0)
.setInterpolator(new LinearInterpolator())
.setDuration(500)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
appBar.setElevation(APPBAR_ELEVATION);
}
});
}
});
}

这是调用 AnimateAppBar(appBar); 时的结果

enter image description here

您可以同时更改 Duration 以加快动画速度,如果您不希望动画结束时效果太高,则将 APPBAR_ELEVATION 设置为 0。

编辑:

onCreate() 中使用它

animationAppBarDown(appBar);

并添加这个方法:

private void animationAppBarDown(final AppBarLayout appBar){
new CountDownTimer(300, 1) {
public void onTick(long millisUntilFinished) {
appBar.setTranslationY(-appBar.getHeight());
}

public void onFinish() {
appBar.animate()
.translationY(0)
.setDuration(500).start();
}
}.start();
}

这是结果:

enter image description here

关于android - 从顶部动画 AppBarLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33860825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com