package com.xscm.moduleutil.widget; import android.animation.ObjectAnimator; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.LayerDrawable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; import androidx.core.content.ContextCompat; import com.google.android.material.card.MaterialCardView; import com.xscm.moduleutil.R; import com.xscm.moduleutil.bean.GiftBean; import com.xscm.moduleutil.utils.ImageUtils; /** * 礼物卡片控件 - 支持选中效果和自定义样式 */ public class GiftCardView extends FrameLayout { private ImageView mIconImageView; private TextView mNameTextView; private TextView mCountTextView; private TextView mResultTextView; private boolean isSelected = false; private Drawable selectedBackground; private Drawable normalBackground; // 添加GiftBean数据引用 private GiftBean giftBean; private ObjectAnimator pulseAnimator; public GiftCardView(Context context) { this(context, null); } public GiftCardView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public GiftCardView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initViews(); initAttributes(attrs); } private void initViews() { LayoutInflater.from(getContext()).inflate(R.layout.view_gift_card, this, true); mIconImageView = findViewById(R.id.img_gift_icon); mNameTextView = findViewById(R.id.tv_gift_name); mCountTextView = findViewById(R.id.tv_gift_count); mResultTextView= findViewById(R.id.result); // 设置默认点击事件 setOnClickListener(v -> { if (getOnGiftClickListener() != null) { getOnGiftClickListener().onGiftClick(this); } }); } private void initAttributes(AttributeSet attrs) { TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.GiftCardView); try { // 获取自定义属性 String name = typedArray.getString(R.styleable.GiftCardView_giftName); String count = typedArray.getString(R.styleable.GiftCardView_giftCount); int iconResId = typedArray.getResourceId(R.styleable.GiftCardView_giftIcon, 0); // 设置默认值 if (name != null) { setName(name); } if (count != null) { setCount(count); } if (iconResId != 0) { setIcon(""); } // 获取选中状态相关属性 selectedBackground = typedArray.getDrawable(R.styleable.GiftCardView_selectedBackground); normalBackground = typedArray.getDrawable(R.styleable.GiftCardView_normalBackground); // 如果没有设置选中背景,则使用默认的选中效果 // if (selectedBackground == null) { // selectedBackground = ContextCompat.getDrawable(getContext(), R.mipmap.tkzj_x); // } // if (normalBackground == null) { // normalBackground = ContextCompat.getDrawable(getContext(), R.mipmap.tkzj_w); // } // 设置默认背景 if (normalBackground != null) { setBackground(normalBackground); } } finally { typedArray.recycle(); } } /** * 绑定GiftBean数据 */ public void bindGiftData(GiftBean giftBean) { this.giftBean = giftBean; if (giftBean != null) { // 设置礼物图标 if (giftBean.getBase_image() != null && !giftBean.getBase_image().isEmpty()) { setIcon(giftBean.getBase_image()); // 如果是资源ID } else { setIcon(""); } // 设置礼物名称 setName(giftBean.getGift_name() != null ? giftBean.getGift_name() : "未知礼物"); // 设置礼物数量 setCount(giftBean.getGift_price() != null ? giftBean.getGift_price() : "0"); setmResultTextView(giftBean.getCount()); } } /** * 获取绑定的GiftBean数据 */ public GiftBean getGiftBean() { return giftBean; } /** * 设置礼物图标(URL) */ public void setIcon(String url) { if (mIconImageView != null) { if (url != null && !url.isEmpty()) { ImageUtils.loadHeadCC(url, mIconImageView); } else { mIconImageView.setImageResource(R.mipmap.ic_launcher); } } } private android.animation.AnimatorSet animatorSet; public void startPulseAnimationWithLayer() { if (pulseAnimator != null && pulseAnimator.isRunning()) { pulseAnimator.cancel(); } pulseAnimator = ObjectAnimator.ofFloat(this, "scaleX", 0.9f, 1.1f); pulseAnimator.setDuration(500); pulseAnimator.setRepeatCount(ObjectAnimator.INFINITE); pulseAnimator.setRepeatMode(ObjectAnimator.REVERSE); ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(this, "scaleY", 0.9f, 1.1f); scaleYAnimator.setDuration(500); scaleYAnimator.setRepeatCount(ObjectAnimator.INFINITE); scaleYAnimator.setRepeatMode(ObjectAnimator.REVERSE); // 组合动画 animatorSet = new android.animation.AnimatorSet(); animatorSet.playTogether(pulseAnimator, scaleYAnimator); animatorSet.start(); } public void stopPulseAnimationWithLayer() { if (pulseAnimator != null) { pulseAnimator.cancel(); } // 停止组合动画 if (animatorSet != null && animatorSet.isRunning()) { animatorSet.cancel(); } // 清除引用 animatorSet = null; pulseAnimator = null; // 重置缩放 this.setScaleX(1f); this.setScaleY(1f); // 强制重新绘制 this.invalidate(); } /** * 设置礼物名称 */ public void setName(String name) { if (mNameTextView != null) { mNameTextView.setText(name); } } /** * 设置礼物数量 */ public void setCount(String count) { if (mCountTextView != null) { mCountTextView.setText(count); } } /** * 设置选中状态 */ public void setSelected(boolean selected) { isSelected = selected; if (selected) { setBackground(selectedBackground); // 可以添加额外的选中效果 // setElevation(8f); // 提高阴影 // 添加发光效果 // addGlowEffect(); } else { setBackground(normalBackground); // setElevation(4f); // 恢复默认阴影 // removeGlowEffect(); } } public void setmResultTextView(int num){ if (mResultTextView!=null) { mResultTextView.setText("x" + num); } } public void setVisibilitymResultTextView(boolean isVisible){ if (mResultTextView!=null) { mResultTextView.setVisibility(isVisible?View.VISIBLE:View.GONE); } } /** * 添加发光效果 */ private void addGlowEffect() { // 使用LayerDrawable创建发光效果 GradientDrawable glow = new GradientDrawable(); glow.setColor(Color.TRANSPARENT); glow.setStroke(8, Color.argb(150, 255, 255, 255)); glow.setCornerRadius(16f); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{glow, getBackground()}); setBackground(layerDrawable); } /** * 移除发光效果 */ private void removeGlowEffect() { setBackground(normalBackground); } /** * 获取当前是否选中 */ public boolean isSelected() { return isSelected; } /** * 切换选中状态 */ public void toggleSelected() { setSelected(!isSelected); } /** * 设置点击监听器 */ public void setOnGiftClickListener(OnGiftClickListener listener) { this.onGiftClickListener = listener; } /** * 获取点击监听器 */ public OnGiftClickListener getOnGiftClickListener() { return onGiftClickListener; } /** * 点击监听器接口 */ public interface OnGiftClickListener { void onGiftClick(GiftCardView giftCardView); } private OnGiftClickListener onGiftClickListener; }