Android 帶有彈出收縮動(dòng)畫(huà)的扇形菜單實(shí)例
最近試著做了個(gè)Android 帶有彈出收縮動(dòng)畫(huà)的扇形菜單,留個(gè)筆記記錄一下。
效果如下

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView imgPublish;
private TextView textView1;
private TextView textView2;
private boolean isMenuOpen = false;
private List<TextView> textViews = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgPublish = (ImageView) findViewById(R.id.img_publish);
textView1 = (TextView) findViewById(R.id.tv_1);
textView2 = (TextView) findViewById(R.id.tv_2);
textViews.add(textView1);
textViews.add(textView2);
imgPublish.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.img_publish:
if (!isMenuOpen) {
showOpenAnim(80);
imgPublish.setImageResource(R.mipmap.publish_select);
}else {
showCloseAnim(80);
imgPublish.setImageResource(R.mipmap.fabu);
}
break;
}
}
//打開(kāi)扇形菜單的屬性動(dòng)畫(huà), dp為半徑長(zhǎng)度
private void showOpenAnim(int dp) {
textView1.setVisibility(View.VISIBLE);
textView2.setVisibility(View.VISIBLE);
//for循環(huán)來(lái)開(kāi)始小圖標(biāo)的出現(xiàn)動(dòng)畫(huà)
for (int i = 0; i < textViews.size(); i++) {
AnimatorSet set = new AnimatorSet();
//標(biāo)題1與x軸負(fù)方向角度為20°,標(biāo)題2為100°,轉(zhuǎn)換為弧度
double a = -Math.cos(20 * Math.PI / 180 * (i * 2 + 1));
double b = -Math.sin(20 * Math.PI / 180 * (i * 2 + 1));
double x = a * dip2px(dp);
double y = b * dip2px(dp);
set.playTogether(
ObjectAnimator.ofFloat(textViews.get(i), "translationX", (float) (x * 0.25), (float) x),
ObjectAnimator.ofFloat(textViews.get(i), "translationY", (float) (y * 0.25), (float) y)
, ObjectAnimator.ofFloat(textViews.get(i), "alpha", 0, 1).setDuration(2000)
);
set.setInterpolator(new BounceInterpolator());
set.setDuration(500).setStartDelay(100);
set.start();
set.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
//菜單狀態(tài)置打開(kāi)
isMenuOpen = true;
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
//轉(zhuǎn)動(dòng)加號(hào)大圖標(biāo)本身45°
ObjectAnimator rotate = ObjectAnimator.ofFloat(imgPublish, "rotation", 0, 90).setDuration(300);
rotate.setInterpolator(new BounceInterpolator());
rotate.start();
}
//關(guān)閉扇形菜單的屬性動(dòng)畫(huà),參數(shù)與打開(kāi)時(shí)相反
private void showCloseAnim(int dp) {
//for循環(huán)來(lái)開(kāi)始小圖標(biāo)的出現(xiàn)動(dòng)畫(huà)
for (int i = 0; i < textViews.size(); i++) {
AnimatorSet set = new AnimatorSet();
double a = -Math.cos(20 * Math.PI / 180 * (i * 2 + 1));
double b = -Math.sin(20 * Math.PI / 180 * (i * 2 + 1));
double x = a * dip2px(dp);
double y = b * dip2px(dp);
set.playTogether(
ObjectAnimator.ofFloat(textViews.get(i), "translationX", (float) x, (float) (x * 0.25)),
ObjectAnimator.ofFloat(textViews.get(i), "translationY", (float) y, (float) (y * 0.25)),
ObjectAnimator.ofFloat(textViews.get(i), "alpha", 1, 0).setDuration(2000)
);
// set.setInterpolator(new AccelerateInterpolator());
set.setDuration(500);
set.start();
set.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
textView1.setVisibility(View.GONE);
textView2.setVisibility(View.GONE);
//菜單狀態(tài)置關(guān)閉
isMenuOpen = false;
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
//轉(zhuǎn)動(dòng)加號(hào)大圖標(biāo)本身45°
ObjectAnimator rotate = ObjectAnimator.ofFloat(imgPublish, "rotation", 0, 90).setDuration(300);
rotate.setInterpolator(new BounceInterpolator());
rotate.start();
}
private int dip2px(int value) {
float density = getResources()
.getDisplayMetrics().density;
return (int) (density * value + 0.5f);
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lina.animationapplication.MainActivity">
<TextView
android:id="@+id/tv_1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="bottom|end"
android:layout_marginBottom="40dp"
android:layout_marginRight="40dp"
android:gravity="center"
android:text="標(biāo)題1"
android:textColor="#ffffff"
android:visibility="gone"
android:background="@drawable/circle_purple"
/>
<TextView
android:id="@+id/tv_2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="bottom|end"
android:layout_marginBottom="40dp"
android:layout_marginRight="40dp"
android:gravity="center"
android:text="標(biāo)題2"
android:textColor="#ffffff"
android:visibility="gone"
android:background="@drawable/circle_orange"/>
<ImageView
android:id="@+id/img_publish"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="bottom|end"
android:layout_marginBottom="35dp"
android:layout_marginRight="35dp"
android:src="@mipmap/fabu"
/>
</FrameLayout>
circle_purple.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#5d2a89" /> </shape>
參考
Android開(kāi)罐頭———快速打造扇形衛(wèi)星菜單
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android編程實(shí)現(xiàn)仿優(yōu)酷圓盤(pán)旋轉(zhuǎn)菜單效果的方法詳解【附demo源碼下載】
- Android自定義控件之仿優(yōu)酷菜單
- Android仿優(yōu)酷圓形菜單學(xué)習(xí)筆記分享
- Android編程實(shí)現(xiàn)仿優(yōu)酷旋轉(zhuǎn)菜單效果(附demo源碼)
- Android菜單(動(dòng)畫(huà)菜單、360波紋菜單)
- Android實(shí)現(xiàn)360手機(jī)助手底部的動(dòng)畫(huà)菜單
- Android程序開(kāi)發(fā)之使用Design包實(shí)現(xiàn)QQ動(dòng)畫(huà)側(cè)滑效果和滑動(dòng)菜單導(dǎo)航
- Android利用屬性動(dòng)畫(huà)實(shí)現(xiàn)優(yōu)酷菜單
相關(guān)文章
Android中截取當(dāng)前屏幕圖片的實(shí)例代碼
該篇文章是說(shuō)明在A(yíng)ndroid手機(jī)或平板電腦中如何實(shí)現(xiàn)截取當(dāng)前屏幕的功能,并把截取的屏幕保存到SDCard中的某個(gè)目錄文件夾下面。實(shí)現(xiàn)的代碼如下:2013-08-08
Android平臺(tái)生成二維碼并實(shí)現(xiàn)掃描 & 識(shí)別功能
這篇文章主要介紹了Android平臺(tái)生成二維碼并實(shí)現(xiàn)掃描 & 識(shí)別功能的相關(guān)資料,需要的朋友可以參考下2016-06-06
Android開(kāi)發(fā)中模仿qq列表信息滑動(dòng)刪除功能
這篇文章主要介紹了Android開(kāi)發(fā)中模仿qq列表信息滑動(dòng)刪除功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
Android控件WebView實(shí)現(xiàn)完整截圖
這篇文章主要為大家詳細(xì)介紹了Android控件WebView實(shí)現(xiàn)完整截圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
淺析Flutter AbsorbPointer 與 IgnorePointer的區(qū)別
Flutter是Google一個(gè)新的用于構(gòu)建跨平臺(tái)的手機(jī)App的SDK。這篇文章主要介紹了Flutter AbsorbPointer 與 IgnorePointer的區(qū)別,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android webview手動(dòng)校驗(yàn)https證書(shū)(by 星空武哥)
有些時(shí)候由于A(yíng)ndroid系統(tǒng)的bug或者其他的原因,導(dǎo)致我們的webview不能驗(yàn)證通過(guò)我們的https證書(shū),最明顯的例子就是華為手機(jī)mate7升級(jí)到Android7.0后,手機(jī)有些網(wǎng)站打不開(kāi)了,而更新了webview的補(bǔ)丁后就沒(méi)問(wèn)題了2017-09-09
Android實(shí)現(xiàn)圓圈倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圓圈倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
如何使用Flutter開(kāi)發(fā)一款電影APP詳解
這篇文章主要給大家介紹了關(guān)于如何使用Flutter開(kāi)發(fā)一款電影APP的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Android自定義View實(shí)現(xiàn)折線(xiàn)圖效果
這篇文章介紹的是一個(gè)折線(xiàn)圖控件,用來(lái)顯示一系列的狀態(tài),并可以進(jìn)行滑動(dòng)。有需要的可以參考借鑒。2016-08-08

