直接上布局
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
>
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:text="按钮"
android:textSize="16dp" />
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:background="#df8a8a"
android:padding="8dp"
android:text="洒"
android:textSize="16sp"
android:visibility="gone" />
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:background="#d4bb69"
android:padding="8dp"
android:text="家"
android:textSize="16sp"
android:visibility="gone" />
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:background="#76ce89"
android:padding="8dp"
android:text="卖"
android:textSize="16sp"
android:visibility="gone" />
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:background="#67b7dc"
android:padding="8dp"
android:text="蘑"
android:textSize="16sp"
android:visibility="gone" />
android:id="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:background="#d090e7"
android:padding="8dp"
android:text="菇"
android:textSize="16sp"
android:visibility="gone" />
然后是代码
package com.example.tanshemenu;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.TextView;
public class MainActivityextends AppCompatActivity {
private Buttonbutton =null;
private TextViewtv1,tv2,tv3,tv4,tv5 =null;
private boolean LockMenu =false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv4 = (TextView) findViewById(R.id.tv4);
tv5 = (TextView) findViewById(R.id.tv5);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!LockMenu) {
LockMenu =true;
openAnim(tv1,2,5,200);
openAnim(tv2,2,5,500);
openAnim(tv3,2,5,800);
openAnim(tv4,2,5,1100);
openAnim(tv5,2,5,1400);
}else {
LockMenu =false;
closeAnim(tv1,2,5,200);
closeAnim(tv2,2,5,500);
closeAnim(tv3,2,5,800);
closeAnim(tv4,2,5,1100);
closeAnim(tv5,2,5,1400);
}
}
});
}
/**
* 打开菜单
* view:动画控件
* index:第几个控件
* num:有几个控件
* radius:扇形半径
*/
public void openAnim(View view,int index,int num,int radius) {
if (view.getVisibility() != View.VISIBLE) {
view.setVisibility(View.VISIBLE);
}
double angle = Math.toRadians(180) / (num -1) * index;
int translationX = -(int) (radius * Math.cos(angle));
int translationY = -(int) (radius * Math.sin(angle));
ObjectAnimator one = ObjectAnimator.ofFloat(view,"translationX",0, translationX);
ObjectAnimator two = ObjectAnimator.ofFloat(view,"translationY",0, translationY);
ObjectAnimator three = ObjectAnimator.ofFloat(view,"rotation",0,360);
ObjectAnimator four = ObjectAnimator.ofFloat(view,"scaleX",0f,1f);
ObjectAnimator five = ObjectAnimator.ofFloat(view,"scaleY",0f,1f);
ObjectAnimator six = ObjectAnimator.ofFloat(view,"alpha",0f,1);
AnimatorSet set =new AnimatorSet();
set.playTogether(one, two, three, four, five, six);
set.setDuration(2000);
//回弹效果
set.setInterpolator(new BounceInterpolator());
set.start();
}
//关闭菜单
public void closeAnim(View view,int index,int num,int radius) {
if (view.getVisibility() != View.VISIBLE) {
view.setVisibility(View.VISIBLE);
}
double angle = Math.toRadians(180) / (num -1) * index;
Log.e("angle", angle +"");
int translationX = -(int) (radius * Math.cos(angle));
int translationY = -(int) (radius * Math.sin(angle));
ObjectAnimator one = ObjectAnimator.ofFloat(view,"translationX", translationX,0);
ObjectAnimator two = ObjectAnimator.ofFloat(view,"translationY", translationY,0);
ObjectAnimator three = ObjectAnimator.ofFloat(view,"rotation",0,360);
ObjectAnimator four = ObjectAnimator.ofFloat(view,"scaleX",1f,0f);
ObjectAnimator five = ObjectAnimator.ofFloat(view,"scaleY",1f,0f);
ObjectAnimator six = ObjectAnimator.ofFloat(view,"alpha",1f,0f);
AnimatorSet set =new AnimatorSet();
set.playTogether(one, two, three, four, five, six);
set.setDuration(1000);
//回弹效果
set.setInterpolator(new BounceInterpolator());
set.start();
}
}