From e190d3ff396819b947756547ee310dd709e4ee6f Mon Sep 17 00:00:00 2001 From: ZhanGSKen Date: Tue, 2 Dec 2025 13:58:04 +0800 Subject: [PATCH] =?UTF-8?q?BackgroundView=20=E7=B1=BB=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- powerbell/build.properties | 4 +- .../BackgroundSettingsActivity.java | 64 +++--- .../dialogs/NetworkBackgroundDialog.java | 16 +- .../powerbell/fragments/MainViewFragment.java | 27 +-- .../powerbell/views/BackgroundView.java | 191 +++++------------- 5 files changed, 96 insertions(+), 206 deletions(-) diff --git a/powerbell/build.properties b/powerbell/build.properties index c1f1327c..a1a2f7fd 100644 --- a/powerbell/build.properties +++ b/powerbell/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Mon Dec 01 21:56:35 GMT 2025 +#Tue Dec 02 05:57:17 GMT 2025 stageCount=13 libraryProject= baseVersion=15.11 publishVersion=15.11.12 -buildCount=106 +buildCount=107 baseBetaVersion=15.11.13 diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/activities/BackgroundSettingsActivity.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/activities/BackgroundSettingsActivity.java index 29d990f6..10726a14 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/activities/BackgroundSettingsActivity.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/activities/BackgroundSettingsActivity.java @@ -72,7 +72,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity implements Backg // 控件实例 private AToolbar mAToolbar; - private BackgroundView bvPreviewBackground; + private BackgroundView mBackgroundView; // 拍照临时文件(仅拍照用,路径由工具类间接管理) private File mfTakePhoto; // 配置标记(是否提交设置) @@ -97,7 +97,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity implements Backg setContentView(R.layout.activity_background_settings); // 初始化核心控件 - bvPreviewBackground = (BackgroundView) findViewById(R.id.activitybackgroundpictureBackgroundView1); + mBackgroundView = (BackgroundView) findViewById(R.id.activitybackgroundpictureBackgroundView1); // 初始化工具类(单例模式,全局复用,解耦业务逻辑) mBgSourceUtils = BackgroundSourceUtils.getInstance(this); mPermissionUtils = PermissionUtils.getInstance(); @@ -119,7 +119,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity implements Backg // 初始化UI及数据 initToolbar(); initClickListeners(); - initPreviewBeanFromFormal(); // 正式Bean → 预览Bean(确保预览数据准确) + initBackgroundViewByPreviewBean(); // 正式Bean → 预览Bean(确保预览数据准确) handleShareIntent(); // 处理分享图片意图 LogUtils.d(TAG, "【初始化】BackgroundSettingsActivity 初始化完成(Java7 语法版)"); @@ -128,10 +128,10 @@ public class BackgroundSettingsActivity extends WinBoLLActivity implements Backg /** * 初始化预览Bean:从正式Bean拷贝所有属性(确保每次操作都是正式图的副本) */ - private void initPreviewBeanFromFormal() { + private void initBackgroundViewByPreviewBean() { LogUtils.d(TAG, "【Bean初始化】正式Bean → 预览Bean(拷贝初始化)"); mBgSourceUtils.setCurrentSourceToPreview(); - bvPreviewBackground.reloadPreviewBackground(); // 加载预览图 + doubleRefreshPreview(); LogUtils.d(TAG, "【Bean初始化】预览Bean初始化完成,当前预览路径:" + mBgSourceUtils.getPreviewBackgroundScaledCompressFilePath()); } @@ -200,14 +200,14 @@ public class BackgroundSettingsActivity extends WinBoLLActivity implements Backg /** * 更新背景预览(强制使用预览Bean,全程不操作正式Bean) */ - public void updateBackgroundView(File sourceFile, String sourceFileInfo) { - LogUtils.d(TAG, "【预览更新】updateBackgroundView 触发,sourceFile是否为空:" + (sourceFile == null)); - if (sourceFile != null) { - mBgSourceUtils.saveFileToPreviewBean(sourceFile, sourceFileInfo); // 同步到预览Bean - } - bvPreviewBackground.reloadPreviewBackground(); // 强制加载预览Bean - LogUtils.d(TAG, "【预览更新】预览背景更新完成(强制使用previewBackgroundBean)"); - } +// public void updateBackgroundView(File sourceFile, String sourceFileInfo) { +// LogUtils.d(TAG, "【预览更新】updateBackgroundView 触发,sourceFile是否为空:" + (sourceFile == null)); +// if (sourceFile != null) { +// mBgSourceUtils.saveFileToPreviewBean(sourceFile, sourceFileInfo); // 同步到预览Bean +// } +// mBackgroundView.loadBackground2(mBgSourceUtils.getPreviewBackgroundBean()); +// LogUtils.d(TAG, "【预览更新】预览背景更新完成(强制使用previewBackgroundBean)"); +// } // ======================================== 按钮点击事件(仅UI交互) ======================================== /** @@ -218,14 +218,14 @@ public class BackgroundSettingsActivity extends WinBoLLActivity implements Backg public void onClick(View v) { LogUtils.d(TAG, "【按钮点击】触发取消背景功能"); // 1. 修改正式Bean - BackgroundBean formalBean = mBgSourceUtils.getCurrentBackgroundBean(); - formalBean.setIsUseBackgroundFile(false); - formalBean.resetBackgroundConfig(); + BackgroundBean currentBackgroundBean = mBgSourceUtils.getCurrentBackgroundBean(); + currentBackgroundBean.setIsUseBackgroundFile(false); + currentBackgroundBean.resetBackgroundConfig(); mBgSourceUtils.saveSettings(); // 2. 同步预览Bean(关键优化:确保控件刷新后显示“取消背景”状态) mBgSourceUtils.setCurrentSourceToPreview(); // 3. 控件刷新(仍依赖预览Bean) - bvPreviewBackground.reloadPreviewBackground(); + doubleRefreshPreview(); ToastUtils.show("背景已取消"); } }; @@ -483,8 +483,9 @@ public class BackgroundSettingsActivity extends WinBoLLActivity implements Backg BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean(); previewBean.setIsUseBackgroundScaledCompressFile(false); mBgSourceUtils.saveSettings(); - bvPreviewBackground.reloadPreviewBackground(); - + + doubleRefreshPreview(); + File previewFile = mBgSourceUtils.getCropSourceFile(); // 裁剪缓存图片 // 2. 生成裁剪输入Uri(强化MIUI权限授予,直接用原图Uri) @@ -516,8 +517,8 @@ public class BackgroundSettingsActivity extends WinBoLLActivity implements Backg // 裁剪比例设置 if (!isCropFree) { - int viewWidth = bvPreviewBackground.getWidth() > 0 ? bvPreviewBackground.getWidth() : getResources().getDisplayMetrics().widthPixels; - int viewHeight = bvPreviewBackground.getHeight() > 0 ? bvPreviewBackground.getHeight() : getResources().getDisplayMetrics().heightPixels; + int viewWidth = mBackgroundView.getWidth() > 0 ? mBackgroundView.getWidth() : getResources().getDisplayMetrics().widthPixels; + int viewHeight = mBackgroundView.getHeight() > 0 ? mBackgroundView.getHeight() : getResources().getDisplayMetrics().heightPixels; int gcd = calculateGCD(viewWidth, viewHeight); intent.putExtra("aspectX", viewWidth / gcd); intent.putExtra("aspectY", viewHeight / gcd); @@ -899,8 +900,9 @@ public class BackgroundSettingsActivity extends WinBoLLActivity implements Backg // 同步预览并启动裁剪 mBgSourceUtils.saveFileToPreviewBean(mfTakePhoto, mfTakePhoto.getAbsolutePath()); - bvPreviewBackground.reloadPreviewBackground(); - startCropImageActivity(false); + doubleRefreshPreview(); + + startCropImageActivity(false); LogUtils.d(TAG, "【拍照完成】拍照回调处理结束,已启动裁剪"); } @@ -1088,8 +1090,8 @@ public class BackgroundSettingsActivity extends WinBoLLActivity implements Backg LogUtils.d(TAG, "【双重刷新】开始执行预览双重刷新(适配机型渲染延迟)"); // 1. 立即刷新(第一重:优先加载最新图片) - if (bvPreviewBackground != null && !isFinishing()) { - bvPreviewBackground.reloadPreviewBackground(); + if (mBackgroundView != null && !isFinishing()) { + mBackgroundView.loadBackground(mBgSourceUtils.getPreviewBackgroundBean()); LogUtils.d(TAG, "【双重刷新】第一重刷新完成(立即执行)"); } else { LogUtils.w(TAG, "【双重刷新】第一重刷新跳过:预览控件为空或页面即将销毁"); @@ -1101,8 +1103,8 @@ public class BackgroundSettingsActivity extends WinBoLLActivity implements Backg @Override public void run() { // 校验:页面未销毁+控件有效,才执行二次刷新 - if (bvPreviewBackground != null && !isFinishing()) { - bvPreviewBackground.reloadPreviewBackground(); + if (mBackgroundView != null && !isFinishing()) { + mBackgroundView.loadBackground(mBgSourceUtils.getPreviewBackgroundBean()); LogUtils.d(TAG, "【双重刷新】第二重刷新完成(延迟50ms执行)"); } else { LogUtils.w(TAG, "【双重刷新】第二重刷新跳过:页面已销毁或控件无效"); @@ -1192,7 +1194,8 @@ public class BackgroundSettingsActivity extends WinBoLLActivity implements Backg } // 刷新预览并启动裁剪(无需重复同步预览Bean,已提前绑定) - bvPreviewBackground.reloadPreviewBackground(); + doubleRefreshPreview(); + // 新增:延迟50ms启动裁剪(适配部分机型选图后文件写入延迟) new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { @Override @@ -1209,8 +1212,9 @@ public class BackgroundSettingsActivity extends WinBoLLActivity implements Backg * 处理所有操作取消/失败(统一清理+提示) */ private void handleOperationCancelOrFail() { - initPreviewBeanFromFormal(); - bvPreviewBackground.reloadPreviewBackground(); + initBackgroundViewByPreviewBean(); + doubleRefreshPreview(); + LogUtils.d(TAG, "【操作回调】操作取消或失败"); ToastUtils.show("操作已取消"); } diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/dialogs/NetworkBackgroundDialog.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/dialogs/NetworkBackgroundDialog.java index 26dc314b..84e664a3 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/dialogs/NetworkBackgroundDialog.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/dialogs/NetworkBackgroundDialog.java @@ -42,7 +42,7 @@ public class NetworkBackgroundDialog extends AlertDialog { private Button btnConfirm; private Button btnPreview; private EditText etURL; - BackgroundView bvBackgroundPreview; + BackgroundView mBackgroundView; Context mContext; // 主线程 Handler,用于接收子线程消息并更新 UI private Handler mUiHandler; @@ -95,7 +95,7 @@ public class NetworkBackgroundDialog extends AlertDialog { break; case MSG_IMAGE_LOAD_FAILED: // 图片加载失败,设置默认背景 - bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher); + mBackgroundView.setBackgroundResource(R.drawable.ic_launcher); ToastUtils.show("图片预览失败,请检查链接"); break; } @@ -137,9 +137,9 @@ public class NetworkBackgroundDialog extends AlertDialog { btnConfirm = (Button) dialogView.findViewById(R.id.btn_confirm); btnPreview = (Button) dialogView.findViewById(R.id.btn_preview); etURL = (EditText) dialogView.findViewById(R.id.et_url); - bvBackgroundPreview = (BackgroundView) dialogView.findViewById(R.id.bv_background_preview); + mBackgroundView = (BackgroundView) dialogView.findViewById(R.id.bv_background_preview); // 加载初始图片 - bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher); + mBackgroundView.setBackgroundResource(R.drawable.ic_launcher); // 设置按钮点击事件 setButtonClickListeners(); } @@ -199,7 +199,7 @@ public class NetworkBackgroundDialog extends AlertDialog { if (!imageFile.exists()) { ToastUtils.show("图片文件不存在:" + previewFilePath); LogUtils.e(TAG, "图片文件不存在:" + previewFilePath); - bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher); + mBackgroundView.setBackgroundResource(R.drawable.ic_launcher); return; } @@ -207,12 +207,10 @@ public class NetworkBackgroundDialog extends AlertDialog { mPreviewFilePath = previewFilePath; BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(mContext); utils.saveFileToPreviewBean(new File(mPreviewFilePath), mPreviewFileUrl); - bvBackgroundPreview.reloadPreviewBackground(); - - //ToastUtils.show("预览背景中。。。"); + mBackgroundView.loadBackground(utils.getPreviewBackgroundBean()); } catch (Exception e) { e.printStackTrace(); - bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher); + mBackgroundView.setBackgroundResource(R.drawable.ic_launcher); LogUtils.e(TAG, "图片预览失败:" + e.getMessage()); } finally { // Java7 手动关闭流,避免资源泄漏 diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/fragments/MainViewFragment.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/fragments/MainViewFragment.java index 17d5b916..e75f285a 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/fragments/MainViewFragment.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/fragments/MainViewFragment.java @@ -71,7 +71,7 @@ public class MainViewFragment extends Fragment { TextView mtvUsegeReminderValue; CheckBox mcbUsegeReminderValue; TextView mtvCurrentValue; - BackgroundView bvPreviewBackground; + BackgroundView mBackgroundView; @Override @@ -81,12 +81,12 @@ public class MainViewFragment extends Fragment { mAppConfigUtils = App.getAppConfigUtils(getActivity()); // 获取指定ID的View实例 - bvPreviewBackground = mView.findViewById(R.id.fragmentmainviewBackgroundView1); + mBackgroundView = mView.findViewById(R.id.fragmentmainviewBackgroundView1); BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(getActivity()); BackgroundBean bean = utils.getCurrentBackgroundBean(); int nPixelColor = bean.getPixelColor(); - bvPreviewBackground.setBackgroundColor(nPixelColor); + mBackgroundView.setBackgroundColor(nPixelColor); /*final View mainImageView = mView.findViewById(R.id.fragmentmainviewImageView1); // 注册OnGlobalLayoutListener @@ -155,7 +155,7 @@ public class MainViewFragment extends Fragment { BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(getActivity()); BackgroundBean bean = utils.getCurrentBackgroundBean(); int nPixelColor = bean.getPixelColor(); - bvPreviewBackground.setBackgroundColor(nPixelColor); + mBackgroundView.setBackgroundColor(nPixelColor); } @@ -320,22 +320,9 @@ public class MainViewFragment extends Fragment { } public void reloadBackground() { - bvPreviewBackground.reloadCurrentBackground(); -// BackgroundPictureBean bean = BackgroundPictureUtils.getInstance(getActivity()).getBackgroundPictureBean(); -// ImageView imageView = mView.findViewById(R.id.fragmentmainviewImageView1); -// String szBackgroundFilePath = BackgroundPictureUtils.getInstance(getActivity()).getBackgroundDir() + BackgroundPictureActivity.getBackgroundFileName(); -// File fBackgroundFilePath = new File(szBackgroundFilePath); -// LogUtils.d(TAG, "szBackgroundFilePath : " + szBackgroundFilePath); -// LogUtils.d(TAG, String.format("fBackgroundFilePath.exists() %s", fBackgroundFilePath.exists())); -// if (bean.isUseBackgroundFile() && fBackgroundFilePath.exists()) { -// Drawable drawableBackground = Drawable.createFromPath(szBackgroundFilePath); -// //drawableBackground.setAlpha(120); -// imageView.setImageDrawable(drawableBackground); -// } else { -// Drawable drawableBackground = getActivity().getDrawable(R.drawable.blank10x10); -// //drawableBackground.setAlpha(120); -// imageView.setImageDrawable(drawableBackground); -// } + BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(getActivity()); + BackgroundBean bean = utils.getCurrentBackgroundBean(); + mBackgroundView.loadBackground(bean); } Handler mHandler = new Handler(){ diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/views/BackgroundView.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/views/BackgroundView.java index f1102a30..b535564a 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/views/BackgroundView.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/views/BackgroundView.java @@ -30,14 +30,12 @@ public class BackgroundView extends RelativeLayout { // 上下文对象(全局复用) private Context mContext; // 背景图片显示控件(核心子View) - private ImageView ivBackground; + private ImageView mivBackground; // 背景资源工具类(单例实例,避免重复创建) - private BackgroundSourceUtils backgroundSourceUtils; + private BackgroundSourceUtils mBackgroundSourceUtils; // 图片原始宽高比(控制图片不拉伸的核心参数,宽/高) - private float imageAspectRatio = 1.0f; - // 预览模式标记(true:预览模式,false:正式模式,区分加载不同背景资源) - private boolean isPreviewMode = false; + private float mnImageAspectRatio = 1.0f; // ====================================== 构造器(兼容所有布局场景)====================================== /** @@ -104,13 +102,13 @@ public class BackgroundView extends RelativeLayout { setBackground(new ColorDrawable(0x00000000)); // 双重保障:兼容Android低版本,确保背景透明 // 2. 初始化背景资源工具类(单例模式,全局唯一实例) - backgroundSourceUtils = BackgroundSourceUtils.getInstance(mContext); + mBackgroundSourceUtils = BackgroundSourceUtils.getInstance(mContext); // 3. 初始化内部ImageView(背景图片显示核心控件) initBackgroundImageView(); // 4. 初始加载:默认加载正式模式背景 - reloadCurrentBackground(); + setDefaultBackground(); LogUtils.d(TAG, "=== initView(视图初始化)完成 ==="); } @@ -120,149 +118,55 @@ public class BackgroundView extends RelativeLayout { */ private void initBackgroundImageView() { LogUtils.d(TAG, "=== initBackgroundImageView(内部ImageView初始化)启动 ==="); - ivBackground = new ImageView(mContext); + mivBackground = new ImageView(mContext); // 配置ImageView布局参数:宽高自适应 + 居中显示 + 无内边距/外边距 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); // 核心:让ImageView在当前控件中居中 layoutParams.setMargins(0, 0, 0, 0); // 取消外边距,避免ImageView与控件间出现缝隙 - ivBackground.setLayoutParams(layoutParams); + mivBackground.setLayoutParams(layoutParams); // 配置ImageView显示属性:保持比例 + 全透明背景 - ivBackground.setScaleType(ImageView.ScaleType.FIT_CENTER); // 缩放模式:保持比例,完整显示图片 - ivBackground.setPadding(0, 0, 0, 0); // 取消内边距,避免图片与ImageView间出现缝隙 - ivBackground.setBackgroundColor(0x00000000); // ImageView自身背景全透明 - ivBackground.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(ivBackground); + this.addView(mivBackground); LogUtils.d(TAG, "=== initBackgroundImageView(内部ImageView初始化)完成 ==="); } - // ====================================== 对外提供的公共方法 ====================================== - /** - * 【对外提供】重新加载正式模式背景图片 - * 适用场景:退出预览模式、恢复默认背景、正式背景资源更新后刷新显示 - */ - public void reloadCurrentBackground() { - LogUtils.d(TAG, "=== reloadCurrentBackground(重新加载正式背景)启动 ==="); - isPreviewMode = false; // 标记为正式模式 - // 从工具类获取最新的正式背景图片路径(确保路径与全局同步) - String backgroundPath = backgroundSourceUtils.getCurrentBackgroundFilePath(); - // 加载并显示图片(复用统一的加载逻辑) - loadAndSetImageViewBackground(backgroundPath); - LogUtils.d(TAG, "=== reloadCurrentBackground(重新加载正式背景)完成 ==="); - } - /** * 【对外提供】重新加载预览模式背景图片 * 逻辑:优先加载压缩图(省内存)→ 压缩图无效则加载原图 → 均无效则显示默认透明背景 */ - public void reloadPreviewBackground() { - LogUtils.d(TAG, "=== reloadPreviewBackground(重新加载预览背景)启动 ==="); - // 获取背景资源工具类实例(单例) - BackgroundSourceUtils bgSourceUtils = BackgroundSourceUtils.getInstance(getContext()); - // 获取预览模式对应的背景Bean(存储预览图路径等信息) - BackgroundBean previewBean = bgSourceUtils.getPreviewBackgroundBean(); - + public void loadBackground(BackgroundBean backgroundBean) { + LogUtils.d(TAG, "=== loadBackground(加载预览背景)启动 ==="); // 校验预览Bean是否为空,为空则直接显示默认背景 - if (previewBean == null) { - LogUtils.e(TAG, "【reloadPreviewBackground】预览Bean为空,无法加载预览图"); + if (backgroundBean == null || backgroundBean.isUseBackgroundFile()) { + LogUtils.e(TAG, "【loadBackground】预览Bean为空,正在加载默认图片"); + setDefaultBackground(); + return; + } else if (backgroundBean.isUseBackgroundFile()) { + LogUtils.e(TAG, "【loadBackground】设置为不使用图片,正在加载默认图片"); setDefaultBackground(); - LogUtils.d(TAG, "=== reloadPreviewBackground(重新加载预览背景)完成(预览Bean为空)==="); return; } - // 1. 优先加载压缩图路径(预览模式首选,节省内存) - String compressPath = previewBean.getBackgroundScaledCompressFilePath(); - File compressFile = checkFileValidity(compressPath); // 校验压缩图文件有效性 - if (compressFile != null) { - loadPreviewImageByFile(compressFile, "压缩图"); // 加载压缩图 - LogUtils.d(TAG, "=== reloadPreviewBackground(重新加载预览背景)完成(加载压缩图成功)==="); + // 加载背景图片路径 + String szBackgroundPath = backgroundBean.isUseBackgroundScaledCompressFile()? backgroundBean.getBackgroundScaledCompressFilePath() : backgroundBean.getBackgroundFilePath(); + File szBackgroundFile = checkFileValidity(szBackgroundPath); // 校验压缩图文件有效性 + if (szBackgroundFile != null) { + loadAndSetImageViewBackground(szBackgroundPath); + LogUtils.d(TAG, "=== loadBackground 加载背景完成==="); return; } - - // 2. 压缩图无效时,兜底加载原图路径 - LogUtils.w(TAG, "【reloadPreviewBackground】压缩图无效,尝试加载原图"); - String originalPath = previewBean.getBackgroundFilePath(); - File originalFile = checkFileValidity(originalPath); // 校验原图文件有效性 - if (originalFile != null) { - loadPreviewImageByFile(originalFile, "原图"); // 加载原图 - LogUtils.d(TAG, "=== reloadPreviewBackground(重新加载预览背景)完成(加载原图成功)==="); - return; - } - + // 3. 压缩图和原图均无效时,显示默认透明背景 - LogUtils.e(TAG, "【reloadPreviewBackground】压缩图和原图均无效,显示默认背景"); + LogUtils.e(TAG, "【loadBackground】压缩图和原图均无效,显示默认背景"); setDefaultBackground(); - LogUtils.d(TAG, "=== reloadPreviewBackground(重新加载预览背景)完成(显示默认背景)==="); - } - - /** - * 【对外提供】预览指定路径的临时图片(直接传入路径,不依赖BackgroundBean) - * 适用场景:临时预览本地图片、测试图片等无需存入Bean的场景 - * @param previewImagePath 临时图片的绝对路径(为空则显示透明背景) - */ - public void previewBackgroundImage(String previewImagePath) { - LogUtils.d(TAG, "=== previewBackgroundImage(预览指定路径图片)启动 ==="); - isPreviewMode = true; // 标记为预览模式 - // 加载并显示指定路径的图片(复用统一的加载逻辑) - loadAndSetImageViewBackground(previewImagePath); - LogUtils.d(TAG, "=== previewBackgroundImage(预览指定路径图片)完成 ==="); - } - - /** - * 【对外提供】获取当前是否处于预览模式 - * 适用场景:Activity中判断当前背景模式,决定是否提交预览背景到正式模式 - * @return true:预览模式,false:正式模式 - */ - public boolean isPreviewMode() { - LogUtils.d(TAG, "=== isPreviewMode(获取当前模式)启动 ==="); - LogUtils.d(TAG, "【isPreviewMode】当前模式:" + (isPreviewMode ? "预览模式" : "正式模式")); - LogUtils.d(TAG, "=== isPreviewMode(获取当前模式)完成 ==="); - return isPreviewMode; - } - - // ====================================== 内部私有工具方法 ====================================== - /** - * 预览模式专用:按文件加载图片(复用比例计算逻辑,确保图片不拉伸) - * @param imageFile 待加载的图片文件 - * @param imageType 图片类型描述(如"压缩图"、"原图",用于日志区分) - */ - private void loadPreviewImageByFile(File imageFile, String imageType) { - LogUtils.d(TAG, "=== loadPreviewImageByFile(预览模式加载" + imageType + ")启动 ==="); - isPreviewMode = true; // 标记为预览模式(双重确认) - - // 1. 计算图片原始宽高比(核心:确保图片不拉伸的关键) - if (!calculateImageAspectRatio(imageFile)) { - LogUtils.e(TAG, "【loadPreviewImageByFile】" + imageType + "宽高比计算失败,图片尺寸无效"); - setDefaultBackground(); - LogUtils.d(TAG, "=== loadPreviewImageByFile(预览模式加载" + imageType + ")完成(尺寸无效)==="); - return; - } - - // 2. 压缩加载Bitmap(避免OOM,同时保持原始比例) - Bitmap bitmap = decodeBitmapWithCompress(imageFile, 1080, 1920); - // 校验Bitmap有效性(为空或宽高<=0均视为无效) - if (bitmap == null || bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) { - LogUtils.e(TAG, "【loadPreviewImageByFile】" + imageType + "加载失败,Bitmap无效"); - setDefaultBackground(); - LogUtils.d(TAG, "=== loadPreviewImageByFile(预览模式加载" + imageType + ")完成(Bitmap无效)==="); - return; - } - - // 3. 配置ImageView并设置图片(保持比例+全透明背景) - ivBackground.setScaleType(ScaleType.FIT_CENTER); // 缩放模式:保持比例,完整显示 - ivBackground.setBackgroundColor(0x00000000); // 确保ImageView背景透明,不覆盖图片 - ivBackground.setImageBitmap(bitmap); // 设置图片到ImageView - - // 4. 调整ImageView尺寸(按图片比例扩展到父控件,确保居中显示) - adjustImageViewSize(); - - LogUtils.d(TAG, "【loadPreviewImageByFile】" + imageType + "加载成功"); - LogUtils.d(TAG, "→ 图片尺寸:" + bitmap.getWidth() + "x" + bitmap.getHeight()); - LogUtils.d(TAG, "→ 图片宽高比:" + imageAspectRatio); - LogUtils.d(TAG, "=== loadPreviewImageByFile(预览模式加载" + imageType + ")完成(加载成功)==="); + LogUtils.d(TAG, "=== loadBackground(重新加载预览背景)完成(显示默认背景)==="); } /** @@ -312,9 +216,8 @@ public class BackgroundView extends RelativeLayout { /** * 预览模式兜底:设置默认透明背景(无有效图片时使用,避免显示白色背景) */ - private void setDefaultBackground() { + public void setDefaultBackground() { LogUtils.d(TAG, "=== setDefaultBackground(设置预览默认透明背景)启动 ==="); - isPreviewMode = true; // 标记为预览模式 setBackgroundColor(0x00000000); // 当前控件背景全透明 adjustImageViewSize(); // 调整ImageView尺寸,确保居中且不占位异常 LogUtils.d(TAG, "【setDefaultBackground】已设置预览默认透明背景"); @@ -365,18 +268,17 @@ public class BackgroundView extends RelativeLayout { // 5. 设置图片到ImageView(兼容低版本系统) Drawable backgroundDrawable = new BitmapDrawable(mContext.getResources(), bitmap); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { - ivBackground.setBackground(backgroundDrawable); // API 16+ 方法 + mivBackground.setBackground(backgroundDrawable); // API 16+ 方法 } else { - ivBackground.setBackgroundDrawable(backgroundDrawable); // 低版本兼容方法 + mivBackground.setBackgroundDrawable(backgroundDrawable); // 低版本兼容方法 } // 6. 调整ImageView尺寸(按图片比例扩展到父控件,确保不拉伸) adjustImageViewSize(); LogUtils.d(TAG, "【loadAndSetImageViewBackground】图片加载成功"); - LogUtils.d(TAG, "→ 当前模式:" + (isPreviewMode ? "预览模式" : "正式模式")); LogUtils.d(TAG, "→ 图片路径:" + imagePath); - LogUtils.d(TAG, "→ 图片宽高比:" + imageAspectRatio); + LogUtils.d(TAG, "→ 图片宽高比:" + mnImageAspectRatio); LogUtils.d(TAG, "=== loadAndSetImageViewBackground(加载并设置图片)完成(加载成功)==="); } @@ -405,10 +307,10 @@ public class BackgroundView extends RelativeLayout { } // 计算并保存图片原始宽高比(宽/高) - imageAspectRatio = (float) imageWidth / imageHeight; + mnImageAspectRatio = (float) imageWidth / imageHeight; LogUtils.d(TAG, "【calculateImageAspectRatio】计算成功"); LogUtils.d(TAG, "→ 图片原始尺寸:" + imageWidth + "x" + imageHeight); - LogUtils.d(TAG, "→ 图片宽高比:" + imageAspectRatio); + LogUtils.d(TAG, "→ 图片宽高比:" + mnImageAspectRatio); LogUtils.d(TAG, "=== calculateImageAspectRatio(计算图片宽高比)完成(计算成功)==="); return true; } catch (Exception e) { @@ -446,35 +348,34 @@ public class BackgroundView extends RelativeLayout { int imageViewWidth, imageViewHeight; // 核心逻辑:按图片宽高比计算ImageView尺寸(确保不拉伸) - if (imageAspectRatio >= 1.0f) { // 横图(宽 ≥ 高):优先填满父控件宽度 + if (mnImageAspectRatio >= 1.0f) { // 横图(宽 ≥ 高):优先填满父控件宽度 imageViewWidth = parentWidth; // ImageView宽度 = 父控件宽度(填满) - imageViewHeight = Math.round(imageViewWidth / imageAspectRatio); // 高度按比例计算 + imageViewHeight = Math.round(imageViewWidth / mnImageAspectRatio); // 高度按比例计算 // 若计算出的高度超过父控件高度,按父控件高度重新计算(确保完全显示在父控件内) if (imageViewHeight > parentHeight) { imageViewHeight = parentHeight; - imageViewWidth = Math.round(imageViewHeight * imageAspectRatio); + imageViewWidth = Math.round(imageViewHeight * mnImageAspectRatio); } } else { // 竖图(宽 < 高):优先填满父控件高度 imageViewHeight = parentHeight; // ImageView高度 = 父控件高度(填满) - imageViewWidth = Math.round(imageViewHeight * imageAspectRatio); // 宽度按比例计算 + imageViewWidth = Math.round(imageViewHeight * mnImageAspectRatio); // 宽度按比例计算 // 若计算出的宽度超过父控件宽度,按父控件宽度重新计算 if (imageViewWidth > parentWidth) { imageViewWidth = parentWidth; - imageViewHeight = Math.round(imageViewWidth / imageAspectRatio); + imageViewHeight = Math.round(imageViewWidth / mnImageAspectRatio); } } // 应用计算出的尺寸到ImageView(更新布局参数) - RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ivBackground.getLayoutParams(); + RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mivBackground.getLayoutParams(); layoutParams.width = imageViewWidth; layoutParams.height = imageViewHeight; - ivBackground.setLayoutParams(layoutParams); + mivBackground.setLayoutParams(layoutParams); LogUtils.d(TAG, "【adjustImageViewSize】尺寸调整成功"); - LogUtils.d(TAG, "→ 当前模式:" + (isPreviewMode ? "预览模式" : "正式模式")); LogUtils.d(TAG, "→ 父控件尺寸:" + parentWidth + "x" + parentHeight); LogUtils.d(TAG, "→ ImageView调整后尺寸:" + imageViewWidth + "x" + imageViewHeight); - LogUtils.d(TAG, "→ 图片宽高比:" + imageAspectRatio); + LogUtils.d(TAG, "→ 图片宽高比:" + mnImageAspectRatio); LogUtils.d(TAG, "=== adjustImageViewSize(调整ImageView尺寸)完成(调整成功)==="); } @@ -530,9 +431,9 @@ public class BackgroundView extends RelativeLayout { */ private void setDefaultTransparentBackground() { LogUtils.d(TAG, "=== setDefaultTransparentBackground(设置通用默认透明背景)启动 ==="); - ivBackground.setBackground(new ColorDrawable(0x00000000)); // ImageView背景全透明 - ivBackground.setImageBitmap(null); // 清空ImageView的图片 - imageAspectRatio = 1.0f; // 重置宽高比(避免影响下次图片加载) + mivBackground.setBackground(new ColorDrawable(0x00000000)); // ImageView背景全透明 + mivBackground.setImageBitmap(null); // 清空ImageView的图片 + mnImageAspectRatio = 1.0f; // 重置宽高比(避免影响下次图片加载) adjustImageViewSize(); // 调整ImageView尺寸,确保居中且不占位异常 LogUtils.d(TAG, "【setDefaultTransparentBackground】已设置通用默认透明背景"); LogUtils.d(TAG, "=== setDefaultTransparentBackground(设置通用默认透明背景)完成 ===");