最近在做公司的直播项目,需要实现一个观看端连击送礼物的控件:
直接上代码:
/** * @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,defStyleAttr,AttributeSet attrs,int defStyleAttr,int defStyleRes) { super(context,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,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",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礼物连击动画效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android直播app送礼物连击动画效果(实例代码)全部内容,希望文章能够帮你解决Android直播app送礼物连击动画效果(实例代码)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)