Android 动态菜单实现实例代码
程序员文章站
2024-02-22 08:42:10
android 动态菜单
先上效果图
比较简单,主要就是属性动画的使用和坐标角度的小细节。
实现
实现效果:
图标按照路径一路缩放渐变过来即可。
核心代码...
android 动态菜单
先上效果图
比较简单,主要就是属性动画的使用和坐标角度的小细节。
实现
实现效果:
图标按照路径一路缩放渐变过来即可。
核心代码
/** * item开启动画 * * @param btnitem * @param index * @param total * @param radius */ private void btnitemstartanimator(view btnitem, int index, int total, int radius) { if (btnitem.getvisibility() != view.visible) { btnitem.setvisibility(view.visible); } double degree = math.toradians(90) / (total - 1) * index;//math中根据度数得到弧度值的函数 int translationx = -(int) (radius * math.sin(degree)); int translationy = -(int) (radius * math.cos(degree)); animatorset set = new animatorset(); //实现平移缩放和透明动画 set.playtogether( objectanimator.offloat(btnitem, "translationx", 0, translationx), objectanimator.offloat(btnitem, "translationy", 0, translationy), objectanimator.offloat(btnitem, "scalex", 0, 1), objectanimator.offloat(btnitem, "scaley", 0, 1), objectanimator.offloat(btnitem, "alpha", 0, 1) ); set.setinterpolator(new bounceinterpolator()); set.setduration(500).start(); } /** * item关闭动画 * * @param btnitem * @param index * @param total * @param radius */ private void btnitemcloseanimator(view btnitem, int index, int total, int radius) { double degree = math.pi * index / ((total - 1) * 2); int translationx = -(int) (radius * math.sin(degree)); int translationy = -(int) (radius * math.cos(degree)); animatorset set = new animatorset(); //包含平移、缩放和透明度动画 set.playtogether( objectanimator.offloat(btnitem, "translationx", translationx, 0), objectanimator.offloat(btnitem, "translationy", translationy, 0), objectanimator.offloat(btnitem, "scalex", 1f, 0f), objectanimator.offloat(btnitem, "scaley", 1f, 0f), objectanimator.offloat(btnitem, "alpha", 1f, 0f)); set.setduration(500).start(); if (btnitem.getvisibility() == view.visible) { btnitem.setvisibility(view.invisible); } }
item开启动画和关闭动画为一个逆过程,体现在x,y距离变化上。
x,y的距离开启时距离逐渐增长
objectanimator.offloat(btnitem, "translationx", 0, translationx), objectanimator.offloat(btnitem, "translationy", 0, translationy),
这里要注意下sin这些弧度的计算,可以使用math.toradins(数字)
double degree = math.toradians(90) / (total - 1) * index;//math中根据度数得到弧度值的函数 int translationx = -(int) (radius * math.sin(degree));
或者使用pi=180°来折算
double degree = math.pi * index / ((total - 1) * 2); int translationx = -(int) (radius * math.sin(degree));
实例代码:
package xsf.customview; import android.animation.animatorset; import android.animation.objectanimator; import android.view.view; import android.view.animation.bounceinterpolator; import android.widget.button; import android.widget.toast; import xsf.customview.base.baseactvity; public class statelliteactivity extends baseactvity { private button btnmenu, btnitem1, btnitem2, btnitem3, btnitem4, btnitem5; private boolean ismenuopen = false; @override protected int setlayoutresourceid() { return r.layout.activity_statellite; } @override protected void initview() { btnmenu = (button) findviewbyid(r.id.btnmenu); btnmenu.setonclicklistener(this); btnitem1 = (button) findviewbyid(r.id.btnitem1); btnitem1.setonclicklistener(this); btnitem2 = (button) findviewbyid(r.id.btnitem2); btnitem2.setonclicklistener(this); btnitem3 = (button) findviewbyid(r.id.btnitem3); btnitem3.setonclicklistener(this); btnitem4 = (button) findviewbyid(r.id.btnitem4); btnitem4.setonclicklistener(this); btnitem5 = (button) findviewbyid(r.id.btnitem5); btnitem5.setonclicklistener(this); } @override public void onclick(view v) { btnmenu.requestfocus(); switch (v.getid()) { case r.id.btnmenu: showitemanimator(); break; case r.id.btnitem1: toast.maketext(statelliteactivity.this, "点击了item1", toast.length_short).show(); break; case r.id.btnitem2: toast.maketext(statelliteactivity.this, "点击了item2", toast.length_short).show(); break; case r.id.btnitem3: toast.maketext(statelliteactivity.this, "点击了item3", toast.length_short).show(); break; case r.id.btnitem4: toast.maketext(statelliteactivity.this, "点击了item4", toast.length_short).show(); break; case r.id.btnitem5: toast.maketext(statelliteactivity.this, "点击了item5", toast.length_short).show(); break; } } private void showitemanimator() { if (!ismenuopen) { //此时menu是关闭的 ismenuopen = true; btnitemstartanimator(btnitem1, 0, 5, 300); btnitemstartanimator(btnitem2, 1, 5, 300); btnitemstartanimator(btnitem3, 2, 5, 300); btnitemstartanimator(btnitem4, 3, 5, 300); btnitemstartanimator(btnitem5, 4, 5, 300); } else { //此时menu是打开的 ismenuopen = false; btnitemcloseanimator(btnitem1, 0, 5, 300); btnitemcloseanimator(btnitem2, 1, 5, 300); btnitemcloseanimator(btnitem3, 2, 5, 300); btnitemcloseanimator(btnitem4, 3, 5, 300); btnitemcloseanimator(btnitem5, 4, 5, 300); } } /** * 关闭动画 * * @param btnitem * @param index * @param total * @param radius */ private void btnitemcloseanimator(view btnitem, int index, int total, int radius) { double degree = math.pi * index / ((total - 1) * 2); int translationx = -(int) (radius * math.sin(degree)); int translationy = -(int) (radius * math.cos(degree)); animatorset set = new animatorset(); //包含平移、缩放和透明度动画 set.playtogether( objectanimator.offloat(btnitem, "translationx", translationx, 0), objectanimator.offloat(btnitem, "translationy", translationy, 0), objectanimator.offloat(btnitem, "scalex", 1f, 0f), objectanimator.offloat(btnitem, "scaley", 1f, 0f), objectanimator.offloat(btnitem, "alpha", 1f, 0f)); set.setduration(500).start(); if (btnitem.getvisibility() == view.visible) { btnitem.setvisibility(view.invisible); } } /** * 开启动画 * * @param btnitem * @param index * @param total * @param radius */ private void btnitemstartanimator(view btnitem, int index, int total, int radius) { if (btnitem.getvisibility() != view.visible) { btnitem.setvisibility(view.visible); } double degree = math.toradians(90) / (total - 1) * index;//math中根据度数得到弧度值的函数 int translationx = -(int) (radius * math.sin(degree)); int translationy = -(int) (radius * math.cos(degree)); animatorset set = new animatorset(); //实现平移缩放和透明动画 set.playtogether( objectanimator.offloat(btnitem, "translationx", 0, translationx), objectanimator.offloat(btnitem, "translationy", 0, translationy), objectanimator.offloat(btnitem, "scalex", 0, 1), objectanimator.offloat(btnitem, "scaley", 0, 1), objectanimator.offloat(btnitem, "alpha", 0, 1) ); set.setinterpolator(new bounceinterpolator()); set.setduration(500).start(); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: Java替换中使用正则表达式实现中间模糊匹配的方法
下一篇: Java编程实现非对称加密的方法详解