android实现抖音直播间飘屏送礼物动画(超简单)

没有效果图的示例简直就是扯淡

在这里插入图片描述

飘屏动画

直接上代码吧

自定义view:ShowNewLuckyMsgView.class

package com.chushou.demo2;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;


/**
 * cc--消息通道====中奖通知界面
 * cc 96.10.11
 */
public class ShowNewLuckyMsgView implements Animation.AnimationListener {
    private View convertView;
    private LinearLayout rootLayout;
    private ViewGroup mRelLuckyMsgView;
    private Animation inAnimation;
    private Animation outAnimation;
    private String anchorName;
    private Context context;

    //抽奖通道显示的两个布局
    LinearLayout mLinGift;
    LinearLayout mLinMsg;

    //金币消息头像、昵称、中奖金币数
    ImageView mImgLuckyMsgAv;
    TextView mTxtLuckyMsgName;
    RunTextView mTxtLuckyMsgNumber;
    ImageView mImgLuckyMsgNumber;
    TextView mTxtLuckyMsgRound;

    ImageView mImgLuckyGiftAv;
    ImageView mImgLuckyGiftCoin;
    TextView mTxtLuckyGiftName;
    TextView mTxtLuckyGiftAnchorName;
    TextView mTxtLuckyGiftRound;


    //用户头像
    String userAv = "";
    //用户名
    String userName = "";
    //中奖金额
    int goodNumber = 0;
    //礼物集合
    List<LuckyGiftBean> giftLists = new ArrayList<>();

    /**
     * 第二版
     *
     * @param context
     * @param mRelLuckyMsgView
     */
    public ShowNewLuckyMsgView(Context context, ViewGroup mRelLuckyMsgView) {
        this.mRelLuckyMsgView = mRelLuckyMsgView;
        this.context = context;
        initView();
    }

    public void setData(String userAv, String userName, int forAnchorGoodNumber, List<LuckyGiftBean> giftLists, String anchorName) {
        this.userAv = userAv;
        this.userName = userName;
        this.goodNumber = forAnchorGoodNumber;
        this.giftLists = giftLists;
        this.anchorName = anchorName;
    }

    @SuppressLint("InflateParams")
    public void initView() {
        //抽奖消息
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.toast_lusky_msg_toast_layout, null);
            rootLayout = convertView.findViewById(R.id.toast_lucky_msg_layout);

            //抽奖通道显示的两个布局
            mLinGift = rootLayout.findViewById(R.id.lin_lucky_gift);
            mLinMsg = rootLayout.findViewById(R.id.lin_lucky_msg);

            //金币消息头像、昵称、中奖金币数
            mImgLuckyMsgAv = rootLayout.findViewById(R.id.img_lucky_msg_av);
            mTxtLuckyMsgName = rootLayout.findViewById(R.id.txt_lucky_msg_name);
            mTxtLuckyMsgNumber = rootLayout.findViewById(R.id.txt_lucky_msg_number);
            mImgLuckyMsgNumber = rootLayout.findViewById(R.id.img_lucky_msg_number);
            mTxtLuckyMsgRound = rootLayout.findViewById(R.id.txt_lucky_msg_round);

            mImgLuckyGiftAv = rootLayout.findViewById(R.id.img_lucky_gift_av);
            mImgLuckyGiftCoin = rootLayout.findViewById(R.id.img_lucky_gift_icon);
            mTxtLuckyGiftName = rootLayout.findViewById(R.id.txt_lucky_gift_name);
            mTxtLuckyGiftAnchorName = rootLayout.findViewById(R.id.txt_lucky_gift_anchor_name);
            mTxtLuckyGiftRound = rootLayout.findViewById(R.id.txt_lucky_gift_round);
        }

        //默认不显示
        mLinGift.setVisibility(View.GONE);

        //如果不是空的显示
        if (giftLists.size() > 0) {
            mLinGift.setVisibility(View.VISIBLE);


            displayCircleImage(context, giftLists.get(0).getUrl(), mImgLuckyGiftCoin, 0);
            displayCircleImage(context, userAv, mImgLuckyGiftAv, 0);

            //昵称
            mTxtLuckyGiftName.setText(userName);
            //主播name
            mTxtLuckyGiftAnchorName.setText(">>" + anchorName);
            //round
            mTxtLuckyGiftRound.setText("x" + giftLists.get(0).getCount());
            giftLists.remove(0);
        } else {
            mLinGift.setVisibility(View.GONE);
        }


        //消息通道消息
        mLinMsg.setVisibility(View.VISIBLE);
        //头像
        displayCircleImage(context, userAv, mImgLuckyMsgAv, 0);
        //昵称
        mTxtLuckyMsgName.setText(userName);
        //中奖数
        mTxtLuckyMsgNumber.runWithAnimation(
                TextUtils.isEmpty(mTxtLuckyMsgNumber.getText().toString().trim()) ? 0 :
                        Integer.parseInt(mTxtLuckyMsgNumber.getText().toString().trim().replace(",", ""))
                , goodNumber / 100);
        //第几轮
        mTxtLuckyMsgRound.setText(new StringBuilder("Bonus").append("1"));


        startDh(context, R.anim.scalebig, mTxtLuckyMsgNumber);
        startDh(context, R.anim.scalebig, mImgLuckyMsgNumber);

        if (mRelLuckyMsgView.getChildCount() == 0) {
            mRelLuckyMsgView.addView(convertView);
            //适配阿拉伯语
            inAnimation = AnimationUtils.loadAnimation(context, R.anim.lucky_msg_in);

            inAnimation.setAnimationListener(this);
            mLinMsg.startAnimation(inAnimation);
            if (giftLists.size() != 0) {
                mLinGift.startAnimation(inAnimation);
            }
        } else {
            handler.removeCallbacksAndMessages(null);
            showCount = 0;
        }
        handler.sendEmptyMessage(1);
    }

    private int showCount = 0;

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 1:
                    if (showCount > 1) {     //从0开始大于1,说明当前是2。也就是2s后执行
                        handler.sendEmptyMessageDelayed(2, 750L);
                        showCount = 0;
                    } else {
                        showCount++;
                        handler.sendEmptyMessageDelayed(1, 750L);
                    }
                    break;
                case 2:
                    if (giftLists.size() > 0) {
                        Animation giftExitAnimation = AnimationUtils.loadAnimation(
                                context,
                                R.anim.lucky_gift_exit
                        );
                        giftExitAnimation.setAnimationListener(new Animation.AnimationListener() {
                            @Override
                            public void onAnimationStart(Animation animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animation animation) {
                                displayCircleImage(context, giftLists.get(0).getUrl(), mImgLuckyGiftCoin, 0);
                                //round
                                mTxtLuckyGiftRound.setText("x" + giftLists.get(0).getCount());
                                giftLists.remove(0);
                                mLinGift.setVisibility(View.VISIBLE);
                                mLinGift.setAnimation(inAnimation);
                                handler.sendEmptyMessage(1);
                            }

                            @Override
                            public void onAnimationRepeat(Animation animation) {

                            }
                        });
                        mLinGift.startAnimation(giftExitAnimation);
                    } else {
                        mLinMsg.clearAnimation();
                        mLinGift.clearAnimation();
                        mRelLuckyMsgView.removeView(convertView);
                        if (mRelLuckyMsgView.getChildCount() == 0) {
                            mRelLuckyMsgView.setVisibility(View.VISIBLE);
                        }
                        convertView = null;
                        if (null != onDismissListener) {
                            onDismissListener.onDismiss();
                        }
                    }
                    break;
            }
            return true;
        }
    });

    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    private static void startDh(Context mContext, int id, View view) {
        Animation a = AnimationUtils.loadAnimation(mContext, id);
        a.setDuration(200);
        a.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
//                startDh(mContext, R.anim.scalesmall, view);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(a);
    }

    //消失事件
    public interface OnDismissListener {
        void onDismiss();
    }

    private OnDismissListener onDismissListener;

    public void setOnDismissListener(OnDismissListener onDismissListener) {
        this.onDismissListener = onDismissListener;
    }

    public static void displayCircleImage(Context context, String url, ImageView imageView, int placeHolderId) {
        RequestOptions options = (new RequestOptions()).circleCrop().placeholder(placeHolderId);
        Glide.with(context).load(!isEmpty(url) ? url.trim() : url).transition(DrawableTransitionOptions.withCrossFade()).apply(options).into(imageView);
    }

    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0;
    }

}
 注:这里处理了所有的逻辑操作和显示操作。剩下就是在使用的地方调用就可以了

调用的地方也很简单

activity.class

RelativeLayout mRelMsgLayout = findViewById(R.id.rel_message_layout);
//飘窗动画来
if (showNewLuckyMsgView == null) {
    showNewLuckyMsgView = new ShowNewLuckyMsgView(MainActivity.this, mRelMsgLayout);
}

showNewLuckyMsgView.setData(
        "https://img2.baidu.com/it/u=1984375996,501332464&fm=253&fmt=auto&app=138&f=JPEG?w=740&h=351",              //用户头像
        "榜1⃣️大哥",            //用户昵称
        100000 * count,    //赠送的金币  (显示的时候除了100)
        giftLists,                    //赠送礼物集合
        "苍井空");          //主播昵称
showNewLuckyMsgView.initView();
/**
 * 监听飘窗消息是回调
 */
showNewLuckyMsgView.setOnDismissListener(new ShowNewLuckyMsgView.OnDismissListener() {
    @Override
    public void onDismiss() {
        count = 1;
        Snackbar.make(mRelMsgLayout, "飘窗结束了", 1000).show();
    }
});
注:至此,所有的都已经完成了。点击start就可以看到效果了。

附上demo源码。

源码:源码请点这里


Q:486789970(QQ现在很少用)
email:mr.cai_cai@foxmail.com

如果有什么问题,欢迎大家指导。并相互联系,希望能够通过文章互相学习。

											                               	---财财亲笔
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Android Studio中实现直播间送礼物功能可以参考以下步骤: 1. 首先,你需要在后端设置一个接口,用于处理用户送礼物的请求。这个接口应该能够扣除用户的金币并增加主播的映票数。在接口的响应中,生成一个唯一的token,并将其返回给客户端。 2. 在客户端接收到后端响应后,从响应数据中获取token,并使用socket通信的方法将token发送给socket服务器。服务器会将这个消息广播给当前直播间的所有用户。 3. 当每个用户收到socket消息后,解析json数据,获取送礼物的用户ID、昵称、礼物ID、名称、种类(普通或豪华礼物)以及礼物资源文件的下载地址等信息。 4. 如果是豪华礼物,首先检查本地磁盘中是否已经缓存了该礼物的资源文件。如果没有缓存,先下载资源文件。 5. 下载完成后,根据礼物的类型(gif或svga),使用相应的库进行展示。 对于gif礼物,可以使用koral–/android-gif-drawable库进行播放。对于svga礼物,可以使用yyued/SVGAPlayer-Android库进行播放。 以上是在Android Studio中实现直播间送礼物功能的一种实现方式。你可以根据具体需求和项目情况进行相应的调整和优化。\[1\] \[2\] \[3\] #### 引用[.reference_title] - *1* [Android直播APP源码搭建中豪华物特效的实现](https://blog.csdn.net/yun_bao_2144899870/article/details/99428803)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [android实现直播点赞飘心动画效果](https://blog.csdn.net/weixin_39618956/article/details/117287947)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谁抢我的小口口

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值