这篇文章将为大家详细讲解有关Android如何实现直播app送礼物连击动画效果,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
最近在做公司的直播项目,需要实现一个观看端连击送礼物的控件:
直接上代码:
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); } 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); } private View addGiftView() { View view = null; if (giftViewCollection.size() <= 0) { 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; } public void showGift(String tag) { View giftView = this.findViewWithTag(tag); if (giftView == null) { giftView = addGiftView(); giftView.setTag(tag); 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);// llgiftcontent.invalidate(); 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送礼物连击动画效果”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。