Android 折叠工具栏布局定义动画开始

Android 折叠工具栏布局定义动画开始,android,android-design-library,Android,Android Design Library,如何在CollasingToolbarLayout中更改动画的开始?我发现动画是由app:contentScrim属性控制的。 但是“阿尔法”动画开始得太快了。我是否可以以某种方式更改动画属性?我想从高度来看,大概是200dp 例如,如果我定义,我甚至看不到图像,因为它在开始时已被动画隐藏。以下是我的xml: <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/paralax_tabs_co

如何在CollasingToolbarLayout中更改动画的开始?我发现动画是由
app:contentScrim
属性控制的。 但是“阿尔法”动画开始得太快了。我是否可以以某种方式更改动画属性?我想从高度来看,大概是200dp

例如,如果我定义
,我甚至看不到图像,因为它在开始时已被动画隐藏。以下是我的xml:

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/paralax_tabs_collapse_toolbar"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:background="@color/my_action_bar_color"
    android:fitsSystemWindows="true"
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginStart="10dp"
    app:layout_scrollFlags="scroll|exitUntilCollapsed">

    <ImageView
        android:id="@+id/paralax_tabs_header_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:scaleType="centerCrop"
        app:layout_collapseMode="parallax" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/paralax_tabs_toolbar"
        android:layout_width="match_parent"
        android:layout_height="104dp"
        android:gravity="top"
        android:minHeight="?attr/actionBarSize"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleMarginTop="13dp" />

    <android.support.design.widget.TabLayout
        android:id="@+id/paralax_tabs_tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="bottom"
        android:background="@color/app_tab_backgorund"
        app:tabIndicatorColor="@android:color/white"
        app:tabIndicatorHeight="5dp"
        app:tabMode="scrollable" />
</android.support.design.widget.CollapsingToolbarLayout>

恐怕解决办法不容易

我必须创建自己的折叠工具栏布局来更改原始布局

原件:

在我的例子中,我更改了getScrimTriggerOffset方法

发件人:

致:

此方法在CollasingToolbarLayout的第677行中用于确定是显示还是隐藏内容scrim

// Show or hide the scrims if needed
if (mContentScrim != null || mStatusBarScrim != null) {
     if (getHeight() + verticalOffset < getScrimTriggerOffset() + insetTop) {
         showScrim();
     } else {
         hideScrim();
     }
}
//如果需要,显示或隐藏scrims
if(mContentScrim!=null | | mStatusBarScrim!=null){
如果(getHeight()+垂直偏移
我试图扩展CollasingToolbarLayout并重写此方法,但它被声明为final

很抱歉我的回复太晚了,希望能奏效

  • 创建自己的折叠工具栏布局
  • 覆盖下一个函数:(显示/隐藏内容scrim)、onAttachedToWindow和OnAttachedFromWindow
  • 在onAttachedToWindow中创建并添加到父级。在OnAttachedFromWindow中删除侦听器。(仅当父级为AppBarLayout时才有效。
  • 在SetCrimsShown中,设置开始动画的规则
  • 例如:

    public class MyCollapsingLayout extends CollapsingToolbarLayout {
        private int mCurrentVOffset = 0;
        private AppBarLayout.OnOffsetChangedListener mListener = null;
    
        ...
    
        @Override
        public void setScrimsShown(boolean shown) {
            boolean show = /*your code */
    
            super.setScrimsShown(show);
        }
    
        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
    
            final ViewParent parent = getParent();
    
            if (parent instanceof AppBarLayout) {
                mListener = new AppBarLayout.OnOffsetChangedListener() {
                    @Override
                    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                        mCurrentVOffset = verticalOffset;
                    }
                };
                ((AppBarLayout) parent).addOnOffsetChangedListener(mListener);
            }
        }
    
        @Override
        protected void onDetachedFromWindow() {
            final ViewParent parent = getParent();
            if (mListener != null && parent instanceof AppBarLayout) {
                ((AppBarLayout) parent).removeOnOffsetChangedListener(mListener);
            }
    
            super.onDetachedFromWindow();
        }
    }
    
    基于,您需要在CollasingToolbarLayout上调用该方法

    setScrimVisibleHeightTrigger(int)
    
    android.support.design:scrimVisibleHeightTrigger

    指定用于定义时间的可见高度(以像素为单位) 触发scrim可见性更改

    对于支持设计库版本:
    26.1.0

    public class MyCollapsingLayout extends CollapsingToolbarLayout {
        private int mCurrentVOffset = 0;
        private AppBarLayout.OnOffsetChangedListener mListener = null;
    
        ...
    
        @Override
        public void setScrimsShown(boolean shown) {
            boolean show = /*your code */
    
            super.setScrimsShown(show);
        }
    
        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
    
            final ViewParent parent = getParent();
    
            if (parent instanceof AppBarLayout) {
                mListener = new AppBarLayout.OnOffsetChangedListener() {
                    @Override
                    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                        mCurrentVOffset = verticalOffset;
                    }
                };
                ((AppBarLayout) parent).addOnOffsetChangedListener(mListener);
            }
        }
    
        @Override
        protected void onDetachedFromWindow() {
            final ViewParent parent = getParent();
            if (mListener != null && parent instanceof AppBarLayout) {
                ((AppBarLayout) parent).removeOnOffsetChangedListener(mListener);
            }
    
            super.onDetachedFromWindow();
        }
    }
    
    setScrimVisibleHeightTrigger(int)