zoukankan      html  css  js  c++  java
  • FloatActionButton弹出菜单

    浮动按钮的弹出菜单动画

    floatActionButton

    将几个按钮重叠摆放,使用ValueAnimator更新按钮的坐标实现。

    布局

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/float_btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="20dp"
            android:layout_marginEnd="10dp"
            android:src="@mipmap/fav_2"
            app:elevation="5dp"
            app:fabSize="normal" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/float_btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="20dp"
            android:layout_marginEnd="10dp"
            android:src="@mipmap/idea"
            app:elevation="5dp"
            app:fabSize="normal" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/float_btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="20dp"
            android:layout_marginEnd="10dp"
            android:src="@mipmap/faxian"
            app:elevation="5dp"
            app:fabSize="normal" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/float_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_marginBottom="20dp"
            android:layout_marginEnd="10dp"
            android:src="@mipmap/menu"
            app:elevation="5dp"
            app:fabSize="normal" />
    
    </FrameLayout>
    

    控制

    private FloatingActionButton actionButton, actionButton1, actionButton2, actionButton3;
    private boolean menuOpen = false;
    private void showMenu() {
            menuOpen = true;
            int x = (int) actionButton.getX();
            int y = (int) actionButton.getY();
            ValueAnimator v1 = ValueAnimator.ofInt(x, x - DISTANCE);
            v1.setDuration(500);
            v1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int l = (int) animation.getAnimatedValue();
                    int t = (int) actionButton1.getY();
                    int r = actionButton1.getWidth() + l;
                    int b = actionButton1.getHeight() + t;
                    actionButton1.layout(l, t, r, b);
                }
            });
            ValueAnimator v2x = ValueAnimator.ofInt(x, x - DISTANCE2);
            ValueAnimator v2y = ValueAnimator.ofInt(y, y - DISTANCE2);
            v2x.setDuration(500).addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int l = (int) animation.getAnimatedValue();
                    int t = (int) actionButton2.getY();
                    int r = actionButton2.getWidth() + l;
                    int b = actionButton2.getHeight() + t;
                    actionButton2.layout(l, t, r, b);
                }
            });
            v2y.setDuration(500).addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int t = (int) animation.getAnimatedValue();
                    int l = (int) actionButton2.getX();
                    int r = actionButton2.getWidth() + l;
                    int b = actionButton2.getHeight() + t;
                    actionButton2.layout(l, t, r, b);
                }
            });
            ValueAnimator v3 = ValueAnimator.ofInt(y, y - DISTANCE);
            v3.setDuration(500).addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int t = (int) animation.getAnimatedValue();
                    int l = (int) actionButton3.getX();
                    int r = actionButton3.getWidth() + l;
                    int b = actionButton3.getHeight() + t;
                    actionButton3.layout(l, t, r, b);
                }
            });
            v1.start();
            v2x.start();
            v2y.start();
            v3.start();
        }
    
        private void hideMenu() {
            menuOpen = false;
            int x = (int) actionButton1.getX();
            ValueAnimator v1 = ValueAnimator.ofInt(x, (int) actionButton.getX());
            v1.setDuration(500);
            v1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int l = (int) animation.getAnimatedValue();
                    int t = (int) actionButton1.getY();
                    int r = actionButton1.getWidth() + l;
                    int b = actionButton1.getHeight() + t;
                    actionButton1.layout(l, t, r, b);
                }
            });
            x = (int) actionButton2.getX();
            int y = (int) actionButton2.getY();
            ValueAnimator v2x = ValueAnimator.ofInt(x, (int) actionButton.getX());
            ValueAnimator v2y = ValueAnimator.ofInt(y, (int) actionButton.getY());
            v2x.setDuration(500).addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int l = (int) animation.getAnimatedValue();
                    int t = (int) actionButton2.getY();
                    int r = actionButton2.getWidth() + l;
                    int b = actionButton2.getHeight() + t;
                    actionButton2.layout(l, t, r, b);
                }
            });
            v2y.setDuration(500).addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int t = (int) animation.getAnimatedValue();
                    int l = (int) actionButton2.getX();
                    int r = actionButton2.getWidth() + l;
                    int b = actionButton2.getHeight() + t;
                    actionButton2.layout(l, t, r, b);
                }
            });
            y = (int) actionButton3.getY();
            ValueAnimator v3 = ValueAnimator.ofInt(y, (int) actionButton.getY());
            v3.setDuration(500).addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int t = (int) animation.getAnimatedValue();
                    int l = (int) actionButton3.getX();
                    int r = actionButton3.getWidth() + l;
                    int b = actionButton3.getHeight() + t;
                    actionButton3.layout(l, t, r, b);
                }
            });
            v1.start();
            v2x.start();
            v2y.start();
            v3.start();
        }
    
  • 相关阅读:
    16 IO流(十三)——Object流 序列化与反序列化
    java.IO.EOFException异常
    15 IO流(十二)——数据流Data InputStream/OutputStream 未学会
    14 IO流(十一)——装换流InputStreamReader与OutputStreamWriter
    13 IO流(十)——BufferedReader/BufferedWriter 装饰流
    12 IO流(九)——装饰流 BufferedInputStream/OutputStream
    11 IO流(八)——装饰器设计模式,Filter装饰流
    10 IO流(七)——copy方法封装、关闭流方法封装的两种方式以及try...with...resource
    09 IO流(六)——ByteArray字节流、流对接
    新手学习PHP的避雷针,这些坑在PHP开发中就别跳了
  • 原文地址:https://www.cnblogs.com/jiy-for-you/p/6898477.html
Copyright © 2011-2022 走看看