FAB翻译动画不起作用

时间:2018-04-18 12:46:37

标签: android android-recyclerview android-animation floating-action-button

我正在尝试隐藏一个浮动操作按钮,当RecyclerView滚动时,动画使得FAB必须从其实际位置向下移动并使用平移动画。显示FAB时的方式相同我用动画显示从底部到实际位置。从底部到实际位置的FAB显示工作正常。但是隐藏FAB是行不通的。我也在我的动画中使用阿尔法"淡入"和"淡出"效果。



<tr ng-repeat="rows in $ctrl.myData|   
    orderBy:$ctrl.sort.active:$ctrl.sort.descending |        
    filter: {Type:type} track by $index"
> 
&#13;
// fab_show.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">

    <!--Move-->
    <translate
        android:duration="1000"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXDelta="0%"
        android:toYDelta="200%"/>

    <!--Fade In-->
    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toAlpha="0.0"/>

</set>
&#13;
//fab_hide.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">

    <!--Move-->
    <translate
        android:duration="1000"
        android:fromXDelta="0%"
        android:fromYDelta="200%"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXDelta="0%"
        android:toYDelta="0%"/>

    <!--Fade Out-->
    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0"/>

</set>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

尝试这样 内部XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/layout_background"
    android:orientation="vertical">

  <com.malinskiy.superrecyclerview.SuperRecyclerView
        android:id="@+id/activity_marketplace_feed"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/dp_56"
        app:layout_empty="@layout/emptyview"
        app:mainLayoutId="@layout/layout_recyclerview_verticalscroll"
        app:recyclerClipToPadding="false"
        app:scrollbarStyle="insideOverlay" />

    <com.github.clans.fab.FloatingActionMenu
            android:id="@+id/menu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom|end"
            fab:menu_animationDelayPerItem="50"
            fab:menu_backgroundColor="#3b000000"
            fab:menu_buttonSpacing="0dp"
            fab:menu_colorNormal="@color/colorAccent"
            fab:menu_colorPressed="#FFCA3D3E"
            fab:menu_colorRipple="#99FFFFFF"
            fab:menu_fab_hide_animation="@anim/hide_to_bottom"
            fab:menu_fab_label=""
            fab:menu_fab_show_animation="@anim/show_from_bottom"
            fab:menu_fab_size="normal"
            fab:menu_icon="@drawable/fab_add"
            fab:menu_labels_colorNormal="#333333"
            fab:menu_labels_colorPressed="#444444"
            fab:menu_labels_colorRipple="#66FFFFFF"
            fab:menu_labels_cornerRadius="3dp"
            fab:menu_labels_ellipsize="none"
            fab:menu_labels_hideAnimation="@anim/fab_slide_out_to_right"
            fab:menu_labels_margin="0dp"
            fab:menu_labels_maxLines="-1"
            fab:menu_labels_padding="8dp"
            fab:menu_labels_paddingBottom="4dp"
            fab:menu_labels_paddingLeft="8dp"
            fab:menu_labels_paddingRight="8dp"
            fab:menu_labels_paddingTop="4dp"
            fab:menu_labels_position="left"
            fab:menu_labels_showAnimation="@anim/fab_slide_in_from_right"
            fab:menu_labels_showShadow="true"
            fab:menu_labels_singleLine="false"
            fab:menu_labels_style="@style/MenuLabelsStyle"
            fab:menu_labels_textColor="#FFFFFF"
            fab:menu_labels_textSize="@dimen/sp_14"
            fab:menu_openDirection="up"
            fab:menu_shadowColor="#66000000"
            fab:menu_shadowRadius="4dp"
            fab:menu_shadowXOffset="1dp"
            fab:menu_shadowYOffset="3dp"
            fab:menu_showShadow="true">

</FrameLayout>

资源文件

<强> hide_to_bottom

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fromYDelta="0"
    android:interpolator="@android:interpolator/accelerate_quint"
    android:toYDelta="30%p" />

<强> show_from_bottom

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="300"
    android:fromYDelta="30%p"
    android:interpolator="@android:interpolator/accelerate_cubic"
    android:toYDelta="0" />

<强> fab_slide_out_to_right

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:interpolator/accelerate_quint">
    <alpha
        android:fromAlpha="1"
        android:toAlpha="0"
        android:duration="150" />
    <translate
        android:fromXDelta="0"
        android:toXDelta="30%p"
        android:duration="200" />
</set>

<强> fab_slide_in_from_right

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:interpolator/overshoot">
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="300" />
    <translate
        android:fromXDelta="15%p"
        android:toXDelta="0"
        android:duration="200" />
</set>

<强>风格

 <style name="MenuLabelsStyle">
        <item name="android:background">@drawable/fab_label_background</item>
        <item name="android:textColor">#000</item>
        <item name="android:textSize">14sp</item>
        <item name="android:maxLines">1</item>
        <item name="android:ellipsize">end</item>
    </style>

Inside Oncreate

@BindView(R.id.menu) FloatingActionMenu fab_menu;
fab_menu.setClosedOnTouchOutside(true);

 feedRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener(){
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy){
                if (dy > 0 ||dy<0 && fab_menu.isShown())
                    fab_menu.hideMenu(true);
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE){
                    fab_menu.showMenu(true);
                }
                super.onScrollStateChanged(recyclerView, newState);
            }
        });

答案 1 :(得分:0)

我使用了这个link及其工作的好处。

&#13;
&#13;
recycler.addOnScrollListener(new RecyclerView.OnScrollListener()
        {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy)
            {
                if (dy > 0 ||dy<0 && fab.isShown())
                {
                    hideFab();
                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState)
            {
                if (newState == RecyclerView.SCROLL_STATE_IDLE)
                {
                    showFab();
                }

                super.onScrollStateChanged(recyclerView, newState);
            }
        });
&#13;
private void hideFab() {
        if (isFabShowing) {
            isFabShowing = false;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                final Point point = new Point();
                getWindow().getWindowManager().getDefaultDisplay().getSize(point);
                final float translation = fab.getY() - point.y;
                fab.animate().translationYBy(-translation).start();
            } else {
                Animation animation = AnimationUtils.makeOutAnimation(MainActivity.this, true);
                animation.setFillAfter(true);

                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        fab.setClickable(false);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });

                fab.startAnimation(animation);
            }
        }
    }

    private void showFab() {
        if (!isFabShowing) {
            isFabShowing = true;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                fab.animate().translationY(0).start();
            } else {
                Animation animation = AnimationUtils.makeInAnimation(MainActivity.this, false);
                animation.setFillAfter(true);

                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        fab.setClickable(true);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });

                fab.startAnimation(animation);
            }
        }
    }
&#13;
&#13;
&#13;