Files
midi-android/moduleUtil/src/main/java/com/xscm/moduleutil/view/QXMeetUserView.java
2025-09-26 23:57:00 +08:00

252 lines
8.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.xscm.moduleutil.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
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.UserInfo;
import com.xscm.moduleutil.bean.blindboxwheel.BlindBoxBean;
import com.xscm.moduleutil.utils.ImageUtils;
import com.xscm.moduleutil.widget.GifAvatarOvalView;
public class QXMeetUserView extends RelativeLayout {
private GifAvatarOvalView headerImageView;
private ImageView dressImageView;
private TextView tagLabel;
private TextView nameLabel;
private boolean isLuckUser;
private Object model; // 这里用 Object 代替 QXUserModel
public QXMeetUserView(Context context) {
super(context);
initSubviews(context);
}
public QXMeetUserView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initSubviews(context);
}
public QXMeetUserView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initSubviews(context);
}
private void initSubviews(Context context) {
setClipChildren(false);
setClipToPadding(false);
// 创建头像图片视图
headerImageView = new GifAvatarOvalView(context);
headerImageView.setScaleType(ImageView.ScaleType.FIT_CENTER); // 添加这一行
// headerImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
// headerImageView.setImageResource(R.mipmap.default_avatar);
int headerSize = getMeasuredWidth() - dpToPx(18); // self.width-9*2
LayoutParams headerParams = new LayoutParams(headerSize, headerSize);
headerParams.setMargins(0, 10, 0, 0);
headerParams.addRule(CENTER_IN_PARENT);
// 将头像添加到装饰视图之上
addView(headerImageView, headerParams);
// 创建装饰图片视图
dressImageView = new ImageView(context);
dressImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
dressImageView.setImageResource(R.mipmap.xlh_image);
LayoutParams dressParams = new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT
);
addView(dressImageView, dressParams);
// 创建标签标签
tagLabel = new TextView(context);
tagLabel.setTextColor(0xFFFFE554); // RGB16(0xFFE554)
tagLabel.setTextSize(12);
tagLabel.setGravity(android.view.Gravity.CENTER);
tagLabel.setBackground(getRoundedRectBackground(0xFF8D6F28, dpToPx(8))); // 默认房主背景色
LayoutParams tagParams = new LayoutParams(dpToPx(45), dpToPx(16));
tagParams.addRule(CENTER_HORIZONTAL);
// 需要在测量完成后设置底部位置
addView(tagLabel, tagParams);
// 创建名称标签
nameLabel = new TextView(context);
nameLabel.setTextColor(0xFFFFFFFF); // RGB16(0xffffff)
nameLabel.setTextSize(12);
nameLabel.setText("虚位以待");
nameLabel.setGravity(android.view.Gravity.CENTER);
LayoutParams nameParams = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
nameParams.addRule(CENTER_HORIZONTAL);
nameParams.addRule(ALIGN_PARENT_BOTTOM);
addView(nameLabel, nameParams);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 确保在测量时设置正确的头像尺寸
if (headerImageView != null) {
int headerSize = getMeasuredWidth() - dpToPx(18); // width - 9dp * 2
LayoutParams headerParams = (LayoutParams) headerImageView.getLayoutParams();
if (headerParams != null) {
headerParams.width = headerSize;
headerParams.height = headerSize;
headerParams.leftMargin = dpToPx(9);
headerParams.topMargin = dpToPx(9);
headerImageView.setLayoutParams(headerParams);
}
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
// 在布局完成后设置标签的位置(在头像底部-8的位置
if (changed && headerImageView != null && tagLabel != null) {
int tagTop = headerImageView.getBottom() - dpToPx(8);
int tagLeft = (getWidth() - tagLabel.getWidth()) / 2;
tagLabel.layout(tagLeft, tagTop, tagLeft + tagLabel.getWidth(), tagTop + tagLabel.getHeight());
}
}
public void setIsLuckUser(boolean isLuckUser) {
this.isLuckUser = isLuckUser;
if (isLuckUser) {
tagLabel.setTextColor(0xFFFFFFFF); // RGB16(0xffffff)
tagLabel.setBackground(getRoundedRectBackground(0xFF6C49E4, dpToPx(8))); // RGB16(0x6C49E4)
// tagLabel.setBackgroundColor(getResources().getColor(R.color.color_FF6C49E4)); // RGB16(0x6C49E4)
tagLabel.setText("幸运者");
} else {
tagLabel.setTextColor(0xFFFFE554); // RGB16(0xFFE554)
tagLabel.setBackground(getRoundedRectBackground(0xFF8D6F28, dpToPx(8))); // RGB16(0x8D6F28)
// tagLabel.setBackgroundColor(getResources().getColor(R.color.color_FF8D6F28)); // RGB16(0x6C49E4)
tagLabel.setText("房主");
}
}
public void setModel(BlindBoxBean.xlhUser model) {
this.model = model;
// 这里需要根据您的 QXUserModel 类来实现具体逻辑
if (model instanceof BlindBoxBean.xlhUser) {
BlindBoxBean.xlhUser userModel = (BlindBoxBean.xlhUser) model;
// 使用图片加载库加载头像
// Glide.with(getContext()).load(userModel.getAvatar()).into(headerImageView);
ImageUtils.loadHeadCC(userModel.getAvatar(), headerImageView);
nameLabel.setText(userModel.getNickname());
}
}
public void resetView() {
headerImageView.setImageResource(R.mipmap.default_avatar);
nameLabel.setText("虚位以待");
}
// 创建圆角矩形背景
private android.graphics.drawable.Drawable getRoundedRectBackground(int color, float radius) {
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(color);
drawable.setCornerRadius(radius);
return drawable;
}
// 辅助方法dp 转 px
private int dpToPx(int dp) {
float density = getResources().getDisplayMetrics().density;
return Math.round(dp * density);
}
// Getter 方法
public ImageView getHeaderImageView() {
return headerImageView;
}
public ImageView getDressImageView() {
return dressImageView;
}
public TextView getTagLabel() {
return tagLabel;
}
public TextView getNameLabel() {
return nameLabel;
}
public boolean isLuckUser() {
return isLuckUser;
}
public Object getModel() {
return model;
}
// 自定义圆形 ImageView
private static class RoundImageView extends androidx.appcompat.widget.AppCompatImageView {
public RoundImageView(Context context) {
super(context);
}
public RoundImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public RoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
// 创建圆形裁剪区域
int diameter = Math.min(getWidth(), getHeight());
Bitmap bitmap = Bitmap.createBitmap(diameter, diameter, Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bitmap);
// 绘制圆形
Paint paint = new Paint();
paint.setAntiAlias(true);
tempCanvas.drawCircle(diameter / 2f, diameter / 2f, diameter / 2f, paint);
// 设置混合模式
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
// 绘制原始图片
super.onDraw(tempCanvas);
// 将处理后的图片绘制到实际canvas
canvas.drawBitmap(bitmap, 0, 0, null);
}
}
}