Android直播app送礼物连击动画效果(实例代码)
最近在做公司的直播项目,需要实现一个观看端连击送礼物的控件:
直接上代码:
/** *@authoryangyinglongon2017/7/1116:52. *@Description:todo(这里用一句话描述这个类的作用) *@CopyrightCopyright(c)2017TuandaiInc.AllRightsReserved. */ publicclassCustomGiftViewextendsLinearLayout{ privateTimertimer; privateListgiftViewCollection=newArrayList<>(); publicCustomGiftView(Contextcontext){ this(context,null); } publicCustomGiftView(Contextcontext,@NullableAttributeSetattrs){ this(context,attrs,0); } publicCustomGiftView(Contextcontext,@NullableAttributeSetattrs,intdefStyleAttr){ this(context,attrs,defStyleAttr,0); } publicCustomGiftView(Contextcontext,AttributeSetattrs,intdefStyleAttr,intdefStyleRes){ super(context,attrs,defStyleAttr,defStyleRes); } /** *
Description:todo(这里用一句话描述这个方法的作用) *
Author:yangyinglong *
Date:2017/7/1117:40 */ publicvoidpause(){ if(null!=timer){ timer.cancel(); } } publicvoidcancel(){ if(null!=timer){ timer.cancel(); } } publicvoidresume(){ clearTiming(); } /** *定时清除礼物 */ privatevoidclearTiming(){ TimerTasktask=newTimerTask(){ @Override publicvoidrun(){ intcount=CustomGiftView.this.getChildCount(); for(inti=0;i=3000){ finalintj=i; post(newRunnable(){ @Override publicvoidrun(){ CustomGiftView.this.removeViewAt(j); } }); //removeGiftView(i); return; } } } }; if(null!=timer){ timer.cancel(); } timer=newTimer(); timer.schedule(task,0,100); } /** *添加礼物view,(考虑垃圾回收) */ privateViewaddGiftView(){ Viewview=null; if(giftViewCollection.size()<=0){ /*如果垃圾回收中没有view,则生成一个*/ view=LayoutInflater.from(getContext()).inflate(R.layout.item_gift,null); LinearLayout.LayoutParamslp=newLinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); lp.topMargin=10; view.setLayoutParams(lp); this.addOnAttachStateChangeListener(newView.OnAttachStateChangeListener(){ @Override publicvoidonViewAttachedToWindow(Viewview){} //复用Item,当一个View移除时将它放到池内 @Override publicvoidonViewDetachedFromWindow(Viewview){ if(giftViewCollection.size()<5){ giftViewCollection.add(view); } } }); }else{ //如果Item池内有缓存的view,将它取出来,并从池中删除 view=giftViewCollection.get(0); giftViewCollection.remove(view); } returnview; } /** *
Description:todo(这里用一句话描述这个方法的作用) *
Author:yangyinglong *
Date:2017/7/1116:54 *@paramtag */ publicvoidshowGift(Stringtag){ ViewgiftView=this.findViewWithTag(tag); if(giftView==null){/*该用户不在礼物显示列表*/ giftView=addGiftView();/*获取礼物的View的布局*/ giftView.setTag(tag);/*设置view标识*/ CustomRoundViewcrvheadimage=(CustomRoundView)giftView.findViewById(R.id.crvheadimage); finalMagicTextViewgiftNum=(MagicTextView)giftView.findViewById(R.id.giftNum);/*找到数量控件*/ TextViewsender=(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*/ TranslateAnimationinAnim=(TranslateAnimation)AnimationUtils.loadAnimation(getContext(),R.anim.gift_in); giftView.startAnimation(inAnim);/*开始执行显示礼物的动画*/ inAnim.setAnimationListener(newAnimation.AnimationListener(){/*显示动画的监听*/ @Override publicvoidonAnimationStart(Animationanimation){} @Override publicvoidonAnimationEnd(Animationanimation){ //注释调,第一次添加没动画 //giftNumAnim.start(giftNum); Log.d("gao",""+CustomGiftView.this.getHeight()); } @Override publicvoidonAnimationRepeat(Animationanimation){} }); }else{/*该用户在礼物显示列表*/ for(inti=0;i=3){ CustomGiftView.this.removeView(giftView); } } } //llgiftcontent.addView(giftView,0); CustomRoundViewcrvheadimage=(CustomRoundView)giftView.findViewById(R.id.crvheadimage);/*找到头像控件*/ MagicTextViewgiftNum=(MagicTextView)giftView.findViewById(R.id.giftNum);/*找到数量控件*/ intshowNum=(Integer)giftNum.getTag()+1; giftNum.setText("x"+showNum); giftNum.setTag(showNum); crvheadimage.setTag(System.currentTimeMillis()); newNumAnim().start(giftNum); } } /** *数字放大动画 */ publicstaticclassNumAnim{ privateAnimatorlastAnimator=null; publicvoidstart(Viewview){ if(lastAnimator!=null){ lastAnimator.removeAllListeners(); lastAnimator.end(); lastAnimator.cancel(); } ObjectAnimatoranim1=ObjectAnimator.ofFloat(view,"scaleX",0.7f,1.5f,1f); ObjectAnimatoranim2=ObjectAnimator.ofFloat(view,"scaleY",0.7f,1.5f,1f); AnimatorSetanimSet=newAnimatorSet(); lastAnimator=animSet; animSet.setDuration(500); animSet.setInterpolator(newOvershootInterpolator()); animSet.playTogether(anim1,anim2); animSet.start(); } } publicstaticclassGiftInfo{ privateStringsenderFace; privateStringsenderNickName; privateStringgiftUrl; privateintgiftID; publicStringgetSenderFace(){ returnsenderFace; } publicvoidsetSenderFace(StringsenderFace){ this.senderFace=senderFace; } publicStringgetSenderNickName(){ returnsenderNickName; } publicvoidsetSenderNickName(StringsenderNickName){ this.senderNickName=senderNickName; } publicStringgetGiftUrl(){ returngiftUrl; } publicvoidsetGiftUrl(StringgiftUrl){ this.giftUrl=giftUrl; } publicintgetGiftID(){ returngiftID; } publicvoidsetGiftID(intgiftID){ this.giftID=giftID; } } }
以上所述是小编给大家介绍的Android直播app礼物连击动画效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!