Android利用ObjectAnimator实现ArcMenu

作者: 日期:2019-11-13

本文介绍利用objectanimator简单地实现arcmenu,直接使用本文的arcmenu类即可快捷地实现菜单功能。 



 private int[] imageres = {r.id.img_menu, r.id.img_menu1, r.id.img_menu2, r.id.img_menu3, 
r.id.img_menu4, r.id.img_menu5};
private arcmenu arcmenu;
//初始化,参数为资源图片id
 arcmenu = new arcmenu;
//点击事件,这边使用了annotation,直接使用findviewbyid然后设置监听事件也可以
 @click
 public void img_menu {
 mylog.d;
 arcmenu.switchmenu;
 @click
 public void img_menu1 {
 arcmenu.clickitem;
 mylog.d;
 @click
 public void img_menu2 {
 arcmenu.clickitem;
 mylog.d;
 @click
 public void img_menu3 {
 arcmenu.clickitem;
 mylog.d;
 @click
 public void img_menu4 {
 arcmenu.clickitem;
 mylog.d;
 @click
 public void img_menu5 {
 arcmenu.clickitem;
 mylog.d;

 relativelayout xmlns:凯发体育官网android="http://schemas.android.com/apk/res/android"
 android:id="@+id/widget33"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#262a34" 
 imageview
 android:id="@+id/img_menu"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_alignparentbottom="true"
 android:layout_alignparentright="true"
 android:layout_marginright="10dp"
 android:layout_marginbottom="10dp"
 android:src="@drawable/menu_add" / 
 imageview
 android:id="@+id/img_menu1"
 android:visibility="gone"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_alignparentbottom="true"
 android:layout_alignparentright="true"
 android:layout_marginright="10dp"
 android:layout_marginbottom="10dp"
 android:src="@drawable/float_on" / 
 imageview
 android:id="@+id/img_menu2"
 android:visibility="gone"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_alignparentbottom="true"
 android:layout_alignparentright="true"
 android:layout_marginright="10dp"
 android:layout_marginbottom="10dp"
 android:src="@drawable/float_on" / 
 imageview
 android:id="@+id/img_menu3"
 android:visibility="gone"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_alignparentbottom="true"
 android:layout_alignparentright="true"
 android:layout_marginright="10dp"
 android:layout_marginbottom="10dp"
 android:src="@drawable/float_on" / 
 imageview
 android:id="@+id/img_menu4"
 android:visibility="gone"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_alignparentbottom="true"
 android:layout_alignparentright="true"
 android:layout_marginright="10dp"
 android:layout_marginbottom="10dp"
 android:src="@drawable/float_on" / 
 imageview
 android:id="@+id/img_menu5"
 android:visibility="gone"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_alignparentbottom="true"
 android:layout_alignparentright="true"
 android:layout_marginright="10dp"
 android:layout_marginbottom="10dp"
 android:src="@drawable/float_on" / 
 /relativelayout 

 



 public class arcmenu {
 private activity context;
 private int[] imageres;
 private list imageview imageviewlist = new arraylist ;
 private boolean isshowmenu = false;
 int radius = 180;
 double angle;
 public arcmenu {
 angle = math.pi / 2 / ;
 radius = tool.dip2px;
 this.context = context;
 this.imageres = imageres;
 for  {
 imageview imageview =  context.findviewbyid;
 imageviewlist.add;


 private void openmenu {
 isshowmenu = true;
 setitemvisible;
 objectanimator animator1;
 objectanimator animator2;
 list objectanimator objectanimators = new arraylist ;
 animatorset set = new animatorset;
 for  {
 animator1 = objectanimator.offloat, 
"translationx",  )));
 animator2 = objectanimator.offloat, "translationy", 
 )));
 objectanimators.add;
 objectanimators.add;
 for ; i++) {
 set.playtogether);
 set.setduration;
 set.start;
 //第0个图标,菜单图标,加入动画
 objectanimator.offloat,"rotation",0,135f).setduration.start;

 private void closemenu {
 isshowmenu = false;
 objectanimator animator1 = null;
 objectanimator animator2;
 list objectanimator objectanimators = new arraylist ;
 animatorset set = new animatorset;
 for  {
 animator1 = objectanimator.offloat, "translationx", 0);
 animator2 = objectanimator.offloat, "translationy", 0);
 objectanimators.add;
 objectanimators.add;
 animator1.addlistener {
 @override
 public void onanimationend {
 setitemvisible;
 super.onanimationend;
 for ; i++) {
 set.playtogether);
 set.setduration;
 set.start;
 objectanimator.offloat,"rotation",135f,0).setduration.start;

 public void switchmenu {
 if  {
 closemenu;
 } else {
 openmenu;
 } 

 public void clickitem {
 setitemvisible;
 closemenu;
 private void setitemvisible {
 for  {
 if  {
 imageviewlist.get.setvisibility;
 } else {
 imageviewlist.get.setvisibility;
首页
电话
短信
联系