【问题标题】:Android Floating Action Button animationAndroid 浮动操作按钮动画
【发布时间】:2015-11-08 12:11:06
【问题描述】:

当滚动视图移动时,我想要的只是一个简单的动画。我尝试了一些解决方案,但没有一个能完美/流畅地工作。如果我滚动,我想用向下滑动动画隐藏工厂,如果 2 秒后没有任何反应,工厂会用向上滑动动画显示。我知道这是一个基本问题,感谢您的耐心。

提前致谢。

final ScrollView scroll = (ScrollView) v.findViewById(R.id.sw);
scroll.setOnTouchListener(new View.OnTouchListener()){
   @Override
   public boolean onTouch(View v, Motionevent event){
      int action = event.getAction();
      if (action == Motionevent.ACTION_MOVE){

      //slide down animation here

      }else{
        new Handler().postDelayed(new Runnable(){
          public void run(){

          //slide up animation here

          }
        }, 2000);
      }
    return false;
  }
});

【问题讨论】:

    标签: android user-interface animation floating-action-button


    【解决方案1】:

    这里是a tutorial,如何使用带有滚动动画的FAB按钮。

    基本上:

    1. 使用 v22.2.1 支持 v4 库,有一个 show()hide() 方法可以执行浮动操作按钮的淡入和淡出动画
    2. 您必须将 ScrollView 和 FAB 放在 CoordinatorLayout 中。
    3. 将 FAB layout_anchor 设置为 ScrollView's id
    4. 创建一个类并扩展FloatingActionButton.Behavior 类并将其设置为布局xml 中FAB 的layout_behavior 属性
    5. 覆盖您的 Behavior 类 onStartNestedScroll 以检查是否垂直
    6. 覆盖您的 Behavior 类 onStopNestedScroll 以在向下滚动时调用子 (FAB) 参数 hide() 方法并延迟 Runnable 以在 2 秒后显示 FAB

    布局如下:

    <android.support.design.widget.CoordinatorLayout
    ... >
        <ScrollView
        android:id="@+id/myList"
        ...
        />
            <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            app:layout_anchor="@id/myList"
            app:layout_behavior="package.CustomScrollAwareBehavior"
            ...
            />
        </android.support.design.widget.CoordinatorLayout>
    

    我建议,在 Behavior 类中也创建一个 Handler 来调用 FAB 的 show() 方法。 行为类如(未测试):

    public class CustomScrollAwareBehavior extends FloatingActionButton.Behavior{
    
    private Handler handler = new Handler();
    private FloatingActionButton fab;
    
    public CustomScrollAwareBehavior(Context context, AttributeSet attrs) {
        super();
    }
    
    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
                                       FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
        fab = child;
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL ||
                super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,
                        nestedScrollAxes);
    }
    
    
    
    Runnable showRunnable = new Runnable() {
        @Override
        public void run() {
            fab.show();
        }
    };
    
    
    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child,
                               View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
                dyUnconsumed);
         if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
            handler.removeCallbacks(showRunnable);
            handler.postDelayed(showRunnable,2000);
            child.hide();
        } 
        }
    }
    

    【讨论】:

    • 谢谢!!非常准确的答案,我会尽快尝试这个解决方案。
    猜你喜欢
    • 2015-03-28
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2015-03-11
    • 2023-03-15
    相关资源
    最近更新 更多