按钮滑动隐藏,停止滑动显示的动画

1,030 阅读1分钟

想用tweenAnimation来做

res下新建anim文件夹,添加bottom_slide_in.xml和bottom_slide_out.xml

bottom_slide_in.xml

滑入动画,从右边往左边移动,fromXDelta越大,移动距离越远

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="350">
    <translate
        android:fillAfter="true"
        android:fromXDelta="70%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="0%" />

</set>

bottom_slide_out.xml

滑出动画,从展示位置往右边移动,toXDelta越大,移动距离越远

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="350">
    <translate
        android:fillAfter="true"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="70%"
        android:toYDelta="0%" />

</set>

TestActivity.java

boolean scrollStateOld = true; //默认是展示状态

 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                switch (newState) {
            case SCROLL_STATE_IDLE: //空闲状态,停止滚动
                if (!scrollStateOld) {
                    showBottomSVGA(R.anim.bottom_slide_in); //只show一次
                }
                break;
            case SCROLL_STATE_DRAGGING: //手指拖动,滚动状态
            case SCROLL_STATE_SETTLING: //手指离开,惯性滚动状态
                if (scrollStateOld) {
                    showBottomSVGA(R.anim.bottom_slide_out); //只hide一次
                }
                break;
        }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                Log.e("zhen", "onScrolled: dx: " + dx + "  dy: " + dy);
            }
        });
        
         private void showBottomSVGA(int animationStyle) {
        Animation animation = AnimationUtils.loadAnimation(getActivity(), animationStyle);
        animation.setFillAfter(true); //动画结束后,保持动画结束的状态
        bottomSVGA.startAnimation(animation);
        scrollStateOld = !scrollStateOld; //取反
    }