Android利用属性动画实现优酷菜单
程序员文章站
2022-03-23 13:40:57
利用属性动画实现优酷菜单,供大家参考,具体内容如下
布局文件
利用属性动画实现优酷菜单,供大家参考,具体内容如下
布局文件
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <relativelayout android:layout_width="280dip" android:layout_height="140dip" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:id="@+id/level3" android:background="@drawable/level3" > <imagebutton android:id="@+id/c1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_marginbottom="6dip" android:layout_marginleft="12dip" android:background="@drawable/channel1" /> <imagebutton android:id="@+id/c2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/c1" android:layout_marginbottom="12dip" android:layout_marginleft="28dip" android:background="@drawable/channel2" /> <imagebutton android:id="@+id/c3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/c2" android:layout_marginbottom="6dip" android:layout_marginleft="8dip" android:layout_torightof="@id/c2" android:background="@drawable/channel3" /> <imagebutton android:id="@+id/c4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_margin="6dip" android:background="@drawable/channel4" /> <imagebutton android:id="@+id/c5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/c6" android:layout_marginbottom="6dip" android:layout_marginright="8dip" android:layout_toleftof="@+id/c6" android:background="@drawable/channel5" /> <imagebutton android:id="@+id/c6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/c7" android:layout_marginbottom="12dip" android:layout_marginright="28dip" android:layout_alignparentright="true" android:background="@drawable/channel6" /> <imagebutton android:id="@+id/c7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_marginbottom="6dip" android:layout_marginright="12dip" android:layout_alignparentright="true" android:background="@drawable/channel7" /> </relativelayout> <relativelayout android:layout_width="180dip" android:layout_height="90dip" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:id="@+id/level2" android:background="@drawable/level2" > <imagebutton android:id="@+id/search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_margin="10dip" android:background="@drawable/icon_search" /> <imagebutton android:id="@+id/menu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_margin="6dip" android:background="@drawable/icon_menu" /> <imagebutton android:id="@+id/myyouku" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_margin="10dip" android:background="@drawable/icon_myyouku" /> </relativelayout> <relativelayout android:layout_width="100dip" android:layout_height="50dip" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:background="@drawable/level1" > <imagebutton android:id="@+id/home" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:background="@drawable/icon_home" /> </relativelayout> </relativelayout>
核心代码
package com.example.uumusic.menu; import android.content.context; import android.view.view; import android.widget.imagebutton; import android.widget.relativelayout; import android.widget.toast; import com.example.uumusic.r; import com.example.uumusic.fragment.base.basepager; import com.example.uumusic.utils.tools; import butterknife.butterknife; import butterknife.injectview; /** * created by administrator on 2017.06.07.0007. */ public class youkumenu extends basepager { @injectview(r.id.c1) imagebutton c1; @injectview(r.id.c2) imagebutton c2; @injectview(r.id.c3) imagebutton c3; @injectview(r.id.c4) imagebutton c4; @injectview(r.id.c5) imagebutton c5; @injectview(r.id.c6) imagebutton c6; @injectview(r.id.c7) imagebutton c7; @injectview(r.id.level3) relativelayout level3; @injectview(r.id.search) imagebutton search; @injectview(r.id.menu) imagebutton menu; @injectview(r.id.myyouku) imagebutton myyouku; @injectview(r.id.level2) relativelayout level2; @injectview(r.id.home) imagebutton home; private boolean isleve12 = true; private boolean isleve13 = true; public youkumenu(context context) { super(context); } @override public view initview() { view view = view.inflate(mcontext, r.layout.fragment_youku, null); butterknife.inject(this,view); return view; } @override public void initdata() { //为按钮设置点击事件 home.setonclicklistener(new myonclicklisetner()); menu.setonclicklistener(new myonclicklisetner()); } class myonclicklisetner implements view.onclicklistener{ @override public void onclick(view v) { switch (v.getid()){ case r.id.home: //当点击home按钮时,开始进行动画的效果 if (isleve12){ isleve12 = false; tools.hide(level2); if (isleve13){ isleve13 = false; tools.hide(level3,200); } }else { isleve12 = true; tools.show(level2); } break; case r.id.menu: if (isleve13){ isleve13 = false; tools.hide(level3); }else { isleve13 = true; tools.show(level3); } break; } } } }
动画工具类
package com.example.uumusic.utils; import android.animation.objectanimator; import android.content.context; import android.view.view; import android.view.viewgroup; import android.view.animation.rotateanimation; import android.widget.relativelayout; /** * created by administrator on 2017.06.07.0007. */ public class tools { //隐藏布局 public static void hide(viewgroup view) { hide(view, 0); } //显示布局 public static void show(viewgroup view) { //使用属性动画实现菜单的旋转 objectanimator animator = objectanimator.offloat(view,"rotation",180,360); //设置动画时长 animator.setduration(300); animator.start(); view.setpivotx(view.getwidth()/2); view.setpivoty(view.getheight()); } //延迟隐藏 public static void hide(viewgroup view, int i) { objectanimator animator = objectanimator.offloat(view,"rotation",0,180); //设置动画时长 animator.setduration(300); //设置延迟 animator.setstartdelay(i); animator.start(); view.setpivotx(view.getwidth()/2); view.setpivoty(view.getheight()); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。