20251204_002634_939

This commit is contained in:
2025-12-04 00:26:47 +08:00
parent fe5dd9e1ab
commit 59992542c4
3 changed files with 70 additions and 114 deletions

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Wed Dec 03 13:18:48 GMT 2025
#Wed Dec 03 16:25:43 GMT 2025
stageCount=13
libraryProject=
baseVersion=15.11
publishVersion=15.11.12
buildCount=140
buildCount=158
baseBetaVersion=15.11.13

View File

@@ -31,7 +31,7 @@ public class BackgroundView extends RelativeLayout {
// 上下文对象(全局复用)
private Context mContext;
private LinearLayout mllMain;
private RelativeLayout mrlMain;
// 背景图片显示控件核心子View
private ImageView mivBackground;
// 背景资源工具类(单例实例,避免重复创建)
@@ -98,7 +98,7 @@ public class BackgroundView extends RelativeLayout {
*/
private void initView() {
LogUtils.d(TAG, "=== initView视图初始化启动 ===");
//mllMain = inflate(mContext, R.layout.view_background, null);
// mllMain.setBackgroundColor(0x00000000); // 全透明背景ARGB透明通道+黑色,无视觉影响)
// mllMain.setBackground(new ColorDrawable(0x00000000)); // 双重保障兼容Android低版本确保背景透明
@@ -126,11 +126,11 @@ public class BackgroundView extends RelativeLayout {
*/
private void initBackgroundImageView() {
LogUtils.d(TAG, "=== initBackgroundImageView内部ImageView初始化启动 ===");
mllMain = (LinearLayout)inflate(mContext, R.layout.view_background, null);
mllMain.setBackgroundColor(0x00000000); // 全透明背景ARGB透明通道+黑色,无视觉影响)
mllMain.setBackground(new ColorDrawable(0x00000000)); // 双重保障兼容Android低版本确保背景透明
mivBackground = mllMain.findViewById(R.id.bg_imageview);
mrlMain = (RelativeLayout)inflate(mContext, R.layout.view_background, null);
mrlMain.setBackgroundColor(0x00000000); // 全透明背景ARGB透明通道+黑色,无视觉影响)
mrlMain.setBackground(new ColorDrawable(0x00000000)); // 双重保障兼容Android低版本确保背景透明
mivBackground = mrlMain.findViewById(R.id.bg_imageview);
//mivBackground = new ImageView(mContext);
// 配置ImageView布局参数宽高自适应 + 居中显示 + 无内边距/外边距
// RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
@@ -140,13 +140,13 @@ public class BackgroundView extends RelativeLayout {
// mivBackground.setLayoutParams(layoutParams);
// 配置ImageView显示属性保持比例 + 全透明背景
mivBackground.setScaleType(ImageView.ScaleType.FIT_CENTER); // 缩放模式:保持比例,完整显示图片
//mivBackground.setPadding(0, 0, 0, 0); // 取消内边距避免图片与ImageView间出现缝隙
//mivBackground.setBackgroundColor(0x00000000); // ImageView自身背景全透明
//mivBackground.setBackground(new ColorDrawable(0x00000000)); // 低版本兼容,确保透明
// mivBackground.setScaleType(ImageView.ScaleType.FIT_CENTER); // 缩放模式:保持比例,完整显示图片
// mivBackground.setPadding(0, 0, 0, 0); // 取消内边距避免图片与ImageView间出现缝隙
// mivBackground.setBackgroundColor(0x00000000); // ImageView自身背景全透明
// mivBackground.setBackground(new ColorDrawable(0x00000000)); // 低版本兼容,确保透明
// 将ImageView添加到当前控件父容器
this.addView(mllMain);
this.addView(mrlMain);
LogUtils.d(TAG, "=== initBackgroundImageView内部ImageView初始化完成 ===");
}
@@ -168,62 +168,9 @@ public class BackgroundView extends RelativeLayout {
}
// 加载背景图片路径
String szBackgroundPath = backgroundBean.isUseBackgroundScaledCompressFile()? backgroundBean.getBackgroundScaledCompressFilePath() : backgroundBean.getBackgroundFilePath();
File szBackgroundFile = checkFileValidity(szBackgroundPath); // 校验压缩图文件有效性
if (szBackgroundFile != null) {
loadAndSetImageViewBackground(szBackgroundPath);
LogUtils.d(TAG, "=== loadBackground 加载背景完成===");
return;
}
// 3. 压缩图和原图均无效时,显示默认透明背景
LogUtils.e(TAG, "【loadBackground】压缩图和原图均无效显示默认背景");
setDefaultBackground();
LogUtils.d(TAG, "=== loadBackground重新加载预览背景完成显示默认背景===");
}
/**
* 校验图片文件有效性(路径非空+文件存在+是文件+大小>100bytes
* 统一校验逻辑,避免重复代码
* @param filePath 待校验的图片路径
* @return 有效则返回File对象无效则返回null
*/
@Nullable
private File checkFileValidity(String filePath) {
LogUtils.d(TAG, "=== checkFileValidity文件有效性校验启动 ===");
// 1. 校验路径是否为空
if (TextUtils.isEmpty(filePath)) {
LogUtils.w(TAG, "【checkFileValidity】校验失败图片路径为空");
LogUtils.d(TAG, "=== checkFileValidity文件有效性校验完成路径为空===");
return null;
}
File file = new File(filePath);
// 2. 校验文件是否存在
if (!file.exists()) {
LogUtils.w(TAG, "【checkFileValidity】校验失败文件不存在路径" + filePath);
LogUtils.d(TAG, "=== checkFileValidity文件有效性校验完成文件不存在===");
return null;
}
// 3. 校验是否为文件(避免是文件夹)
if (!file.isFile()) {
LogUtils.w(TAG, "【checkFileValidity】校验失败不是文件路径" + filePath);
LogUtils.d(TAG, "=== checkFileValidity文件有效性校验完成不是文件===");
return null;
}
// 4. 校验文件大小(>100bytes视为有效避免空文件
if (file.length() <= 100) {
LogUtils.w(TAG, "【checkFileValidity】校验失败文件过小无效路径" + filePath + ",大小:" + file.length() + "bytes");
LogUtils.d(TAG, "=== checkFileValidity文件有效性校验完成文件过小===");
return null;
}
// 所有校验通过,文件有效
LogUtils.d(TAG, "【checkFileValidity】校验成功文件有效路径" + filePath + ",大小:" + file.length() + "bytes");
LogUtils.d(TAG, "=== checkFileValidity文件有效性校验完成校验通过===");
return file;
String szBackgroundPath = backgroundBean.isUseBackgroundScaledCompressFile() ? backgroundBean.getBackgroundScaledCompressFilePath() : backgroundBean.getBackgroundFilePath();
loadAndSetImageViewBackground(szBackgroundPath);
LogUtils.d(TAG, "=== loadBackground 加载背景完成===");
}
/**
@@ -339,56 +286,66 @@ public class BackgroundView extends RelativeLayout {
* 适配场景:控件初始化、图片加载后、父容器尺寸变化(如屏幕旋转)
*/
private void adjustImageViewSize() {
//LogUtils.d(TAG, "=== adjustImageViewSize调整ImageView尺寸启动 ===");
// 获取当前控件尺寸(已完全填充父视图,即父容器尺寸
int parentWidth = getWidth();
int parentHeight = getHeight();
LogUtils.d(TAG, "=== adjustImageViewSize调整ImageView尺寸启动 ===");
// 1. 绑定布局控件匹配xml中bg_main容器和bg_imageview
if (mrlMain == null || mivBackground == null) {
LogUtils.e(TAG, "【调整失败】布局控件绑定失败bg_main或bg_imageview为空");
return;
}
// 父容器未测量完成(宽/高为0延迟调整避免尺寸计算错误
if (parentWidth == 0 || parentHeight == 0) {
post(new Runnable() {
// 2. 获取根容器bg_main尺寸以bg_main为基准实现居中平铺
int parentWidth = mrlMain.getWidth();
int parentHeight = mrlMain.getHeight();
// 3. bg_main未测量完成宽/高为0延迟调整避免尺寸计算错误
if (parentWidth == 0 || parentHeight == 0) {
postDelayed(new Runnable() {
@Override
public void run() {
adjustImageViewSize(); // 延迟后重新调整
}
});
return;
}
}, 10); // 10ms延迟平衡响应速度与测量稳定性
return;
}
// 初始化ImageView目标宽高
int imageViewWidth, imageViewHeight;
// 4. 初始化ImageView目标宽高核心严格保持原图宽高比适配bg_main居中平铺
int imageViewWidth, imageViewHeight;
float imageRatio = mnImageAspectRatio; // 原图宽高比(宽/高,确保比例不变)
float parentRatio = (float) parentWidth / parentHeight; // bg_main容器宽高比宽/高)
// 核心逻辑:按图片宽高比计算ImageView尺寸确保不拉伸)
if (mnImageAspectRatio >= 1.0f) { // 横图(宽 ≥ 高):优先填满父控件宽度
imageViewWidth = parentWidth; // ImageView宽度 = 父控件宽度(填满
imageViewHeight = Math.round(imageViewWidth / mnImageAspectRatio); // 高度按比例计算
// 若计算出的高度超过父控件高度,按父控件高度重新计算(确保完全显示在父控件内)
if (imageViewHeight > parentHeight) {
imageViewHeight = parentHeight;
imageViewWidth = Math.round(imageViewHeight * mnImageAspectRatio);
}
} else { // 竖图(宽 < 高):优先填满父控件高度
imageViewHeight = parentHeight; // ImageView高度 = 父控件高度(填满)
imageViewWidth = Math.round(imageViewHeight * mnImageAspectRatio); // 宽度按比例计算
// 若计算出的宽度超过父控件宽度,按父控件宽度重新计算
if (imageViewWidth > parentWidth) {
imageViewWidth = parentWidth;
imageViewHeight = Math.round(imageViewWidth / mnImageAspectRatio);
}
}
// 关键逻辑:按比计算ImageView最大尺寸(完整显示图片+适配bg_main+无拉伸)
if (imageRatio >= parentRatio) {
// 情况1原图比bg_main更宽横图→ 按bg_main宽度缩放高度按原图比例适配横向填满纵向不超出
imageViewWidth = parentWidth; // 宽度=bg_main宽度横向平铺填满
imageViewHeight = Math.round(imageViewWidth / imageRatio); // 高度按原图比例计算
} else {
// 情况2原图比bg_main更高竖图→ 按bg_main高度缩放宽度按原图比例适配纵向填满横向不超出
imageViewHeight = parentHeight; // 高度=bg_main高度纵向平铺填满
imageViewWidth = Math.round(imageViewHeight * imageRatio); // 宽度按原图比例计算
}
// 应用计算出的尺寸到ImageView更新布局参数
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mivBackground.getLayoutParams();
layoutParams.width = imageViewWidth;
layoutParams.height = imageViewHeight;
mivBackground.setLayoutParams(layoutParams);
// 5. 应用尺寸到ImageView匹配xml布局参数确保居中
// 适配xml中RelativeLayoutLayoutParams(与布局文件根容器一致)
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mivBackground.getLayoutParams();
layoutParams.width = imageViewWidth; // 按比例计算的宽度适配bg_main
layoutParams.height = imageViewHeight; // 按比例计算的高度适配bg_main
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); // 强制居中匹配需求图片在bg_main中居中
mivBackground.setLayoutParams(layoutParams);
LogUtils.d(TAG, "【adjustImageViewSize】尺寸调整成功");
LogUtils.d(TAG, "→ 父控件尺寸:" + parentWidth + "x" + parentHeight);
LogUtils.d(TAG, "→ ImageView调整后尺寸" + imageViewWidth + "x" + imageViewHeight);
LogUtils.d(TAG, "→ 图片宽高比:" + mnImageAspectRatio);
LogUtils.d(TAG, "=== adjustImageViewSize调整ImageView尺寸完成调整成功===");
}
// 6. 关键设置缩放模式匹配xml布局+保持比例+居中平铺)
mivBackground.setScaleType(ImageView.ScaleType.FIT_CENTER);
// 确保ImageView背景透明避免遮挡适配布局视觉效果
mivBackground.setBackgroundColor(0x00000000);
LogUtils.d(TAG, "【适配布局】设置FIT_CENTER缩放模式+透明背景匹配xml布局的居中平铺需求");
// 7. 日志验证(确保比例一致+适配成功)
LogUtils.d(TAG, "【adjustImageViewSize】尺寸调整成功适配bg_main布局保持原图比例+居中平铺)");
LogUtils.d(TAG, "→ bg_main容器尺寸" + parentWidth + "x" + parentHeight);
LogUtils.d(TAG, "→ bg_imageview调整后尺寸" + imageViewWidth + "x" + imageViewHeight);
LogUtils.d(TAG, "→ 原图宽高比:" + mnImageAspectRatio + ",调整后宽高比:" + (float)imageViewWidth/imageViewHeight); // 验证比例无变形
LogUtils.d(TAG, "=== adjustImageViewSize调整ImageView尺寸完成调整成功===");
}
/**
* 带压缩的Bitmap解码通用方法

View File

@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bg_main">
@@ -11,5 +10,5 @@
android:layout_height="wrap_content"
android:id="@+id/bg_imageview"/>
</LinearLayout>
</RelativeLayout>