本文实例讲述了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
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程序设计有所帮助。