当前位置: 移动技术网 > 移动技术>移动开发>Android > Android直播app送礼物连击动画效果(实例代码)

Android直播app送礼物连击动画效果(实例代码)

2019年07月24日  | 移动技术网移动技术  | 我要评论

最近在做公司的直播项目,需要实现一个观看端连击送礼物的控件:

直接上代码:

/**
 * @author yangyinglong on 2017/7/11 16:52.
 * @description: todo(这里用一句话描述这个类的作用)
 * @copyright copyright (c) 2017 tuandai inc. all rights reserved.
 */
public class customgiftview extends linearlayout {
  private timer timer;
  private list<view> giftviewcollection = new arraylist<>();
  public customgiftview(context context) {
    this(context,null);
  }
  public customgiftview(context context, @nullable attributeset attrs) {
    this(context, attrs,0);
  }
  public customgiftview(context context, @nullable attributeset attrs, int defstyleattr) {
    this(context, attrs, defstyleattr,0);
  }
  public customgiftview(context context, attributeset attrs, int defstyleattr, int defstyleres) {
    super(context, attrs, defstyleattr, defstyleres);
  }
  /**
   *<br> description: todo(这里用一句话描述这个方法的作用)
   *<br> author:   yangyinglong
   *<br> date:    2017/7/11 17:40
   */
  public void pause() {
    if (null != timer) {
      timer.cancel();
    }
  }
  public void cancel() {
    if (null != timer) {
      timer.cancel();
    }
  }
  public void resume() {
    cleartiming();
  }
  /**
   * 定时清除礼物
   */
  private void cleartiming() {
    timertask task = new timertask() {
      @override
      public void run() {
        int count = customgiftview.this.getchildcount();
        for (int i = 0; i < count; i++) {
          view view = customgiftview.this.getchildat(i);
          customroundview crvheadimage = (customroundview) view.findviewbyid(r.id.crvheadimage);
          long nowtime = system.currenttimemillis();
          long uptime = (long) crvheadimage.gettag();
          if ((nowtime - uptime) >= 3000) {
            final int j = i;
            post(new runnable() {
              @override
              public void run() {
                customgiftview.this.removeviewat(j);
              }
            });
//            removegiftview(i);
            return;
          }
        }
      }
    };
    if (null != timer) {
      timer.cancel();
    }
    timer = new timer();
    timer.schedule(task, 0, 100);
  }
  /**
   * 添加礼物view,(考虑垃圾回收)
   */
  private view addgiftview() {
    view view = null;
    if (giftviewcollection.size() <= 0) {
      /*如果垃圾回收中没有view,则生成一个*/
      view = layoutinflater.from(getcontext()).inflate(r.layout.item_gift, null);
      linearlayout.layoutparams lp = new linearlayout.layoutparams(viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content);
      lp.topmargin = 10;
      view.setlayoutparams(lp);
      this.addonattachstatechangelistener(new view.onattachstatechangelistener() {
        @override
        public void onviewattachedtowindow(view view) { }
        //复用item,当一个view移除时将它放到池内
        @override
        public void onviewdetachedfromwindow(view view) {
          if (giftviewcollection.size() < 5) {
            giftviewcollection.add(view);
          }
        }
      });
    } else {
      //如果item池内有缓存的view,将它取出来,并从池中删除
      view = giftviewcollection.get(0);
      giftviewcollection.remove(view);
    }
    return view;
  }
  /**
   *<br> description: todo(这里用一句话描述这个方法的作用)
   *<br> author:   yangyinglong
   *<br> date:    2017/7/11 16:54
   * @param tag
   */
  public void showgift(string tag) {
    view giftview = this.findviewwithtag(tag);
    if (giftview == null) {/*该用户不在礼物显示列表*/
      giftview = addgiftview();/*获取礼物的view的布局*/
      giftview.settag(tag);/*设置view标识*/
      customroundview crvheadimage = (customroundview) giftview.findviewbyid(r.id.crvheadimage);
      final magictextview giftnum = (magictextview) giftview.findviewbyid(r.id.giftnum);/*找到数量控件*/
      textview sender = (textview) giftview.findviewbyid(r.id.sender);
      sender.settext(tag);
      giftnum.settext("x1");/*设置礼物数量*/
      crvheadimage.settag(system.currenttimemillis());/*设置时间标记*/
      giftnum.settag(1);/*给数量控件设置标记*/
      this.addview(giftview,0);/*将礼物的view添加到礼物的viewgroup中*/
//      llgiftcontent.invalidate();/*刷新该view*/
      translateanimation inanim = (translateanimation) animationutils.loadanimation(getcontext(), r.anim.gift_in);
      giftview.startanimation(inanim);/*开始执行显示礼物的动画*/
      inanim.setanimationlistener(new animation.animationlistener() {/*显示动画的监听*/
        @override
        public void onanimationstart(animation animation) { }
        @override
        public void onanimationend(animation animation) {
          //注释调,第一次添加没动画
//          giftnumanim.start(giftnum);
          log.d("gao","" + customgiftview.this.getheight());
        }
        @override
        public void onanimationrepeat(animation animation) { }
      });
    } else {/*该用户在礼物显示列表*/
      for (int i = 0;i < customgiftview.this.getchildcount();i ++) {
        if (giftview.equals(customgiftview.this.getchildat(i))) {
          if (i >= 3) {
            customgiftview.this.removeview(giftview);
          }
        }
      }
//            llgiftcontent.addview(giftview,0);
      customroundview crvheadimage = (customroundview) giftview.findviewbyid(r.id.crvheadimage);/*找到头像控件*/
      magictextview giftnum = (magictextview) giftview.findviewbyid(r.id.giftnum);/*找到数量控件*/
      int shownum = (integer) giftnum.gettag() + 1;
      giftnum.settext("x"+shownum);
      giftnum.settag(shownum);
      crvheadimage.settag(system.currenttimemillis());
      new numanim().start(giftnum);
    }
  }
  /**
   * 数字放大动画
   */
  public static class numanim {
    private animator lastanimator = null;
    public void start(view view) {
      if (lastanimator != null) {
        lastanimator.removealllisteners();
        lastanimator.end();
        lastanimator.cancel();
      }
      objectanimator anim1 = objectanimator.offloat(view, "scalex",0.7f, 1.5f,1f);
      objectanimator anim2 = objectanimator.offloat(view, "scaley",0.7f, 1.5f,1f);
      animatorset animset = new animatorset();
      lastanimator = animset;
      animset.setduration(500);
      animset.setinterpolator(new overshootinterpolator());
      animset.playtogether(anim1, anim2);
      animset.start();
    }
  }
  public static class giftinfo {
    private string senderface;
    private string sendernickname;
    private string gifturl;
    private int giftid;
    public string getsenderface() {
      return senderface;
    }
    public void setsenderface(string senderface) {
      this.senderface = senderface;
    }
    public string getsendernickname() {
      return sendernickname;
    }
    public void setsendernickname(string sendernickname) {
      this.sendernickname = sendernickname;
    }
    public string getgifturl() {
      return gifturl;
    }
    public void setgifturl(string gifturl) {
      this.gifturl = gifturl;
    }
    public int getgiftid() {
      return giftid;
    }
    public void setgiftid(int giftid) {
      this.giftid = giftid;
    }
  }
}

以上所述是小编给大家介绍的android直播app礼物连击动画效果,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网