炫酷彈射狀菜單

直接上布局

<?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:動(dòng)畫控件

* index:第幾個(gè)控件

* num:有幾個(gè)控件

* 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();

}

//關(guān)閉菜單

? ? 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();

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容