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",translationX),ObjectAnimator.offloat(btnItem,"translationY",translationY),"scaleX",1),"scaleY","Alpha",1) ); set.setInterpolator(new BounceInterpolator()); set.setDuration(500).start(); } /** * Item关闭动画 * * @param btnItem * @param index * @param total * @param radius */ private voID btnItemCloseAnimator(VIEw btnItem,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,0),translationY,1f,0f),0f)); set.setDuration(500).start(); if (btnItem.getVisibility() == VIEw.VISIBLE) { btnItem.setVisibility(VIEw.INVISIBLE); } }
item开启动画和关闭动画为一个逆过程,体现在x,y距离变化上。
x,y的距离开启时距离逐渐增长
ObjectAnimator.offloat(btnItem,
这里要注意下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,5,300); btnItemStartAnimator(btnItem2,1,300); btnItemStartAnimator(btnItem3,2,300); btnItemStartAnimator(btnItem4,3,300); btnItemStartAnimator(btnItem5,4,300); } else { //此时menu是打开的 isMenuOpen = false; btnItemCloseAnimator(btnItem1,300); btnItemCloseAnimator(btnItem2,300); btnItemCloseAnimator(btnItem3,300); btnItemCloseAnimator(btnItem4,300); btnItemCloseAnimator(btnItem5,300); } } /** * 关闭动画 * * @param btnItem * @param index * @param total * @param radius */ private voID btnItemCloseAnimator(VIEw btnItem,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,1) ); set.setInterpolator(new BounceInterpolator()); set.setDuration(500).start(); }}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android 动态菜单实现实例代码全部内容,希望文章能够帮你解决Android 动态菜单实现实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)