1. 程式人生 > >android直播app禮物連擊動畫效果

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) { super(context, attrs, defStyleAttr); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 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_layout_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.remove(0); } return view; } /** *<br> Description: todo(這裡用一句話描述這個方法的作用) *<br> Author: yangyinglong *<br> Date: 2017/7/11 16:54 * @param tag */ public void showGift(GiftInfo giftInfo) { View giftView = this.findViewWithTag(giftInfo.getGiftID() + giftInfo.getSenderFace()); if (giftView == null) {/*該使用者不在禮物顯示列表*/ giftView = addGiftView();/*獲取禮物的View的佈局*/ giftView.setTag(giftInfo.getGiftID() + giftInfo.getSenderFace());/*設定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(giftInfo.getSenderNickName()); 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()); NumAnim numAnim = new NumAnim(); giftNum.setTag(R.id.tag_imageview,numAnim); numAnim.start(giftNum); } } /** * 數字放大動畫 */ public static class NumAnim { private Animator lastAnimator = null; private NumAnim lastNumAnim; public void start(View view) { lastNumAnim = (NumAnim) view.getTag(NumAnim.class.hashCode()); if (lastNumAnim != null) { if (lastNumAnim.lastAnimator != null) { lastNumAnim.lastAnimator.removeAllListeners(); lastNumAnim.lastAnimator.end(); lastNumAnim.lastAnimator.cancel(); } } ObjectAnimator anim1_1 = ObjectAnimator.ofFloat(view, "scaleX",3.0f,0.5f); ObjectAnimator anim1_2 = ObjectAnimator.ofFloat(view, "scaleY",3.0f,0.5f); ObjectAnimator anim1_3 = ObjectAnimator.ofFloat(view,"alpha",0.8f); AnimatorSet animatorSet1 = new AnimatorSet(); animatorSet1.setDuration((long) (700 * 0.1)); animatorSet1.setInterpolator(new LinearInterpolator()); animatorSet1.playTogether(anim1_1,anim1_2,anim1_3); ObjectAnimator anim2_1 = ObjectAnimator.ofFloat(view, "scaleX",1.2f); ObjectAnimator anim2_2 = ObjectAnimator.ofFloat(view, "scaleY",1.2f); ObjectAnimator anim2_3 = ObjectAnimator.ofFloat(view, "alpha",1.0f); AnimatorSet animatorSet2 = new AnimatorSet(); animatorSet2.setDuration((long) (700 * 0.2)); animatorSet2.setInterpolator(new LinearInterpolator()); animatorSet2.playTogether(anim2_1,anim2_2,anim2_3); ObjectAnimator anim3_1 = ObjectAnimator.ofFloat(view, "scaleX",1.0f); ObjectAnimator anim3_2 = ObjectAnimator.ofFloat(view, "scaleY",1.0f); AnimatorSet animatorSet3 = new AnimatorSet(); animatorSet3.setDuration((long) (700 * 0.2)); animatorSet3.setInterpolator(new LinearInterpolator()); animatorSet3.playTogether(anim3_1,anim3_2); AnimatorSet animSet = new AnimatorSet(); // lastAnimator = animSet; // animSet.setDuration(500); // animSet.setInterpolator(new OvershootInterpolator()); animSet.playSequentially(animatorSet1, animatorSet2,animatorSet3); animSet.playSequentially(); 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; } } }