Android编程实现仿易信精美弹出框效果【附demo源码下载】
程序员文章站
2023-12-22 08:06:28
本文实例讲述了android编程实现仿易信精美弹出框效果。分享给大家供大家参考,具体如下:
截图:
动画效果介绍:
1.点击actionbar上“+”按钮,菜单从...
本文实例讲述了android编程实现仿易信精美弹出框效果。分享给大家供大家参考,具体如下:
截图:
动画效果介绍:
1.点击actionbar上“+”按钮,菜单从上方弹出(带反弹效果);
2.再次点击“+”、点击空白区域或者点击返回键,菜单向上方收起;
3.点击弹出框上的按钮时,该按钮放大,其它按钮缩小,菜单整体渐变退出。
主体代码:
1.activity.
/** * 仿易信动画弹出框 */ public class mainactivity extends actionbaractivity { //用于标记页面顶端位置 private view topview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); topview = findviewbyid(r.id.main_top); } private popupwindow popupwindow; private int line1deltay, line2deltay; //仿易信更多弹出框 private void showpopup() { if (popupwindow == null) { view contentview = layoutinflater.from(this).inflate(r.layout.yixin_pop_layout, null); //点击空白区域关闭 view blankview = contentview.findviewbyid(r.id.yixin_more_blank); view blankview2 = contentview.findviewbyid(r.id.yixin_more_blank2); inititems(contentview); //测量高度 int line2height = viewutils.getviewmeasuredheight(itemviews[0]); line1deltay = -getactionbarheight() - 40; line2deltay = line1deltay - line2height; blankview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { dismisspopup(); } }); blankview2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { dismisspopup(); } }); popupwindow = new popupwindow(contentview, screenutils.getscreenw(this), screenutils.getscreenh(this)); //随便设置一个drawable作为背景 popupwindow.setbackgrounddrawable(new colordrawable()); } if (!popupwindow.isshowing()) { popupwindow.showasdropdown(topview, 0, 0); for (int i = 0; i < itemviews.length; i++) { if (i < 3) { //第一行 itemviews[i].startanimation(animationhelper.createpopupanimin(this, line1deltay)); } else { //第二行 itemviews[i].startanimation(animationhelper.createpopupanimin(this, line2deltay)); } } popupwindow.getcontentview().startanimation(animationhelper.createpopupbgfadeinanim()); } } private void dismisspopup() { if (popupwindow == null || !popupwindow.isshowing()) { return; } viewgroup contentview = (viewgroup) popupwindow.getcontentview(); contentview.startanimation(animationhelper.createpopupbgfadeoutanim(animationhelper.time_out)); for (int i = 0; i < itemviews.length; i++) { if (i < 3) { //第一行 itemviews[i].startanimation(animationhelper.createpopupanimout(this, line1deltay)); } else { //第二行 itemviews[i].startanimation(animationhelper.createpopupanimout(this, line2deltay)); } } //动画结束时隐藏popupwindow contentview.postdelayed(new runnable() { @override public void run() { popupwindow.dismiss(); } }, animationhelper.time_out + 10); } private view[] itemviews; //初始化popupwindow上的按钮 private void inititems(view parent) { int[] viewids = new int[]{r.id.yixin_more_item1, r.id.yixin_more_item2, r.id.yixin_more_item3, r.id.yixin_more_item4, r.id.yixin_more_item5, r.id.yixin_more_item6}; itemviews = new view[viewids.length]; int itemwidth = screenutils.getscreenw(this) / 3; onclickimpl l = new onclickimpl(); for (int i = 0; i < viewids.length; i++) { int id = viewids[i]; itemviews[i] = parent.findviewbyid(id); gridlayout.layoutparams p = (gridlayout.layoutparams) itemviews[i].getlayoutparams(); p.width = itemwidth; itemviews[i].setlayoutparams(p); itemviews[i].setonclicklistener(l); } } private class onclickimpl implements view.onclicklistener { @override public void onclick(view v) { final int viewid = v.getid(); //背景动画 popupwindow.getcontentview().startanimation(animationhelper.createpopupbgfadeoutanim(animationhelper.time_out_click)); //动画结束时隐藏popupwindow v.postdelayed(new runnable() { @override public void run() { popupwindow.dismiss(); //动画结束时响应点击事件 handleevent(viewid); } }, animationhelper.time_out_click + 10); //按钮动画 for (view item : itemviews) { if (item.getid() == v.getid()) { //点击的按钮,放大 item.startanimation(animationhelper.createpopupitembiggeranim(mainactivity.this)); } else { //其它按钮,缩小 item.startanimation(animationhelper.createpopupitemsmalleranim(mainactivity.this)); } } } } //popupwindow上按钮的点击事件 private void handleevent(int viewid) { toast.maketext(this, "点击了按钮:" + viewid, toast.length_short).show(); } private int getactionbarheight() { return getsupportactionbar().getheight(); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_more) { if (popupwindow == null || !popupwindow.isshowing()) { showpopup(); } else { dismisspopup(); } return true; } return super.onoptionsitemselected(item); } //点击返回键时,如果popupwindow是显示状态,则关闭它 @override public void onbackpressed() { if (popupwindow != null && popupwindow.isshowing()) { dismisspopup(); return; } super.onbackpressed(); } }
2.动画工具类。
/** * animationhelper */ public class animationhelper { /** * 进入动画的时间 */ public static final int time_in = 300; /** * 进入动画之后的反弹动画时间 */ public static final int time_in_back = 100; /** * 退出动画的时间 */ public static final int time_out = 300; /** * 点击popupwindow上菜单后退出动画的时间 */ public static final int time_out_click = 500; /** * popupwindow上菜单进入动画 */ public static animation createpopupanimin(context context, int fromydelta) { animationset animationset = new animationset(context, null); // animationset.setinterpolator(new bounceinterpolator()); //结束时弹跳 animationset.setfillafter(true); //移动 translateanimation translateanim = new translateanimation(0, 0, fromydelta, 20); translateanim.setduration(time_in); animationset.addanimation(translateanim); //回弹效果 translateanimation translateanim2 = new translateanimation(0, 0, 0, -20); translateanim2.setstartoffset(time_in); translateanim2.setduration(time_in_back); animationset.addanimation(translateanim2); return animationset; } /** * popupwindow上菜单离开动画 */ public static animation createpopupanimout(context context, int toydelta) { animationset animationset = new animationset(context, null); animationset.setfillafter(true); translateanimation translateanim = new translateanimation(0, 0, 0, toydelta); translateanim.setduration(time_out); animationset.addanimation(translateanim); return animationset; } /** * popupwindow背景进入动画(透明度渐变) */ public static animation createpopupbgfadeinanim() { alphaanimation anim = new alphaanimation(0, 1.0f); anim.setduration(time_in); anim.setfillafter(true); return anim; } /** * popupwindow背景离开动画(透明度渐变) */ public static animation createpopupbgfadeoutanim(int duration) { alphaanimation anim = new alphaanimation(1.0f, 0); anim.setduration(duration); anim.setfillafter(true); return anim; } /** * popupwindow按钮点击动画 */ public static animation createpopupitembiggeranim(context context) { animationset animationset = new animationset(context, null); animationset.setfillafter(true); //放大(设置缩放的中心点为自己的中心) scaleanimation scaleanim = new scaleanimation(1.0f, 2.0f, 1.0f, 2.0f, animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f); scaleanim.setduration(time_out_click); animationset.addanimation(scaleanim); //渐变 alphaanimation alphaanim = new alphaanimation(1.0f, 0); alphaanim.setinterpolator(new accelerateinterpolator()); alphaanim.setduration(time_out_click); animationset.addanimation(alphaanim); return animationset; } /** * popupwindow按钮点击时其它按钮的动画 */ public static animation createpopupitemsmalleranim(context context) { //放大(设置缩放的中心点为自己的中心) scaleanimation scaleanim = new scaleanimation(1.0f, 0, 1.0f, 0, animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f); scaleanim.setduration(time_out_click); scaleanim.setfillafter(true); return scaleanim; } }
完整实例代码点击此处本站下载。
更多关于android相关内容感兴趣的读者可查看本站专题:《android开发动画技巧汇总》、《android编程之activity操作技巧总结》、《android视图view技巧总结》、《android布局layout技巧总结》、《android开发入门与进阶教程》、《android资源操作技巧汇总》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。