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