国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 帶有彈出收縮動畫的扇形菜單實例

2019-12-12 02:35:19
字體:
來源:轉載
供稿:網友

最近試著做了個Android 帶有彈出收縮動畫的扇形菜單,留個筆記記錄一下。

效果如下

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;    }  }  //打開扇形菜單的屬性動畫, dp為半徑長度  private void showOpenAnim(int dp) {    textView1.setVisibility(View.VISIBLE);    textView2.setVisibility(View.VISIBLE);    //for循環來開始小圖標的出現動畫    for (int i = 0; i < textViews.size(); i++) {      AnimatorSet set = new AnimatorSet();      //標題1與x軸負方向角度為20°,標題2為100°,轉換為弧度      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) {          //菜單狀態置打開          isMenuOpen = true;        }        @Override        public void onAnimationCancel(Animator animation) {        }        @Override        public void onAnimationRepeat(Animator animation) {        }      });    }    //轉動加號大圖標本身45°    ObjectAnimator rotate = ObjectAnimator.ofFloat(imgPublish, "rotation", 0, 90).setDuration(300);    rotate.setInterpolator(new BounceInterpolator());    rotate.start();  }  //關閉扇形菜單的屬性動畫,參數與打開時相反  private void showCloseAnim(int dp) {    //for循環來開始小圖標的出現動畫    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);          //菜單狀態置關閉          isMenuOpen = false;        }        @Override        public void onAnimationCancel(Animator animation) {        }        @Override        public void onAnimationRepeat(Animator animation) {        }      });    }    //轉動加號大圖標本身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="標題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="標題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開罐頭―――快速打造扇形衛星菜單

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 双城市| 乐陵市| 周宁县| 思南县| 峨边| 兰考县| 个旧市| 方城县| 武定县| 客服| 洪雅县| 新和县| 金华市| 柘荣县| 五寨县| 资阳市| 河南省| 广昌县| 宜君县| 益阳市| 峡江县| 镇雄县| 龙井市| 磴口县| 北海市| 合阳县| 绥江县| 酒泉市| 霸州市| 呼玛县| 漯河市| 正阳县| 石城县| 宿松县| 廊坊市| 孟州市| 宣汉县| 清涧县| 商洛市| 桐梓县| 石屏县|