Animator 实现 FloatActionBar 点击弹出多个FloatActionBar

来源:互联网 发布:qq群网络原因上传失败 编辑:程序博客网 时间:2024/06/07 20:25

首先看下效果吧..

这里写图片描述

就是上面的一个效果.

实现方法(这里使用ImageView 代替 方便)

首先通过RelativeLayout 定义三个 ImageView .最上层的显示, 其余两个隐藏. 在弹出动画开始的时候显示出来. 在缩回动画里面隐藏

layout

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.msh.animation.MainActivity">    <ImageView        android:id="@+id/iv1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:src="@mipmap/ic_launcher" />    <ImageView        android:id="@+id/iv2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:src="@mipmap/ic_launcher"        android:visibility="gone" />    <ImageView        android:id="@+id/iv3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:src="@mipmap/ic_launcher"        android:visibility="gone" /></RelativeLayout>

Activity

import android.animation.Animator;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.animation.BounceInterpolator;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    ImageView iv1, iv2, iv3, iv4;    boolean isShow = false;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        iv1 = (ImageView) findViewById(R.id.iv1);        iv1.setOnClickListener(this);        iv2 = (ImageView) findViewById(R.id.iv2);        iv3 = (ImageView) findViewById(R.id.iv3);        iv3.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.iv1:                Toast.makeText(this, "" + -(iv1.getHeight() + 50) * 2, Toast.LENGTH_SHORT).show();                AnimatorSet animatorSet = new AnimatorSet();                ObjectAnimator animator = ObjectAnimator.ofFloat(iv2, "translationY", iv1.getTranslationY(), -(iv1.getHeight() + 50) * 1);                ObjectAnimator animator2 = ObjectAnimator.ofFloat(iv3, "translationY", iv1.getTranslationY(), -(iv1.getHeight() + 50) * 2);                animatorSet.playTogether(animator, animator2);                animatorSet.setDuration(1000);                animatorSet.setInterpolator(new BounceInterpolator());                animatorSet.addListener(new Animator.AnimatorListener() {                    @Override                    public void onAnimationStart(Animator animation) {                        iv2.setVisibility(View.VISIBLE);                        iv3.setVisibility(View.VISIBLE);                    }                    @Override                    public void onAnimationEnd(Animator animation) {                    }                    @Override                    public void onAnimationCancel(Animator animation) {                    }                    @Override                    public void onAnimationRepeat(Animator animation) {                    }                });                animatorSet.start();                break;            case R.id.iv3:                Toast.makeText(this, "" + iv1.getHeight(), Toast.LENGTH_SHORT).show();                AnimatorSet animatorSet2 = new AnimatorSet();                ObjectAnimator animator3 = ObjectAnimator.ofFloat(iv2, "translationY", -(iv1.getHeight() + 50) * 1, 0);                ObjectAnimator animator4 = ObjectAnimator.ofFloat(iv3, "translationY", -(iv1.getHeight() + 50) * 2, 0);                animatorSet2.playTogether(animator3, animator4);                animatorSet2.setDuration(1000);                animatorSet2.setInterpolator(new BounceInterpolator());                animatorSet2.addListener(new Animator.AnimatorListener() {                    @Override                    public void onAnimationStart(Animator animation) {                    }                    @Override                    public void onAnimationEnd(Animator animation) {                        iv2.setVisibility(View.GONE);                        iv3.setVisibility(View.GONE);                    }                    @Override                    public void onAnimationCancel(Animator animation) {                    }                    @Override                    public void onAnimationRepeat(Animator animation) {                    }                });                animatorSet2.start();                break;        }    }}

就两个类. 都不放Demo 了. 没有其他任何的东西…

0 0
原创粉丝点击