没有效果图的示例简直就是扯淡
飘屏动画
直接上代码吧
自定义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
如果有什么问题,欢迎大家指导。并相互联系,希望能够通过文章互相学习。
---财财亲笔