修改动画
This commit is contained in:
@@ -1,26 +1,23 @@
|
||||
package com.xscm.moduleutil.view;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
|
||||
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.TypedValue;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.animation.Animation;
|
||||
import android.view.animation.RotateAnimation;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.xscm.moduleutil.R;
|
||||
|
||||
import com.xscm.moduleutil.bean.GiftBean;
|
||||
import com.xscm.moduleutil.bean.blindboxwheel.BlindBoxBean;
|
||||
|
||||
public class QXMeetGiftView extends RelativeLayout {
|
||||
|
||||
@@ -31,198 +28,160 @@ public class QXMeetGiftView extends RelativeLayout {
|
||||
private ImageView giftBgImageView;
|
||||
private ImageView giftImageView;
|
||||
|
||||
// 自定义属性变量
|
||||
private int normalBgRes;
|
||||
private int lockBgRes;
|
||||
private int giftLightBgRes;
|
||||
private int priceBgRes;
|
||||
private int coinIconRes;
|
||||
private int giftNameColor;
|
||||
private int coinTextColor;
|
||||
private float giftNameSize;
|
||||
private float coinTextSize;
|
||||
private int bgBottomMargin;
|
||||
private int giftImageMargin;
|
||||
private int priceBgHeight;
|
||||
private int priceBgBottomMargin;
|
||||
private int lockGiftSize;
|
||||
private int rotateDuration;
|
||||
|
||||
private boolean isLockGift;
|
||||
private Object model;
|
||||
private Object model; // 这里用 Object 代替 QXDrawGiftModel
|
||||
|
||||
public QXMeetGiftView(Context context) {
|
||||
this(context, null);
|
||||
super(context);
|
||||
initSubviews(context);
|
||||
}
|
||||
|
||||
public QXMeetGiftView(Context context, @Nullable AttributeSet attrs) {
|
||||
this(context, attrs, 0);
|
||||
super(context, attrs);
|
||||
initSubviews(context);
|
||||
}
|
||||
|
||||
public QXMeetGiftView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
// 解析自定义属性
|
||||
initAttrs(context, attrs);
|
||||
// 初始化子视图
|
||||
initSubviews(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析自定义属性,设置默认值
|
||||
*/
|
||||
private void initAttrs(Context context, AttributeSet attrs) {
|
||||
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.QXMeetGiftView);
|
||||
|
||||
// 背景资源(默认使用原代码中的资源)
|
||||
normalBgRes = ta.getResourceId(R.styleable.QXMeetGiftView_normalBg, R.mipmap.xlh_g_2);
|
||||
lockBgRes = ta.getResourceId(R.styleable.QXMeetGiftView_lockBg, R.mipmap.ic_launcher);
|
||||
giftLightBgRes = ta.getResourceId(R.styleable.QXMeetGiftView_giftLightBg, R.mipmap.jinb);
|
||||
priceBgRes = ta.getResourceId(R.styleable.QXMeetGiftView_priceBg, R.mipmap.ic_launcher);
|
||||
coinIconRes = ta.getResourceId(R.styleable.QXMeetGiftView_coinIcon, R.mipmap.ic_launcher);
|
||||
|
||||
// 文字样式
|
||||
giftNameColor = ta.getColor(R.styleable.QXMeetGiftView_giftNameColor, 0xFFFFFFFF);
|
||||
coinTextColor = ta.getColor(R.styleable.QXMeetGiftView_coinTextColor, 0xFFC7BF62);
|
||||
giftNameSize = ta.getDimension(R.styleable.QXMeetGiftView_giftNameSize, sp2px(12));
|
||||
coinTextSize = ta.getDimension(R.styleable.QXMeetGiftView_coinTextSize, sp2px(10));
|
||||
|
||||
// 尺寸配置
|
||||
bgBottomMargin = ta.getDimensionPixelOffset(R.styleable.QXMeetGiftView_bgBottomMargin, dp2px(19));
|
||||
giftImageMargin = ta.getDimensionPixelOffset(R.styleable.QXMeetGiftView_giftImageMargin, dp2px(6));
|
||||
priceBgHeight = ta.getDimensionPixelOffset(R.styleable.QXMeetGiftView_priceBgHeight, dp2px(15));
|
||||
priceBgBottomMargin = ta.getDimensionPixelOffset(R.styleable.QXMeetGiftView_priceBgBottomMargin, dp2px(15));
|
||||
lockGiftSize = ta.getDimensionPixelOffset(R.styleable.QXMeetGiftView_lockGiftSize, dp2px(62));
|
||||
|
||||
// 动画时长
|
||||
rotateDuration = ta.getInt(R.styleable.QXMeetGiftView_rotateDuration, 3000);
|
||||
|
||||
ta.recycle(); // 回收资源,避免内存泄漏
|
||||
}
|
||||
|
||||
private void initSubviews(Context context) {
|
||||
// 1. 主背景
|
||||
// 创建背景图片视图
|
||||
bgImageView = new ImageView(context);
|
||||
bgImageView.setScaleType(ImageView.ScaleType.FIT_XY);
|
||||
bgImageView.setImageResource(normalBgRes); // 使用自定义属性
|
||||
LayoutParams bgParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
|
||||
bgParams.addRule(ALIGN_PARENT_TOP);
|
||||
bgParams.addRule(ALIGN_PARENT_LEFT);
|
||||
bgParams.addRule(ALIGN_PARENT_RIGHT);
|
||||
bgParams.bottomMargin = bgBottomMargin; // 使用自定义属性
|
||||
bgImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
||||
bgImageView.setImageResource(R.drawable.ac_left_gift_bg);
|
||||
LayoutParams bgParams = new LayoutParams(
|
||||
LayoutParams.MATCH_PARENT,
|
||||
LayoutParams.MATCH_PARENT
|
||||
);
|
||||
addView(bgImageView, bgParams);
|
||||
|
||||
// 2. 礼物光效背景
|
||||
giftBgImageView = new ImageView(context);
|
||||
giftBgImageView.setScaleType(ImageView.ScaleType.FIT_XY);
|
||||
giftBgImageView.setImageResource(giftLightBgRes); // 使用自定义属性
|
||||
giftBgImageView.setVisibility(View.GONE);
|
||||
LayoutParams giftBgParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
|
||||
giftBgParams.addRule(ALIGN_PARENT_TOP);
|
||||
giftBgParams.addRule(ALIGN_PARENT_LEFT);
|
||||
giftBgParams.addRule(ALIGN_PARENT_RIGHT);
|
||||
giftBgParams.bottomMargin = bgBottomMargin; // 使用自定义属性
|
||||
addView(giftBgImageView, giftBgParams);
|
||||
|
||||
// 3. 礼物图片
|
||||
// 创建礼物图片视图(圆形)
|
||||
giftImageView = new ImageView(context);
|
||||
giftImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
||||
LayoutParams giftImageParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
|
||||
giftImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
||||
LayoutParams giftImageParams = new LayoutParams(
|
||||
dpToPx(60), // 固定宽度
|
||||
dpToPx(60) // 固定高度
|
||||
);
|
||||
giftImageParams.addRule(CENTER_IN_PARENT);
|
||||
giftImageParams.setMargins(giftImageMargin, giftImageMargin, giftImageMargin, 0); // 使用自定义属性
|
||||
giftImageParams.topMargin = dpToPx(10);
|
||||
addView(giftImageView, giftImageParams);
|
||||
|
||||
// 4. 价格背景
|
||||
giftPriceBgView = new ImageView(context);
|
||||
giftPriceBgView.setImageResource(priceBgRes); // 使用自定义属性
|
||||
LayoutParams priceBgParams = new LayoutParams(LayoutParams.MATCH_PARENT, priceBgHeight); // 使用自定义属性
|
||||
priceBgParams.addRule(ALIGN_PARENT_BOTTOM);
|
||||
priceBgParams.bottomMargin = priceBgBottomMargin; // 使用自定义属性
|
||||
addView(giftPriceBgView, priceBgParams);
|
||||
// 创建礼物背景光效视图
|
||||
giftBgImageView = new ImageView(context);
|
||||
giftBgImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
||||
giftBgImageView.setImageResource(R.drawable.ac_lock_gift_light_bg);
|
||||
giftBgImageView.setVisibility(View.GONE); // 初始隐藏
|
||||
LayoutParams giftBgParams = new LayoutParams(
|
||||
LayoutParams.MATCH_PARENT,
|
||||
LayoutParams.MATCH_PARENT
|
||||
);
|
||||
addView(giftBgImageView, giftBgParams);
|
||||
|
||||
// 5. 金币按钮
|
||||
// 创建金币按钮
|
||||
giftCoin = new Button(context);
|
||||
giftCoin.setTextColor(coinTextColor); // 使用自定义属性
|
||||
giftCoin.setTextSize(TypedValue.COMPLEX_UNIT_PX, coinTextSize); // 使用自定义属性
|
||||
// 金币图标
|
||||
Drawable coinDrawable = getResources().getDrawable(coinIconRes); // 使用自定义属性
|
||||
coinDrawable.setBounds(0, 0, dp2px(12), dp2px(12));
|
||||
giftCoin.setTextColor(0xFFC7BF62);
|
||||
// 设置按钮图标
|
||||
Drawable coinDrawable = getResources().getDrawable(R.mipmap.jinb);
|
||||
coinDrawable.setBounds(0, 0, dpToPx(1), dpToPx(1));
|
||||
giftCoin.setCompoundDrawables(coinDrawable, null, null, null);
|
||||
giftCoin.setTextSize(10);
|
||||
giftCoin.setBackgroundColor(0x00000000); // 透明背景
|
||||
LayoutParams coinParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
|
||||
coinParams.addRule(CENTER_IN_PARENT, TRUE);
|
||||
LayoutParams coinParams = new LayoutParams(
|
||||
LayoutParams.WRAP_CONTENT,
|
||||
LayoutParams.WRAP_CONTENT
|
||||
);
|
||||
coinParams.addRule(CENTER_HORIZONTAL);
|
||||
coinParams.addRule(ALIGN_PARENT_BOTTOM);
|
||||
coinParams.bottomMargin = dpToPx(25);
|
||||
addView(giftCoin, coinParams);
|
||||
|
||||
// 6. 礼物名称
|
||||
// 创建礼物名称标签
|
||||
giftNameLabel = new TextView(context);
|
||||
giftNameLabel.setTextColor(giftNameColor); // 使用自定义属性
|
||||
giftNameLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, giftNameSize); // 使用自定义属性
|
||||
giftNameLabel.setGravity(Gravity.CENTER);
|
||||
LayoutParams nameParams = new LayoutParams(LayoutParams.MATCH_PARENT, priceBgHeight); // 使用自定义属性
|
||||
nameParams.addRule(ALIGN_PARENT_BOTTOM);
|
||||
giftNameLabel.setTextColor(0xFFFFFFFF);
|
||||
giftNameLabel.setTextSize(12);
|
||||
giftNameLabel.setGravity(android.view.Gravity.CENTER);
|
||||
giftNameLabel.setSingleLine(true); // 设置为单行显示
|
||||
giftNameLabel.setEllipsize(android.text.TextUtils.TruncateAt.END); // 超出部分用省略号表示
|
||||
LayoutParams nameParams = new LayoutParams(
|
||||
LayoutParams.MATCH_PARENT,
|
||||
LayoutParams.WRAP_CONTENT
|
||||
);
|
||||
nameParams.addRule(CENTER_HORIZONTAL);
|
||||
nameParams.addRule(BELOW, giftCoin.getId());
|
||||
nameParams.topMargin = dpToPx(5);
|
||||
addView(giftNameLabel, nameParams);
|
||||
|
||||
// 调整层级
|
||||
bringChildToFront(giftPriceBgView);
|
||||
bringChildToFront(giftCoin);
|
||||
// 调整视图层级 - 确保正确的层级关系
|
||||
// 按添加顺序已经确定层级,最晚添加的在最上层
|
||||
}
|
||||
|
||||
public void setIsLockGift(boolean isLockGift) {
|
||||
this.isLockGift = isLockGift;
|
||||
|
||||
// 设置背景图片(使用自定义属性)
|
||||
int bgResource = isLockGift ? lockBgRes : normalBgRes;
|
||||
// 设置背景图片
|
||||
int bgResource = isLockGift ?
|
||||
R.drawable.ac_lock_gift_bg : R.drawable.ac_left_gift_bg;
|
||||
bgImageView.setImageResource(bgResource);
|
||||
|
||||
// 显示/隐藏光效背景
|
||||
giftBgImageView.setVisibility(isLockGift ? View.VISIBLE : View.GONE);
|
||||
|
||||
// 调整礼物图片尺寸(使用自定义属性)
|
||||
LayoutParams params = (LayoutParams) giftImageView.getLayoutParams();
|
||||
if (isLockGift) {
|
||||
params.width = lockGiftSize;
|
||||
params.height = lockGiftSize;
|
||||
// 重新设置礼物图片的约束
|
||||
LayoutParams params = (LayoutParams) giftImageView.getLayoutParams();
|
||||
params.width = dpToPx(62);
|
||||
params.height = dpToPx(62);
|
||||
params.addRule(CENTER_IN_PARENT);
|
||||
params.setMargins(0, 0, 0, 0);
|
||||
giftImageView.setLayoutParams(params);
|
||||
} else {
|
||||
// 恢复原始约束
|
||||
LayoutParams params = (LayoutParams) giftImageView.getLayoutParams();
|
||||
params.width = LayoutParams.MATCH_PARENT;
|
||||
params.height = LayoutParams.WRAP_CONTENT;
|
||||
params.setMargins(giftImageMargin, giftImageMargin, giftImageMargin, 0);
|
||||
params.addRule(CENTER_IN_PARENT);
|
||||
params.setMargins(dpToPx(6), dpToPx(6), dpToPx(6), 0);
|
||||
giftImageView.setLayoutParams(params);
|
||||
}
|
||||
giftImageView.setLayoutParams(params);
|
||||
}
|
||||
|
||||
public void setModel(Object model) {
|
||||
public void setModel(BlindBoxBean.GiveGift model) {
|
||||
this.model = model;
|
||||
// 实际业务中解析模型数据
|
||||
/*
|
||||
if (model instanceof QXDrawGiftModel) {
|
||||
QXDrawGiftModel giftModel = (QXDrawGiftModel) model;
|
||||
giftNameLabel.setText(giftModel.getGiftName());
|
||||
giftCoin.setText(giftModel.getGiftPrice());
|
||||
// Glide.with(getContext()).load(giftModel.getBaseImage()).into(giftImageView);
|
||||
// 这里需要根据您的 QXDrawGiftModel 类来实现具体逻辑
|
||||
|
||||
if (model instanceof BlindBoxBean.GiveGift) {
|
||||
BlindBoxBean.GiveGift giftModel = (BlindBoxBean.GiveGift) model;
|
||||
|
||||
// 使用图片加载库加载图片
|
||||
Glide.with(getContext()).load(giftModel.getBase_image()).into(giftImageView);
|
||||
|
||||
giftNameLabel.setText(giftModel.getGift_name());
|
||||
giftCoin.setText(giftModel.getGift_price());
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
public void startAnimation() {
|
||||
// 礼物图片顺时针旋转(使用自定义动画时长)
|
||||
// 礼物图片顺时针旋转动画
|
||||
RotateAnimation rotateAnimation = new RotateAnimation(
|
||||
0, 360,
|
||||
0, 360, // 从 0 度旋转到 360 度
|
||||
Animation.RELATIVE_TO_SELF, 0.5f,
|
||||
Animation.RELATIVE_TO_SELF, 0.5f
|
||||
);
|
||||
rotateAnimation.setDuration(rotateDuration);
|
||||
rotateAnimation.setDuration(3000); // 3 秒
|
||||
rotateAnimation.setRepeatCount(Animation.INFINITE);
|
||||
rotateAnimation.setRepeatMode(Animation.RESTART);
|
||||
rotateAnimation.setInterpolator(getContext(), android.R.anim.linear_interpolator);
|
||||
giftImageView.startAnimation(rotateAnimation);
|
||||
|
||||
// 光效背景逆时针旋转
|
||||
// 光效背景逆时针旋转动画
|
||||
RotateAnimation rotateAnimation1 = new RotateAnimation(
|
||||
0, -360,
|
||||
0, -360, // 从 0 度旋转到 -360 度
|
||||
Animation.RELATIVE_TO_SELF, 0.5f,
|
||||
Animation.RELATIVE_TO_SELF, 0.5f
|
||||
);
|
||||
rotateAnimation1.setDuration(rotateDuration);
|
||||
rotateAnimation1.setDuration(3000); // 3 秒
|
||||
rotateAnimation1.setRepeatCount(Animation.INFINITE);
|
||||
rotateAnimation1.setRepeatMode(Animation.RESTART);
|
||||
rotateAnimation1.setInterpolator(getContext(), android.R.anim.linear_interpolator);
|
||||
@@ -239,31 +198,50 @@ public class QXMeetGiftView extends RelativeLayout {
|
||||
giftBgImageView.clearAnimation();
|
||||
}
|
||||
|
||||
// dp转px
|
||||
private int dp2px(int dp) {
|
||||
return (int) TypedValue.applyDimension(
|
||||
TypedValue.COMPLEX_UNIT_DIP,
|
||||
dp,
|
||||
getResources().getDisplayMetrics()
|
||||
);
|
||||
// 辅助方法:dp 转 px
|
||||
private int dpToPx(int dp) {
|
||||
float density = getResources().getDisplayMetrics().density;
|
||||
return Math.round(dp * density);
|
||||
}
|
||||
|
||||
// sp转px
|
||||
private int sp2px(int sp) {
|
||||
return (int) TypedValue.applyDimension(
|
||||
TypedValue.COMPLEX_UNIT_SP,
|
||||
sp,
|
||||
getResources().getDisplayMetrics()
|
||||
);
|
||||
// 缩放宽度方法(对应 ScaleWidth)
|
||||
private int scaleWidth(int value) {
|
||||
// 这里需要根据您的缩放逻辑实现
|
||||
// 通常可以根据屏幕密度进行缩放
|
||||
float scale = getResources().getDisplayMetrics().density;
|
||||
return (int) (value * scale);
|
||||
}
|
||||
|
||||
// Getter方法
|
||||
public TextView getGiftNameLabel() { return giftNameLabel; }
|
||||
public ImageView getGiftPriceBgView() { return giftPriceBgView; }
|
||||
public Button getGiftCoin() { return giftCoin; }
|
||||
public ImageView getBgImageView() { return bgImageView; }
|
||||
public ImageView getGiftBgImageView() { return giftBgImageView; }
|
||||
public ImageView getGiftImageView() { return giftImageView; }
|
||||
public boolean isLockGift() { return isLockGift; }
|
||||
public Object getModel() { return model; }
|
||||
}
|
||||
// Getter 方法
|
||||
public TextView getGiftNameLabel() {
|
||||
return giftNameLabel;
|
||||
}
|
||||
|
||||
public ImageView getGiftPriceBgView() {
|
||||
return giftPriceBgView;
|
||||
}
|
||||
|
||||
public Button getGiftCoin() {
|
||||
return giftCoin;
|
||||
}
|
||||
|
||||
public ImageView getBgImageView() {
|
||||
return bgImageView;
|
||||
}
|
||||
|
||||
public ImageView getGiftBgImageView() {
|
||||
return giftBgImageView;
|
||||
}
|
||||
|
||||
public ImageView getGiftImageView() {
|
||||
return giftImageView;
|
||||
}
|
||||
|
||||
public boolean isLockGift() {
|
||||
return isLockGift;
|
||||
}
|
||||
|
||||
public Object getModel() {
|
||||
return model;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user