Android编程实现仿易信精美d出框效果【附demo源码下载】

Android编程实现仿易信精美d出框效果【附demo源码下载】,第1张

概述本文实例讲述了Android编程实现仿易信精美d出框效果。分享给大家供大家参考,具体如下:

本文实例讲述了AndroID编程实现仿易信精美d出框效果。分享给大家供大家参考,具体如下:

截图:

动画效果介绍:

1.点击Actionbar上“+”按钮,菜单从上方d出(带反d效果);
2.再次点击“+”、点击空白区域或者点击返回键,菜单向上方收起;
3.点击d出框上的按钮时,该按钮放大,其它按钮缩小,菜单整体渐变退出。

主体代码:

1.Activity.

/** * 仿易信动画d出框 */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;  //仿易信更多d出框  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);      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;  /**   * 进入动画之后的反d动画时间   */  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()); //结束时d跳    animationSet.setFillAfter(true);    //移动    TranslateAnimation translateAnim = new TranslateAnimation(0,fromYDelta,20);    translateAnim.setDuration(TIME_IN);    animationSet.addAnimation(translateAnim);    //回d效果    TranslateAnimation translateAnim2 = new TranslateAnimation(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,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,Animation.relative_TO_SELF,0.5f,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.5f);    scaleAnim.setDuration(TIME_OUT_CliCK);    scaleAnim.setFillAfter(true);    return scaleAnim;  }}

完整实例代码点击此处本站下载

更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android开发动画技巧汇总》、《Android编程之activity *** 作技巧总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》、《Android开发入门与进阶教程》、《Android资源 *** 作技巧汇总》及《Android控件用法总结》

希望本文所述对大家AndroID程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Android编程实现仿易信精美d出框效果【附demo源码下载】全部内容,希望文章能够帮你解决Android编程实现仿易信精美d出框效果【附demo源码下载】所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147202.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存