欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)爆炸式菜單按鈕彈出效果

 更新時間:2018年05月14日 15:01:08   作者:LIFE_R  
這篇文章主要介紹了Android實現(xiàn)爆炸式菜單按鈕彈出效果,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

最近項目要使用到點擊一個按鈕彈出多個按鈕的效果,在試了幾個類庫后感覺不是很理想,所以自己代碼實現(xiàn)了一個,下圖所示:

實現(xiàn)原理很簡單,就是利用android原聲動畫效果,當(dāng)點擊中心按鈕時彈出其余按鈕。閑話少敘,代碼如下。

第一步:activity_main.xml 很簡單,也就是五個相同位置的按鈕

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >
 <ImageButton
  android:id="@+id/button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:layout_margin="10dp"
  android:src="@drawable/im" 
  android:background="@android:color/transparent"/>
 <ImageButton
  android:id="@+id/button1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:layout_margin="10dp"
  android:visibility="invisible" 
  android:src="@drawable/i"
  android:background="@android:color/transparent"/>
 <ImageButton
  android:id="@+id/button2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:layout_margin="10dp"
  android:src="@drawable/ii"
  android:visibility="invisible" 
  android:background="@android:color/transparent"/>
 <ImageButton
  android:id="@+id/button3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:layout_margin="10dp"
  android:src="@drawable/iii"
  android:visibility="invisible"
  android:background="@android:color/transparent"/>
 <ImageButton
  android:id="@+id/button4"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:layout_margin="10dp"
  android:src="@drawable/iiii"
  android:visibility="invisible"
  android:background="@android:color/transparent" />

</RelativeLayout>

第二步:MainActivity

package com.example.boombuttons;
import java.util.ArrayList;
public class MainActivity extends Activity implements OnClickListener{
 // 中心按鈕
 private ImageButton button;
 // 四個子按鈕
 private ImageButton button1;
 private ImageButton button2; 
 private ImageButton button3; 
 private ImageButton button4;
 // 子按鈕列表
 private List<ImageButton> buttonItems = new ArrayList<ImageButton>(3);
 // 標(biāo)識當(dāng)前按鈕彈出與否,1代表已經(jīng)未彈出,-1代表已彈出
 private int flag = 1;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // 實例化按鈕并設(shè)立監(jiān)聽
  button = (ImageButton)findViewById(R.id.button);
  button.setOnClickListener(this);
  button1 = (ImageButton)findViewById(R.id.button1);
  button2 = (ImageButton)findViewById(R.id.button2);
  button3 = (ImageButton)findViewById(R.id.button3);
  button4 = (ImageButton)findViewById(R.id.button4);
  // 將子按鈕們加入列表中
  buttonItems.add(button1);
  buttonItems.add(button2);
  buttonItems.add(button3);
  buttonItems.add(button4); 
 }
 /**
  * 按鈕移動動畫
  * @params 子按鈕列表
  * @params 彈出時圓形半徑radius
  */
 public void buttonAnimation(List<ImageButton> buttonList,int radius){
  for(int i=0;i<buttonList.size();i++){
   ObjectAnimator objAnimatorX;
   ObjectAnimator objAnimatorY;
   ObjectAnimator objAnimatorRotate;
   // 將按鈕設(shè)為可見
   buttonList.get(i).setVisibility(View.VISIBLE);
   // 按鈕在X、Y方向的移動距離
   float distanceX = (float) (flag*radius*(Math.cos(Util.getAngle(buttonList.size(),i))));
   float distanceY = -(float) (flag*radius*(Math.sin(Util.getAngle(buttonList.size(),i))));
   // X方向移動
   objAnimatorX = ObjectAnimator.ofFloat(buttonList.get(i), "x", buttonList.get(i).getX(),buttonList.get(i).getX()+distanceX);
   objAnimatorX.setDuration(200);
   objAnimatorX.setStartDelay(100);
   objAnimatorX.start();
   // Y方向移動
   objAnimatorY = ObjectAnimator.ofFloat(buttonList.get(i), "y", buttonList.get(i).getY(),buttonList.get(i).getY()+distanceY);
   objAnimatorY.setDuration(200);
   objAnimatorY.setStartDelay(100);
   objAnimatorY.start();
   // 按鈕旋轉(zhuǎn)
   objAnimatorRotate = ObjectAnimator.ofFloat(buttonList.get(i), "rotation", 0, 360);
   objAnimatorRotate.setDuration(200);
   objAnimatorY.setStartDelay(100);
   objAnimatorRotate.start();
   if(flag==-1){
    objAnimatorX.addListener(new AnimatorListener() {
     @Override
     public void onAnimationStart(Animator animation) {
      // TODO Auto-generated method stub
     }
     @Override
     public void onAnimationRepeat(Animator animation) {
      // TODO Auto-generated method stub
     }
     @Override
     public void onAnimationEnd(Animator animation) {
      // TODO Auto-generated method stub
      // 將按鈕設(shè)為可見
      for (int i = 0; i < buttonItems.size(); i++) {
       buttonItems.get(i).setVisibility(View.INVISIBLE);
      }
     }
     @Override
     public void onAnimationCancel(Animator animation) {
      // TODO Auto-generated method stub
     }
    });
   }
  }
 }
}

第三步:Util.java 工具類,寫了一個靜態(tài)方法,用于通過按鈕個數(shù)和按鈕在列表中的索引計算其彈出角度。

public class Util {
 /**
  * 返回每個按鈕應(yīng)該出現(xiàn)的角度(弧度單位)
  * @param index
  * @return double 角度(弧度單位)
  */
 public static double getAngle(int total,int index){
  return Math.toRadians(90/(total-1)*index+90);
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android仿網(wǎng)易云音樂播放界面

    Android仿網(wǎng)易云音樂播放界面

    這篇文章主要為大家詳細(xì)介紹了Android仿網(wǎng)易云音樂播放界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Android 8.0 讀取內(nèi)部和外部存儲以及外置SDcard的方法

    Android 8.0 讀取內(nèi)部和外部存儲以及外置SDcard的方法

    今天小編就為大家分享一篇Android 8.0 讀取內(nèi)部和外部存儲以及外置SDcard的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 利用Jetpack Compose繪制可愛的天氣動畫

    利用Jetpack Compose繪制可愛的天氣動畫

    Jetpack Compose是用于構(gòu)建原生Android UI的現(xiàn)代工具包。Jetpack Compose使用更少的代碼,強(qiáng)大的工具和直觀的Kotlin API,簡化并加速了Android上的UI開發(fā)。本文將利用Jetpack Compose繪制可愛的天氣動畫,感興趣的可以了解一下
    2022-01-01
  • ViewPager實現(xiàn)漂亮的引導(dǎo)頁

    ViewPager實現(xiàn)漂亮的引導(dǎo)頁

    這篇文章主要為大家詳細(xì)介紹了ViewPager實現(xiàn)漂亮的引導(dǎo)頁,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • ViewPager+Fragment實現(xiàn)側(cè)滑導(dǎo)航欄

    ViewPager+Fragment實現(xiàn)側(cè)滑導(dǎo)航欄

    這篇文章主要為大家詳細(xì)介紹了ViewPager+Fragment實現(xiàn)側(cè)滑導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android?Flutter實現(xiàn)"斑馬紋"背景的示例代碼

    Android?Flutter實現(xiàn)"斑馬紋"背景的示例代碼

    本文將通過實現(xiàn)一個canvas繪制斑馬紋類。使用Stack布局,將斑馬紋放在下方作為背景板,需要展示的內(nèi)容在上方。從而實現(xiàn)?“斑馬紋”背景,感興趣的可以了解一下
    2022-06-06
  • Android升級gradle 后引入aar包報錯解決

    Android升級gradle 后引入aar包報錯解決

    這篇文章主要為大家介紹了Android升級gradle 后引入aar包報錯解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Android 根據(jù)EditText搜索框ListView動態(tài)顯示數(shù)據(jù)

    Android 根據(jù)EditText搜索框ListView動態(tài)顯示數(shù)據(jù)

    這篇文章主要介紹了Android 根據(jù)EditText搜索框ListView動態(tài)顯示數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Android實現(xiàn)滾動刻度尺效果

    Android實現(xiàn)滾動刻度尺效果

    本篇文章主要介紹了Android實現(xiàn)滾動刻度尺效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Android如何獲取APP啟動時間

    Android如何獲取APP啟動時間

    大家好,本篇文章主要講的是Android如何獲取APP啟動時間,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12

最新評論