您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Android如何實現直播app送禮物連擊動畫效果,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
最近在做公司的直播項目,需要實現一個觀看端連擊送禮物的控件:
直接上代碼:
/** * @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送禮物連擊動畫效果”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。