源码整理

This commit is contained in:
2025-12-24 20:27:30 +08:00
parent e1c3c8f072
commit 80da7677f8
4 changed files with 291 additions and 291 deletions

View File

@@ -86,7 +86,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogUtils.d(TAG, "生命周期 onCreate 开始初始化");
LogUtils.d(TAG, "生命周期 onCreate() 开始初始化");
setContentView(R.layout.activity_background_settings);
// 初始化核心组件
@@ -101,36 +101,16 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
// 初始化预览环境并刷新
initPreviewEnvironment();
LogUtils.d(TAG, "生命周期 onCreate 初始化完成");
LogUtils.d(TAG, "生命周期 onCreate() 初始化完成");
}
//
// @Override
// protected void onPostCreate(Bundle savedInstanceState) {
// super.onPostCreate(savedInstanceState);
// LogUtils.d(TAG, "生命周期 onPostCreate 执行双重刷新预览");
//
// // 初始化配置工具类并保存默认相框尺寸
// AppConfigUtils appConfigUtils = AppConfigUtils.getInstance(this);
// appConfigUtils.loadAppConfig();
// if (mBackgroundView.getWidth() > -1 && mBackgroundView.getHeight() > -1) {
// ToastUtils.show("mBackgroundView.getWidth() > 0 && mBackgroundView.getHeight() > 0");
// appConfigUtils.mAppConfigBean.setDefaultFrameWidth(mBackgroundView.getWidth());
// appConfigUtils.mAppConfigBean.setDefaultFrameHeight(mBackgroundView.getHeight());
// appConfigUtils.saveAppConfig();
// LogUtils.d(TAG, String.format("loadImage() 保存默认相框尺寸 | width=%d | height=%d", mBackgroundView.getWidth(), mBackgroundView.getHeight()));
// }
// LogUtils.d(TAG, String.format("mBackgroundView is %s", mBackgroundView));
// LogUtils.d(TAG, String.format("mBackgroundView.getWidth() %d, mBackgroundView.getHeight() %d", mBackgroundView.getWidth(), mBackgroundView.getHeight()));
// doubleRefreshPreview();
// }
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
LogUtils.d(TAG, "生命周期 onPostCreate 执行双重刷新预览");
// 监听视图布局完成事件
mBackgroundView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
LogUtils.d(TAG, "生命周期 onPostCreate() 执行双重刷新预览");
// 监听视图布局完成事件
mBackgroundView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// 移除监听,避免重复回调
@@ -154,13 +134,12 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
}
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LogUtils.d(TAG, String.format("回调触发 requestCode=%dresultCode=%d", requestCode, resultCode));
LogUtils.d(TAG, String.format("回调触发 onActivityResult() | requestCode=%d | resultCode=%d", requestCode, resultCode));
try {
if (resultCode != RESULT_OK) {
@@ -170,14 +149,14 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
}
handleActivityResult(requestCode, data);
} catch (Exception e) {
LogUtils.e(TAG, String.format("回调异常 requestCode=%d异常信息=%s", requestCode, e.getMessage()));
LogUtils.e(TAG, String.format("回调异常 onActivityResult() | requestCode=%d | 异常信息=%s", requestCode, e.getMessage()));
ToastUtils.show("操作失败");
}
}
@Override
public void finish() {
LogUtils.d(TAG, String.format("生命周期 finish 触发isCommitSettings=%bisPreviewBackgroundChanged=%b", isCommitSettings, isPreviewBackgroundChanged));
LogUtils.d(TAG, String.format("生命周期 finish() 触发 | isCommitSettings=%b | isPreviewBackgroundChanged=%b", isCommitSettings, isPreviewBackgroundChanged));
if (isCommitSettings) {
super.finish();
} else {
@@ -189,7 +168,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
LogUtils.d(TAG, String.format("权限回调 requestCode=%d权限数量=%d", requestCode, permissions.length));
LogUtils.d(TAG, String.format("权限回调 onRequestPermissionsResult() | requestCode=%d | 权限数量=%d", requestCode, permissions.length));
if (requestCode == REQUEST_CAMERA_PERMISSION) {
handleCameraPermissionResult(grantResults);
}
@@ -199,7 +178,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
private void initToolbar() {
mToolbar = findViewById(R.id.toolbar);
if (mToolbar == null) {
LogUtils.e(TAG, "初始化异常 Toolbar未找到");
LogUtils.e(TAG, "初始化异常 initToolbar() | Toolbar未找到");
return;
}
setSupportActionBar(mToolbar);
@@ -213,11 +192,11 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
finish();
}
});
LogUtils.d(TAG, "界面初始化 Toolbar 配置完成");
LogUtils.d(TAG, "界面初始化 initToolbar() | 配置完成");
}
private void initClickListeners() {
LogUtils.d(TAG, "界面初始化 开始绑定按钮点击事件");
LogUtils.d(TAG, "界面初始化 initClickListeners() | 开始绑定按钮点击事件");
// 绑定所有按钮点击事件
bindClickListener(R.id.activitybackgroundsettingsAButton1, onOriginNullClickListener);
bindClickListener(R.id.activitybackgroundsettingsAButton2, onReceivedPictureClickListener);
@@ -229,7 +208,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
bindClickListener(R.id.activitybackgroundsettingsAButton8, onPixelPickerClickListener);
bindClickListener(R.id.activitybackgroundsettingsAButton9, onColorPaletteClickListener);
bindClickListener(R.id.activitybackgroundsettingsAButton10, onCleanPixelClickListener);
LogUtils.d(TAG, "界面初始化 按钮点击事件绑定完成");
LogUtils.d(TAG, "界面初始化 initClickListeners() | 按钮点击事件绑定完成");
}
// 通用按钮绑定工具方法
@@ -237,8 +216,9 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
View view = findViewById(resId);
if (view != null) {
view.setOnClickListener(listener);
LogUtils.d(TAG, String.format("绑定成功 bindClickListener() | resId=%d", resId));
} else {
LogUtils.e(TAG, String.format("绑定异常 未找到视图:%d", resId));
LogUtils.e(TAG, String.format("绑定异常 bindClickListener() | 未找到视图:%d", resId));
}
}
@@ -246,10 +226,10 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
private View.OnClickListener onOriginNullClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "按钮点击 取消背景图片");
LogUtils.d(TAG, "按钮点击 onOriginNullClickListener() | 取消背景图片");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "操作异常 预览Bean为空");
LogUtils.e(TAG, "操作异常 onOriginNullClickListener() | 预览Bean为空");
return;
}
previewBean.setIsUseBackgroundFile(false);
@@ -262,7 +242,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
private View.OnClickListener onSelectPictureClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "按钮点击 选择图片");
LogUtils.d(TAG, "按钮点击 onSelectPictureClickListener() | 选择图片");
launchImageSelector();
}
};
@@ -270,9 +250,11 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
private View.OnClickListener onNetworkBackgroundDialog = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "按钮点击 onNetworkBackgroundDialog() | 打开网络背景对话框");
NetworkBackgroundDialog networkBackgroundDialog = new NetworkBackgroundDialog(BackgroundSettingsActivity.this, new NetworkBackgroundDialog.OnDialogClickListener() {
@Override
public void onConfirm(String szConfirmFilePath) {
LogUtils.d(TAG, String.format("网络背景确认 onConfirm() | 文件路径=%s", szConfirmFilePath));
// 拷贝文件到预览数据并启动裁剪
if (putUriFileToPreviewSource(new File(szConfirmFilePath))) {
startImageCrop(false);
@@ -281,6 +263,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
@Override
public void onCancel() {
LogUtils.d(TAG, "网络背景取消 onCancel()");
}
});
networkBackgroundDialog.show();
@@ -290,7 +273,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
private View.OnClickListener onCropPictureClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "按钮点击 固定比例裁剪");
LogUtils.d(TAG, "按钮点击 onCropPictureClickListener() | 固定比例裁剪");
startImageCrop(false);
}
};
@@ -298,7 +281,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
private View.OnClickListener onCropFreePictureClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "按钮点击 自由裁剪");
LogUtils.d(TAG, "按钮点击 onCropFreePictureClickListener() | 自由裁剪");
startImageCrop(true);
}
};
@@ -306,11 +289,11 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
private View.OnClickListener onTakePhotoClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "按钮点击 拍照");
LogUtils.d(TAG, "按钮点击 onTakePhotoClickListener() | 拍照");
// 动态申请相机权限
if (ContextCompat.checkSelfPermission(BackgroundSettingsActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
LogUtils.d(TAG, "拍照准备 相机权限未授予,发起申请");
LogUtils.d(TAG, "拍照准备 onTakePhotoClickListener() | 相机权限未授予,发起申请");
ActivityCompat.requestPermissions(
BackgroundSettingsActivity.this,
new String[]{Manifest.permission.CAMERA},
@@ -324,10 +307,10 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
private View.OnClickListener onReceivedPictureClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "按钮点击 恢复收到的图片");
LogUtils.d(TAG, "按钮点击 onReceivedPictureClickListener() | 恢复收到的图片");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "操作异常 预览Bean为空");
LogUtils.e(TAG, "操作异常 onReceivedPictureClickListener() | 预览Bean为空");
return;
}
previewBean.setIsUseBackgroundFile(true);
@@ -340,10 +323,10 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
private View.OnClickListener onPixelPickerClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "按钮点击 像素拾取");
LogUtils.d(TAG, "按钮点击 onPixelPickerClickListener() | 像素拾取");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "操作异常 预览Bean为空");
LogUtils.e(TAG, "操作异常 onPixelPickerClickListener() | 预览Bean为空");
ToastUtils.show("无有效图片可拾取像素");
return;
}
@@ -351,23 +334,23 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
File targetFile = new File(targetImagePath);
if (targetFile == null || !targetFile.exists() || targetFile.length() <= 0) {
ToastUtils.show("无有效图片可拾取像素");
LogUtils.e(TAG, String.format("像素拾取失败 文件无效:%s", targetImagePath));
LogUtils.e(TAG, String.format("像素拾取失败 onPixelPickerClickListener() | 文件无效:%s", targetImagePath));
return;
}
Intent intent = new Intent(getApplicationContext(), PixelPickerActivity.class);
intent.putExtra("imagePath", targetImagePath);
startActivityForResult(intent, REQUEST_PIXELPICKER);
LogUtils.d(TAG, String.format("像素拾取启动 路径:%s", targetImagePath));
LogUtils.d(TAG, String.format("像素拾取启动 onPixelPickerClickListener() | 路径:%s", targetImagePath));
}
};
private View.OnClickListener onCleanPixelClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "按钮点击 清空像素颜色");
LogUtils.d(TAG, "按钮点击 onCleanPixelClickListener() | 清空像素颜色");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "操作异常 预览Bean为空");
LogUtils.e(TAG, "操作异常 onCleanPixelClickListener() | 预览Bean为空");
return;
}
int oldColor = previewBean.getPixelColor();
@@ -376,21 +359,21 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
doubleRefreshPreview();
isPreviewBackgroundChanged = true;
ToastUtils.show("像素颜色已清空");
LogUtils.d(TAG, String.format("像素清空 旧颜色:#%08X", oldColor));
LogUtils.d(TAG, String.format("像素清空 onCleanPixelClickListener() | 旧颜色:#%08X", oldColor));
}
};
private View.OnClickListener onColorPaletteClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "按钮点击 调色板按钮");
LogUtils.d(TAG, "按钮点击 onColorPaletteClickListener() | 调色板按钮");
final BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "操作异常 预览Bean为空");
LogUtils.e(TAG, "操作异常 onColorPaletteClickListener() | 预览Bean为空");
return;
}
int initialColor = previewBean.getPixelColor();
LogUtils.d(TAG, String.format("调色板 初始颜色:#%08X", initialColor));
LogUtils.d(TAG, String.format("调色板 onColorPaletteClickListener() | 初始颜色:#%08X", initialColor));
ColorPaletteDialog dialog = new ColorPaletteDialog(BackgroundSettingsActivity.this, initialColor, new ColorPaletteDialog.OnColorSelectedListener() {
@Override
public void onColorSelected(int color) {
@@ -398,11 +381,11 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
mBgSourceUtils.saveSettings();
doubleRefreshPreview();
isPreviewBackgroundChanged = true;
LogUtils.d(TAG, String.format("颜色选择 选中颜色:#%08X", color));
LogUtils.d(TAG, String.format("颜色选择 onColorSelected() | 选中颜色:#%08X", color));
}
});
dialog.show();
LogUtils.d(TAG, "调色板 对话框已显示");
LogUtils.d(TAG, "调色板 onColorPaletteClickListener() | 对话框已显示");
}
};
@@ -413,9 +396,9 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
* @return 适配后的Uri失败返回null
*/
public Uri getFileProviderUri(File file) {
LogUtils.d(TAG, String.format("工具方法 生成FileProvider Uri文件路径:%s", (file != null ? file.getAbsolutePath() : "null")));
LogUtils.d(TAG, String.format("工具方法 getFileProviderUri() | 文件路径:%s", (file != null ? file.getAbsolutePath() : "null")));
if (file == null) {
LogUtils.e(TAG, "工具异常 文件为空");
LogUtils.e(TAG, "工具异常 getFileProviderUri() | 文件为空");
return null;
}
try {
@@ -426,7 +409,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
return Uri.fromFile(file);
}
} catch (Exception e) {
LogUtils.e(TAG, String.format("工具异常 生成Uri失败%s", e.getMessage()));
LogUtils.e(TAG, String.format("工具异常 getFileProviderUri() | 生成Uri失败%s", e.getMessage()));
return null;
}
}
@@ -438,7 +421,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
*/
private boolean isBitmapValid(Bitmap bitmap) {
boolean isValid = bitmap != null && !bitmap.isRecycled();
LogUtils.d(TAG, String.format("工具方法 Bitmap有效性校验%b", isValid));
LogUtils.d(TAG, String.format("工具方法 isBitmapValid() | Bitmap有效性校验%b", isValid));
return isValid;
}
@@ -446,7 +429,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
* 双重刷新预览,确保背景加载最新数据
*/
private void doubleRefreshPreview() {
LogUtils.d(TAG, "工具方法 开始双重刷新预览");
LogUtils.d(TAG, "工具方法 doubleRefreshPreview() | 开始双重刷新预览");
if (mBgSourceUtils == null || mBackgroundView == null || isFinishing()) {
LogUtils.w(TAG, "双重刷新 跳过对象为空或Activity已结束");
return;
@@ -457,9 +440,9 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
mBgSourceUtils.loadSettings();
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
mBackgroundView.loadByBackgroundBean(previewBean, true);
LogUtils.d(TAG, "双重刷新 第一重完成");
LogUtils.d(TAG, "双重刷新 doubleRefreshPreview() | 第一重完成");
} catch (Exception e) {
LogUtils.e(TAG, String.format("双重刷新 第一重异常:%s", e.getMessage()));
LogUtils.e(TAG, String.format("双重刷新 doubleRefreshPreview() | 第一重异常:%s", e.getMessage()));
return;
}
@@ -472,9 +455,9 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
mBgSourceUtils.loadSettings();
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
mBackgroundView.loadByBackgroundBean(previewBean, true);
LogUtils.d(TAG, "双重刷新 第二重完成");
LogUtils.d(TAG, "双重刷新 doubleRefreshPreview() | 第二重完成");
} catch (Exception e) {
LogUtils.e(TAG, String.format("双重刷新 第二重异常:%s", e.getMessage()));
LogUtils.e(TAG, String.format("双重刷新 doubleRefreshPreview() | 第二重异常:%s", e.getMessage()));
}
}
}
@@ -489,13 +472,13 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
// 初始化视图
mBackgroundView = findViewById(R.id.background_view);
if (mBackgroundView == null) {
LogUtils.e(TAG, "初始化异常 BackgroundView未找到");
LogUtils.e(TAG, "初始化异常 initCoreComponents() | BackgroundView未找到");
}
// 初始化工具类
mBgSourceUtils = BackgroundSourceUtils.getInstance(this);
mBgSourceUtils.loadSettings();
mBitmapCache = BitmapCacheUtils.getInstance();
LogUtils.d(TAG, "初始化 视图与工具类加载完成");
LogUtils.d(TAG, "初始化 initCoreComponents() | 视图与工具类加载完成");
}
/**
@@ -504,9 +487,10 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
private void handleIntentOrPreview() {
if (handleShareIntent()) {
ToastUtils.show("已接收分享图片");
LogUtils.d(TAG, "意图处理 handleIntentOrPreview() | 处理分享意图成功");
} else {
mBgSourceUtils.setCurrentSourceToPreview();
LogUtils.d(TAG, "预览初始化 加载当前背景配置");
LogUtils.d(TAG, "意图处理 handleIntentOrPreview() | 加载当前背景配置");
}
}
@@ -517,6 +501,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
mBgSourceUtils.createAndUpdatePreviewEnvironmentForCropping(previewBean);
doubleRefreshPreview();
LogUtils.d(TAG, "预览环境 initPreviewEnvironment() | 初始化完成");
}
/**
@@ -528,7 +513,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
if (intent != null) {
String action = intent.getAction();
String type = intent.getType();
LogUtils.d(TAG, String.format("分享处理 action%stype%s", action, type));
LogUtils.d(TAG, String.format("分享处理 handleShareIntent() | action%stype%s", action, type));
if (Intent.ACTION_SEND.equals(action) && type != null && isImageType(type)) {
showSharePreviewDialog();
return true;
@@ -541,17 +526,18 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
* 显示分享图片预览对话框
*/
private void showSharePreviewDialog() {
LogUtils.d(TAG, "showSharePreviewDialog()");
LogUtils.d(TAG, "工具方法 showSharePreviewDialog()");
BackgroundPicturePreviewDialog dlg = new BackgroundPicturePreviewDialog(this, new BackgroundPicturePreviewDialog.IOnRecivedPictureListener() {
@Override
public void onAcceptRecivedPicture(Uri uriRecivedPicture) {
LogUtils.d(TAG, String.format("分享确认 onAcceptRecivedPicture() | Uri%s", uriRecivedPicture.toString()));
if (putUriFileToPreviewSource(uriRecivedPicture)) {
startImageCrop(false);
}
}
});
dlg.show();
LogUtils.d(TAG, "分享处理 显示图片预览对话框");
LogUtils.d(TAG, "分享处理 showSharePreviewDialog() | 显示图片预览对话框");
}
/**
@@ -564,7 +550,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
return false;
}
String lowerMimeType = mimeType.toLowerCase();
LogUtils.d("isImageType", String.format("mimeType: %s, lowerMimeType: %s", mimeType, lowerMimeType));
LogUtils.d(TAG, String.format("工具方法 isImageType() | mimeType: %s, lowerMimeType: %s", mimeType, lowerMimeType));
return lowerMimeType.startsWith("image/");
}
@@ -572,7 +558,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
* 启动图片选择器
*/
private void launchImageSelector() {
LogUtils.d(TAG, "业务逻辑 启动图片选择器");
LogUtils.d(TAG, "业务逻辑 launchImageSelector() | 启动图片选择器");
Intent[] intents = createImageSelectorIntents();
Intent validIntent = findValidIntent(intents);
@@ -610,6 +596,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
openDocIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
intents[2] = openDocIntent;
}
LogUtils.d(TAG, "业务逻辑 createImageSelectorIntents() | 意图数组创建完成");
return intents;
}
@@ -621,9 +608,11 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
private Intent findValidIntent(Intent[] intents) {
for (Intent intent : intents) {
if (intent != null && intent.resolveActivity(getPackageManager()) != null) {
LogUtils.d(TAG, "业务逻辑 findValidIntent() | 找到有效意图");
return intent;
}
}
LogUtils.d(TAG, "业务逻辑 findValidIntent() | 无有效意图");
return null;
}
@@ -635,14 +624,14 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
Intent chooser = Intent.createChooser(validIntent, "选择图片");
chooser.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
startActivityForResult(chooser, REQUEST_SELECT_PICTURE);
LogUtils.d(TAG, "选图意图 启动图片选择");
LogUtils.d(TAG, "选图意图 launchImageChooser() | 启动图片选择");
}
/**
* 显示无相册应用提示对话框
*/
private void showNoGalleryDialog() {
LogUtils.d(TAG, "选图意图 无相册应用");
LogUtils.d(TAG, "选图意图 showNoGalleryDialog() | 无相册应用");
runOnUiThread(new Runnable() {
@Override
public void run() {
@@ -670,8 +659,10 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
marketIntent.setData(Uri.parse("market://details?id=com.android.gallery3d"));
if (marketIntent.resolveActivity(getPackageManager()) != null) {
startActivity(marketIntent);
LogUtils.d(TAG, "应用商店 launchGalleryMarket() | 启动成功");
} else {
ToastUtils.show("无法打开应用商店");
LogUtils.e(TAG, "应用商店 launchGalleryMarket() | 启动失败");
}
}
@@ -680,7 +671,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
*/
private void handleOperationCancelOrFail() {
mBgSourceUtils.setCurrentSourceToPreview();
LogUtils.d(TAG, "业务逻辑 操作取消或失败,恢复预览");
LogUtils.d(TAG, "业务逻辑 handleOperationCancelOrFail() | 操作取消或失败,恢复预览");
ToastUtils.show("操作取消或失败");
doubleRefreshPreview();
}
@@ -689,10 +680,10 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
* 处理拍照逻辑(权限通过后执行)
*/
void handleTakePhoto() {
LogUtils.d(TAG, "业务逻辑 开始处理拍照");
LogUtils.d(TAG, "业务逻辑 handleTakePhoto() | 开始处理拍照");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "拍照失败 预览Bean为空");
LogUtils.e(TAG, "拍照失败 handleTakePhoto() | 预览Bean为空");
ToastUtils.show("拍照文件创建失败");
return;
}
@@ -700,7 +691,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
File takePhotoFile = new File(previewBean.getBackgroundFilePath());
if (!takePhotoFile.exists()) {
ToastUtils.show("拍照文件创建失败");
LogUtils.e(TAG, String.format("拍照失败 文件不存在:%s", takePhotoFile.getAbsolutePath()));
LogUtils.e(TAG, String.format("拍照失败 handleTakePhoto() | 文件不存在:%s", takePhotoFile.getAbsolutePath()));
return;
}
@@ -712,11 +703,11 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
LogUtils.d(TAG, String.format("拍照启动 Uri%s", photoUri.toString()));
LogUtils.d(TAG, String.format("拍照启动 handleTakePhoto() | Uri%s", photoUri.toString()));
} catch (Exception e) {
String errMsg = "拍照启动异常:" + e.getMessage();
ToastUtils.show(errMsg.substring(0, 20));
LogUtils.e(TAG, String.format("拍照失败 %s", e.getMessage()));
LogUtils.e(TAG, String.format("拍照失败 handleTakePhoto() | %s", e.getMessage()));
}
}
@@ -726,6 +717,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
* @param data 回调数据
*/
private void handleActivityResult(int requestCode, Intent data) {
LogUtils.d(TAG, String.format("业务逻辑 handleActivityResult() | 处理请求码:%d", requestCode));
switch (requestCode) {
case REQUEST_SELECT_PICTURE:
handleSelectPictureResult(data);
@@ -740,7 +732,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
handlePixelPickerResult();
break;
default:
LogUtils.d(TAG, String.format("回调忽略 未知requestCode%d", requestCode));
LogUtils.d(TAG, String.format("回调忽略 handleActivityResult() | 未知requestCode%d", requestCode));
break;
}
}
@@ -750,10 +742,10 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
* @param data 回调数据
*/
private void handleTakePhotoResult(Intent data) {
LogUtils.d(TAG, "业务逻辑 处理拍照结果");
LogUtils.d(TAG, "业务逻辑 handleTakePhotoResult() | 处理拍照结果");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "拍照结果处理 预览Bean为空");
LogUtils.e(TAG, "拍照结果处理 handleTakePhotoResult() | 预览Bean为空");
return;
}
@@ -763,7 +755,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
doubleRefreshPreview();
startImageCrop(false);
LogUtils.d(TAG, "拍照完成 已启动裁剪");
LogUtils.d(TAG, "拍照完成 handleTakePhotoResult() | 已启动裁剪");
}
/**
@@ -771,30 +763,30 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
* @param data 回调数据
*/
private void handleSelectPictureResult(Intent data) {
LogUtils.d(TAG, "业务逻辑 处理选图结果");
LogUtils.d(TAG, "业务逻辑 handleSelectPictureResult() | 处理选图结果");
Uri selectedImage = data.getData();
if (selectedImage == null) {
ToastUtils.show("图片Uri为空");
LogUtils.e(TAG, "选图结果 Uri为空");
LogUtils.e(TAG, "选图结果 handleSelectPictureResult() | Uri为空");
return;
}
LogUtils.d(TAG, String.format("选图回调 系统返回Uri : %s", selectedImage.toString()));
LogUtils.d(TAG, String.format("选图回调 handleSelectPictureResult() | 系统返回Uri : %s", selectedImage.toString()));
// 申请持久化权限API33+
if (Build.VERSION.SDK_INT >= SDK_VERSION_TIRAMISU) {
getContentResolver().takePersistableUriPermission(
selectedImage,
Intent.FLAG_GRANT_READ_URI_PERMISSION);
LogUtils.d(TAG, "选图权限 已添加持久化权限");
LogUtils.d(TAG, "选图权限 handleSelectPictureResult() | 已添加持久化权限");
}
// 同步文件并启动裁剪
if (putUriFileToPreviewSource(selectedImage)) {
LogUtils.d(TAG, "选图同步 路径绑定完成");
LogUtils.d(TAG, "选图同步 handleSelectPictureResult() | 路径绑定完成");
startImageCrop(false);
} else {
ToastUtils.show("图片同步失败");
LogUtils.e(TAG, "选图同步 文件复制失败");
LogUtils.e(TAG, "选图同步 handleSelectPictureResult() | 文件复制失败");
}
}
@@ -806,7 +798,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
private boolean putUriFileToPreviewSource(Uri srcUriFile) {
String filePath = UriUtils.getFilePathFromUri(this, srcUriFile);
if (TextUtils.isEmpty(filePath)) {
LogUtils.e(TAG, "选图同步 Uri解析路径为空");
LogUtils.e(TAG, "选图同步 putUriFileToPreviewSource() | Uri解析路径为空");
return false;
}
File srcFile = new File(filePath);
@@ -819,16 +811,16 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
* @return 同步成功返回true否则false
*/
private boolean putUriFileToPreviewSource(File srcFile) {
LogUtils.d(TAG, String.format("选图同步 源文件:%s", srcFile.getAbsolutePath()));
LogUtils.d(TAG, String.format("选图同步 putUriFileToPreviewSource() | 源文件:%s", srcFile.getAbsolutePath()));
mBgSourceUtils.loadSettings();
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
File dstFile = new File(previewBean.getBackgroundFilePath());
LogUtils.d(TAG, String.format("选图同步 目标文件:%s", dstFile.getAbsolutePath()));
LogUtils.d(TAG, String.format("选图同步 putUriFileToPreviewSource() | 目标文件:%s", dstFile.getAbsolutePath()));
if (FileUtils.copyFile(srcFile, dstFile)) {
LogUtils.d(TAG, "选图同步 文件拷贝成功");
LogUtils.d(TAG, "选图同步 putUriFileToPreviewSource() | 文件拷贝成功");
return true;
}
LogUtils.d(TAG, "选图同步 文件无法拷贝");
LogUtils.d(TAG, "选图同步 putUriFileToPreviewSource() | 文件无法拷贝");
return false;
}
@@ -837,10 +829,10 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
* @param data 回调数据
*/
private void handleCropImageResult(Intent data) {
LogUtils.d(TAG, "业务逻辑 处理裁剪结果");
LogUtils.d(TAG, "业务逻辑 handleCropImageResult() | 处理裁剪结果");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "裁剪结果处理 预览Bean为空");
LogUtils.e(TAG, "裁剪结果处理 handleCropImageResult() | 预览Bean为空");
handleOperationCancelOrFail();
return;
}
@@ -865,11 +857,11 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
*/
private void handleCropSuccess(BackgroundBean previewBean, long fileSize) {
isPreviewBackgroundChanged = true;
LogUtils.d(TAG, String.format("裁剪结果 裁剪成功,文件大小:%d", fileSize));
LogUtils.d(TAG, String.format("裁剪结果 handleCropSuccess() | 裁剪成功,文件大小:%d", fileSize));
previewBean.setIsUseBackgroundFile(true);
previewBean.setIsUseBackgroundScaledCompressFile(true);
mBgSourceUtils.saveSettings();
doubleRefreshPreview();
doubleRefreshPreview();
}
/**
@@ -880,14 +872,14 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
*/
private void handleCropFailure(boolean isFileExist, boolean isFileReadable, long fileSize) {
handleOperationCancelOrFail();
LogUtils.e(TAG, String.format("裁剪结果 裁剪失败,文件状态:存在=%b可读=%b大小=%d", isFileExist, isFileReadable, fileSize));
LogUtils.e(TAG, String.format("裁剪结果 handleCropFailure() | 裁剪失败,文件状态:存在=%b可读=%b大小=%d", isFileExist, isFileReadable, fileSize));
}
/**
* 处理像素拾取结果
*/
private void handlePixelPickerResult() {
LogUtils.d(TAG, "业务逻辑 处理像素拾取结果");
LogUtils.d(TAG, "业务逻辑 handlePixelPickerResult() | 处理像素拾取结果");
doubleRefreshPreview();
isPreviewBackgroundChanged = true;
}
@@ -898,10 +890,10 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
*/
private void handleCameraPermissionResult(int[] grantResults) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
LogUtils.d(TAG, "权限申请 相机权限授予成功");
LogUtils.d(TAG, "权限申请 handleCameraPermissionResult() | 相机权限授予成功");
handleTakePhoto();
} else {
LogUtils.d(TAG, "权限申请 相机权限授予失败");
LogUtils.d(TAG, "权限申请 handleCameraPermissionResult() | 相机权限授予失败");
ToastUtils.show("相机权限被拒绝,无法拍照");
// 引导用户到设置页面开启权限(用户选择不再询问时)
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
@@ -919,7 +911,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
intent.setData(uri);
startActivity(intent);
ToastUtils.show("请在设置中开启相机权限");
LogUtils.d(TAG, "权限引导 启动应用设置页面");
LogUtils.d(TAG, "权限引导 launchAppSettings() | 启动应用设置页面");
}
/**
@@ -954,7 +946,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
private void startImageCrop(boolean isFreeCrop) {
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "裁剪启动 预览Bean为空");
LogUtils.e(TAG, "裁剪启动 startImageCrop() | 预览Bean为空");
ToastUtils.show("裁剪失败:无有效图片");
return;
}
@@ -966,7 +958,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
height,
isFreeCrop,
REQUEST_CROP_IMAGE);
LogUtils.d(TAG, String.format("裁剪启动 是否自由裁剪:%b目标尺寸%dx%d", isFreeCrop, width, height));
LogUtils.d(TAG, String.format("裁剪启动 startImageCrop() | 是否自由裁剪:%b目标尺寸%dx%d", isFreeCrop, width, height));
}
}

View File

@@ -10,7 +10,7 @@ import java.io.IOException;
import java.io.Serializable;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Date 2024/04/29 17:24:53
* @Describe 应用运行参数类
* 适配 API30支持 Serializable 持久化、Parcelable Intent 传递、JSON 序列化/反序列化
@@ -23,13 +23,13 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
// 日志标签(全局统一,替换 Log 为 LogUtils
transient public static final String TAG = "AppConfigBean";
// 字段校验常量(统一阈值,避免硬编码)
private static final int MIN_INTERVAL = 500; // 最小检测间隔ms
private static final int MIN_REMIND_INTERVAL = 1000; // 最小提醒间隔ms
private static final int BATTERY_MIN = 0; // 电量最小值
private static final int BATTERY_MAX = 100; // 电量最大值
private static final int INVALID_BATTERY = -1; // 无效电量标识
private static final int MIN_INTERVAL = 500; // 最小检测间隔ms
private static final int MIN_REMIND_INTERVAL = 1000;// 最小提醒间隔ms
private static final int BATTERY_MIN = 0; // 电量最小值
private static final int BATTERY_MAX = 100; // 电量最大值
private static final int INVALID_BATTERY = -1; // 无效电量标识
private static final int DEFAULT_FRAME_WIDTH = 500; // 默认相框宽度px
private static final int DEFAULT_FRAME_HEIGHT = 500; // 默认相框高度px
private static final int DEFAULT_FRAME_HEIGHT = 500;// 默认相框高度px
// ====================== 成员变量(按功能分类:提醒配置→电量状态→检测配置→相框配置) ======================
// 耗电提醒配置
@@ -46,8 +46,8 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
// 电量检测配置
int batteryDetectInterval = 2000; // 电量检测间隔ms适配 RemindThread
// 相框配置
int defaultFrameWidth = DEFAULT_FRAME_WIDTH; // 默认相框宽度px
int defaultFrameHeight = DEFAULT_FRAME_HEIGHT; // 默认相框高度px
int defaultFrameWidth = DEFAULT_FRAME_WIDTH; // 默认相框宽度px
int defaultFrameHeight = DEFAULT_FRAME_HEIGHT;// 默认相框高度px
// ====================== 构造方法(初始化默认配置,强化默认值校验) ======================
public AppConfigBean() {
@@ -60,7 +60,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
setCurrentBatteryValue(INVALID_BATTERY); // 初始化无效电量标识
setDefaultFrameWidth(DEFAULT_FRAME_WIDTH); // 初始化默认相框宽度
setDefaultFrameHeight(DEFAULT_FRAME_HEIGHT); // 初始化默认相框高度
LogUtils.d(TAG, "AppConfigBean: 初始化默认配置完成");
LogUtils.d(TAG, "AppConfigBean() 初始化默认配置完成");
}
// ====================== 核心业务方法Setter/Getter按字段功能分类补充调试日志 ======================
@@ -72,7 +72,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
public void setCurrentBatteryValue(int currentBatteryValue) {
this.currentBatteryValue = (currentBatteryValue >= BATTERY_MIN && currentBatteryValue <= BATTERY_MAX)
? currentBatteryValue : INVALID_BATTERY;
LogUtils.d(TAG, String.format("setCurrentBatteryValue: 当前电量设置为 %d输入值%d",
LogUtils.d(TAG, String.format("setCurrentBatteryValue() 执行 | 最终电量=%d | 输入值=%d",
this.currentBatteryValue, currentBatteryValue));
}
@@ -91,7 +91,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
*/
public void setReminderIntervalTime(int reminderIntervalTime) {
this.reminderIntervalTime = Math.max(reminderIntervalTime, MIN_REMIND_INTERVAL);
LogUtils.d(TAG, String.format("setReminderIntervalTime: 提醒间隔设置为 %dms输入值%d",
LogUtils.d(TAG, String.format("setReminderIntervalTime() 执行 | 最终间隔=%dms | 输入值=%dms",
this.reminderIntervalTime, reminderIntervalTime));
}
@@ -106,7 +106,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
*/
public void setIsCharging(boolean isCharging) {
this.isCharging = isCharging;
LogUtils.d(TAG, String.format("setIsCharging: 充电状态设置为 %b", isCharging));
LogUtils.d(TAG, String.format("setIsCharging() 执行 | 充电状态=%b", isCharging));
}
public boolean isCharging() {
@@ -116,7 +116,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
// --------------- 耗电提醒配置相关 ---------------
public void setEnableUsageReminder(boolean isEnableUsageReminder) {
this.isEnableUsageReminder = isEnableUsageReminder;
LogUtils.d(TAG, String.format("setEnableUsageReminder: 耗电提醒开关设置为 %b", isEnableUsageReminder));
LogUtils.d(TAG, String.format("setEnableUsageReminder() 执行 | 耗电提醒开关=%b", isEnableUsageReminder));
}
public boolean isEnableUsageReminder() {
@@ -129,7 +129,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
*/
public void setUsageReminderValue(int usageReminderValue) {
this.usageReminderValue = Math.min(Math.max(usageReminderValue, BATTERY_MIN), BATTERY_MAX);
LogUtils.d(TAG, String.format("setUsageReminderValue: 耗电提醒阈值设置为 %d输入值%d",
LogUtils.d(TAG, String.format("setUsageReminderValue() 执行 | 最终阈值=%d | 输入值=%d",
this.usageReminderValue, usageReminderValue));
}
@@ -140,7 +140,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
// --------------- 充电提醒配置相关 ---------------
public void setEnableChargeReminder(boolean isEnableChargeReminder) {
this.isEnableChargeReminder = isEnableChargeReminder;
LogUtils.d(TAG, String.format("setEnableChargeReminder: 充电提醒开关设置为 %b", isEnableChargeReminder));
LogUtils.d(TAG, String.format("setEnableChargeReminder() 执行 | 充电提醒开关=%b", isEnableChargeReminder));
}
public boolean isEnableChargeReminder() {
@@ -153,7 +153,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
*/
public void setChargeReminderValue(int chargeReminderValue) {
this.chargeReminderValue = Math.min(Math.max(chargeReminderValue, BATTERY_MIN), BATTERY_MAX);
LogUtils.d(TAG, String.format("setChargeReminderValue: 充电提醒阈值设置为 %d输入值%d",
LogUtils.d(TAG, String.format("setChargeReminderValue() 执行 | 最终阈值=%d | 输入值=%d",
this.chargeReminderValue, chargeReminderValue));
}
@@ -168,7 +168,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
*/
public void setBatteryDetectInterval(int batteryDetectInterval) {
this.batteryDetectInterval = Math.max(batteryDetectInterval, MIN_INTERVAL);
LogUtils.d(TAG, String.format("setBatteryDetectInterval: 检测间隔设置为 %dms输入值%d",
LogUtils.d(TAG, String.format("setBatteryDetectInterval() 执行 | 最终间隔=%dms | 输入值=%dms",
this.batteryDetectInterval, batteryDetectInterval));
}
@@ -179,11 +179,11 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
// --------------- 相框配置相关 ---------------
/**
* 设置默认相框宽度
* @param defaultFrameWidth 相框宽度px,范围 MIN_FRAME_SIZE - MAX_FRAME_SIZE
* @param defaultFrameWidth 相框宽度px
*/
public void setDefaultFrameWidth(int defaultFrameWidth) {
this.defaultFrameWidth = defaultFrameWidth;
LogUtils.d(TAG, String.format("setDefaultFrameWidth: 默认相框宽度设置为 %dpx输入值%d",
LogUtils.d(TAG, String.format("setDefaultFrameWidth() 执行 | 最终宽度=%dpx | 输入值=%dpx",
this.defaultFrameWidth, defaultFrameWidth));
}
@@ -193,11 +193,11 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
/**
* 设置默认相框高度
* @param defaultFrameHeight 相框高度px,范围 MIN_FRAME_SIZE - MAX_FRAME_SIZE
* @param defaultFrameHeight 相框高度px
*/
public void setDefaultFrameHeight(int defaultFrameHeight) {
this.defaultFrameHeight = defaultFrameHeight;
LogUtils.d(TAG, String.format("setDefaultFrameHeight: 默认相框高度设置为 %dpx输入值%d",
LogUtils.d(TAG, String.format("setDefaultFrameHeight() 执行 | 最终高度=%dpx | 输入值=%dpx",
this.defaultFrameHeight, defaultFrameHeight));
}
@@ -205,7 +205,7 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
return defaultFrameHeight;
}
// ====================== JSON 序列化/反序列化兼容旧配置,补充调试日志 ======================
// ====================== 父类重写方法(JSON 序列化/反序列化兼容旧配置) ======================
@Override
public String getName() {
return AppConfigBean.class.getName();
@@ -214,23 +214,22 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
AppConfigBean bean = this;
// 原有字段序列化
jsonWriter.name("isEnableUsageReminder").value(bean.isEnableUsageReminder());
jsonWriter.name("usageReminderValue").value(bean.getUsageReminderValue());
jsonWriter.name("isEnableChargeReminder").value(bean.isEnableChargeReminder());
jsonWriter.name("chargeReminderValue").value(bean.getChargeReminderValue());
jsonWriter.name("reminderIntervalTime").value(bean.getReminderIntervalTime());
jsonWriter.name("isCharging").value(bean.isCharging());
jsonWriter.name("isEnableUsageReminder").value(isEnableUsageReminder());
jsonWriter.name("usageReminderValue").value(getUsageReminderValue());
jsonWriter.name("isEnableChargeReminder").value(isEnableChargeReminder());
jsonWriter.name("chargeReminderValue").value(getChargeReminderValue());
jsonWriter.name("reminderIntervalTime").value(getReminderIntervalTime());
jsonWriter.name("isCharging").value(isCharging());
// 兼容旧字段 currentValue同步新字段 currentBatteryValue
jsonWriter.name("currentBatteryValue").value(bean.getCurrentBatteryValue());
jsonWriter.name("currentValue").value(bean.getCurrentBatteryValue());
jsonWriter.name("currentBatteryValue").value(getCurrentBatteryValue());
jsonWriter.name("currentValue").value(getCurrentBatteryValue());
// 新增字段序列化(检测配置)
jsonWriter.name("batteryDetectInterval").value(bean.getBatteryDetectInterval());
jsonWriter.name("batteryDetectInterval").value(getBatteryDetectInterval());
// 新增字段序列化(相框配置)
jsonWriter.name("defaultFrameWidth").value(bean.getDefaultFrameWidth());
jsonWriter.name("defaultFrameHeight").value(bean.getDefaultFrameHeight());
LogUtils.d(TAG, "writeThisToJsonWriter: JSON 序列化完成");
jsonWriter.name("defaultFrameWidth").value(getDefaultFrameWidth());
jsonWriter.name("defaultFrameHeight").value(getDefaultFrameHeight());
LogUtils.d(TAG, "writeThisToJsonWriter() JSON序列化完成");
}
@Override
@@ -242,43 +241,50 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
// 兼容拼写错误字段isEnableUsegeReminder → isEnableUsageReminder
if (name.equals("isEnableUsageReminder") || name.equals("isEnableUsegeReminder")) {
bean.setEnableUsageReminder(jsonReader.nextBoolean());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%b", name, bean.isEnableUsageReminder()));
} else if (name.equals("usageReminderValue") || name.equals("usegeReminderValue")) {
bean.setUsageReminderValue(jsonReader.nextInt());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%d", name, bean.getUsageReminderValue()));
} else if (name.equals("isEnableChargeReminder")) {
bean.setEnableChargeReminder(jsonReader.nextBoolean());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%b", name, bean.isEnableChargeReminder()));
} else if (name.equals("chargeReminderValue")) {
bean.setChargeReminderValue(jsonReader.nextInt());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%d", name, bean.getChargeReminderValue()));
} else if (name.equals("reminderIntervalTime")) {
bean.setReminderIntervalTime(jsonReader.nextInt());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%d", name, bean.getReminderIntervalTime()));
} else if (name.equals("isCharging")) {
bean.setIsCharging(jsonReader.nextBoolean());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%b", name, bean.isCharging()));
} else if (name.equals("currentValue")) {
// 优先读取旧字段,兼容历史配置
bean.setCurrentBatteryValue(jsonReader.nextInt());
LogUtils.d(TAG, "readBeanFromJsonReader: 读取旧字段 currentValue 完成");
LogUtils.d(TAG, "readBeanFromJsonReader() 读取旧字段 | currentValue=" + bean.getCurrentBatteryValue());
} else if (name.equals("currentBatteryValue")) {
// 新字段覆盖旧字段,保证数据最新
bean.setCurrentBatteryValue(jsonReader.nextInt());
LogUtils.d(TAG, "readBeanFromJsonReader: 读取新字段 currentBatteryValue 完成");
LogUtils.d(TAG, "readBeanFromJsonReader() 读取新字段 | currentBatteryValue=" + bean.getCurrentBatteryValue());
} else if (name.equals("batteryDetectInterval")) {
bean.setBatteryDetectInterval(jsonReader.nextInt());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%d", name, bean.getBatteryDetectInterval()));
} else if (name.equals("defaultFrameWidth")) {
bean.setDefaultFrameWidth(jsonReader.nextInt());
LogUtils.d(TAG, "readBeanFromJsonReader: 读取字段 defaultFrameWidth 完成");
LogUtils.d(TAG, "readBeanFromJsonReader() 读取字段 | defaultFrameWidth=" + bean.getDefaultFrameWidth());
} else if (name.equals("defaultFrameHeight")) {
bean.setDefaultFrameHeight(jsonReader.nextInt());
LogUtils.d(TAG, "readBeanFromJsonReader: 读取字段 defaultFrameHeight 完成");
LogUtils.d(TAG, "readBeanFromJsonReader() 读取字段 | defaultFrameHeight=" + bean.getDefaultFrameHeight());
} else {
jsonReader.skipValue();
LogUtils.w(TAG, String.format("readBeanFromJsonReader: 跳过未知字段 %s", name));
LogUtils.w(TAG, "readBeanFromJsonReader() 跳过未知字段 | " + name);
}
}
jsonReader.endObject();
LogUtils.d(TAG, "readBeanFromJsonReader: JSON 反序列化完成");
LogUtils.d(TAG, "readBeanFromJsonReader() JSON反序列化完成");
return bean;
}
// ====================== Parcelable 接口实现API30 Intent 传递必备,补充调试日志 ======================
// ====================== Parcelable 接口实现API30 Intent 传递必备) ======================
@Override
public int describeContents() {
return 0; // 无特殊内容描述固定返回0
@@ -295,10 +301,9 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
dest.writeByte((byte) (isCharging ? 1 : 0));
dest.writeInt(currentBatteryValue);
dest.writeInt(batteryDetectInterval);
// 新增相框配置字段写入
dest.writeInt(defaultFrameWidth);
dest.writeInt(defaultFrameHeight);
LogUtils.d(TAG, "writeToParcel: Parcel 序列化完成");
LogUtils.d(TAG, "writeToParcel() Parcel序列化完成");
}
// 反序列化 Creator必须 public static final 修饰Java7 适配)
@@ -315,10 +320,9 @@ public class AppConfigBean extends BaseBean implements Serializable, Parcelable
bean.isCharging = source.readByte() != 0;
bean.currentBatteryValue = source.readInt();
bean.batteryDetectInterval = source.readInt();
// 新增相框配置字段读取
bean.defaultFrameWidth = source.readInt();
bean.defaultFrameHeight = source.readInt();
LogUtils.d(TAG, "createFromParcel: Parcel 反序列化完成");
LogUtils.d(TAG, "createFromParcel() Parcel反序列化完成");
return bean;
}

View File

@@ -34,6 +34,7 @@ public class ImageUtils {
private static final int MIN_COMPRESS_QUALITY = 0;
private static final int MAX_COMPRESS_QUALITY = 100;
private static final int[] COLOR_ACCENT_ATTR = new int[]{R.attr.colorAccent}; // colorAccent属性数组
private static final int DEFAULT_COLOR = Color.parseColor("#FFFFFFFF"); // 默认返回颜色
// ================================== 主题颜色获取方法 =================================
/**
@@ -42,23 +43,24 @@ public class ImageUtils {
* @return 解析到的 colorAccent 颜色值,解析失败返回默认白色#FFFFFFFF
*/
public static int getColorAccent(Context context) {
// 方法调用日志
LogUtils.d(TAG, "【getColorAccent】方法调用");
// 参数校验
if (context == null) {
LogUtils.e(TAG, "【getColorAccent】参数异常Context为空");
return Color.parseColor("#FFFFFFFF");
return DEFAULT_COLOR;
}
TypedArray typedArray = null;
try {
// 从主题解析属性
typedArray = context.obtainStyledAttributes(COLOR_ACCENT_ATTR);
int colorAccent = typedArray.getColor(0, Color.parseColor("#FFFFFFFF"));
int colorAccent = typedArray.getColor(0, DEFAULT_COLOR);
LogUtils.d(TAG, String.format("【getColorAccent】解析成功 | colorAccent=0x%08X", colorAccent));
return colorAccent;
} catch (Exception e) {
LogUtils.e(TAG, "【getColorAccent】解析失败返回默认颜色", e);
return Color.parseColor("#FFFFFFFF");
return DEFAULT_COLOR;
} finally {
// 回收资源
if (typedArray != null) {
@@ -67,87 +69,94 @@ public class ImageUtils {
}
}
}
public static Bitmap drawBitmapOnSolidBackground(final int bgColor, int originalFrameW, int originalFrameH, Bitmap fgBitmap) {
// 方法调用及入参日志
LogUtils.d(TAG, String.format("【drawBitmapOnSolidBackground】方法调用 | 背景色=0x%08X | 目标尺寸=%dx%d | 前景位图=%s",
// ================================== 位图合成方法 =================================
/**
* 在纯色背景上绘制前景位图实现FIT_CENTER居中效果
* @param bgColor 背景颜色
* @param originalFrameW 目标画布宽度
* @param originalFrameH 目标画布高度
* @param fgBitmap 前景位图
* @return 合成后的位图失败返回null
*/
public static Bitmap drawBitmapOnSolidBackground(final int bgColor, int originalFrameW, int originalFrameH, Bitmap fgBitmap) {
// 方法调用及入参日志
LogUtils.d(TAG, String.format("【drawBitmapOnSolidBackground】方法调用 | 背景色=0x%08X | 目标尺寸=%dx%d | 前景位图=%s",
bgColor, originalFrameW, originalFrameH,
(fgBitmap != null ? fgBitmap.getWidth() + "x" + fgBitmap.getHeight() : "null")));
// 1. 严格参数校验
if (fgBitmap == null || fgBitmap.isRecycled()) {
LogUtils.e(TAG, "【drawBitmapOnSolidBackground】参数异常前景Bitmap为空或已回收");
return null;
}
if (fgBitmap.getWidth() <= 0 || fgBitmap.getHeight() <= 0) {
LogUtils.e(TAG, "【drawBitmapOnSolidBackground】参数异常前景Bitmap尺寸无效");
return null;
}
if (originalFrameW <= 0 || originalFrameH <= 0) {
LogUtils.e(TAG, "【drawBitmapOnSolidBackground】参数异常原画框尺寸无效宽高必须大于0");
return null;
}
// 1. 严格参数校验
if (fgBitmap == null || fgBitmap.isRecycled()) {
LogUtils.e(TAG, "【drawBitmapOnSolidBackground】参数异常前景Bitmap为空或已回收");
return null;
}
if (fgBitmap.getWidth() <= 0 || fgBitmap.getHeight() <= 0) {
LogUtils.e(TAG, "【drawBitmapOnSolidBackground】参数异常前景Bitmap尺寸无效");
return null;
}
if (originalFrameW <= 0 || originalFrameH <= 0) {
LogUtils.e(TAG, "【drawBitmapOnSolidBackground】参数异常原画框尺寸无效宽高必须大于0");
return null;
}
// 2. 强制画布尺寸为目标尺寸
int canvasW = originalFrameW;
int canvasH = originalFrameH;
LogUtils.d(TAG, String.format("【drawBitmapOnSolidBackground】画布尺寸已确定%dx%d", canvasW, canvasH));
// 2. 强制画布尺寸为目标尺寸
int canvasW = originalFrameW;
int canvasH = originalFrameH;
LogUtils.d(TAG, String.format("【drawBitmapOnSolidBackground】画布尺寸已确定%dx%d", canvasW, canvasH));
// 3. 创建结果位图ARGB_8888支持透明通道
Bitmap resultBitmap = Bitmap.createBitmap(canvasW, canvasH, Bitmap.Config.ARGB_8888);
if (resultBitmap == null) {
LogUtils.e(TAG, "【drawBitmapOnSolidBackground】创建结果Bitmap失败");
return null;
}
// 3. 创建结果位图ARGB_8888支持透明通道
Bitmap resultBitmap = Bitmap.createBitmap(canvasW, canvasH, Bitmap.Config.ARGB_8888);
if (resultBitmap == null) {
LogUtils.e(TAG, "【drawBitmapOnSolidBackground】创建结果Bitmap失败");
return null;
}
// 4. 画布绘制
Canvas canvas = new Canvas(resultBitmap);
try {
// 4.1 绘制纯色背景
Paint bgPaint = new Paint();
bgPaint.setColor(bgColor);
bgPaint.setStyle(Paint.Style.FILL);
canvas.drawRect(0, 0, canvasW, canvasH, bgPaint);
// 4. 画布绘制
Canvas canvas = new Canvas(resultBitmap);
try {
// 4.1 绘制纯色背景
Paint bgPaint = new Paint();
bgPaint.setColor(bgColor);
bgPaint.setStyle(Paint.Style.FILL);
canvas.drawRect(0, 0, canvasW, canvasH, bgPaint);
// 4.2 计算前景 FIT_CENTER 变换参数(等比缩放至完全放入画布,居中显示)
float fgWidth = fgBitmap.getWidth();
float fgHeight = fgBitmap.getHeight();
float scaleX = (float) canvasW / fgWidth;
float scaleY = (float) canvasH / fgHeight;
float scale = Math.min(scaleX, scaleY); // 取最小比例,保证完全放入画布
// 4.2 计算前景 FIT_CENTER 变换参数(等比缩放至完全放入画布,居中显示)
float fgWidth = fgBitmap.getWidth();
float fgHeight = fgBitmap.getHeight();
float scaleX = (float) canvasW / fgWidth;
float scaleY = (float) canvasH / fgHeight;
float scale = Math.min(scaleX, scaleY); // 取最小比例,保证完全放入画布
// 4.3 计算缩放后前景尺寸
float scaledW = fgWidth * scale;
float scaledH = fgHeight * scale;
// 4.3 计算缩放后前景尺寸
float scaledW = fgWidth * scale;
float scaledH = fgHeight * scale;
// 4.4 计算居中位置(缩放后居中,无裁剪)
float translateX = (canvasW - scaledW) / 2f;
float translateY = (canvasH - scaledH) / 2f;
// 4.4 计算居中位置(缩放后居中,无裁剪)
float translateX = (canvasW - scaledW) / 2f;
float translateY = (canvasH - scaledH) / 2f;
// 4.5 构建变换矩阵(缩放+平移,实现 FIT_CENTER 效果)
Matrix matrix = new Matrix();
matrix.postScale(scale, scale); // 等比缩放
matrix.postTranslate(translateX, translateY); // 居中平移
// 4.5 构建变换矩阵(缩放+平移,实现 FIT_CENTER 效果)
Matrix matrix = new Matrix();
matrix.postScale(scale, scale); // 等比缩放
matrix.postTranslate(translateX, translateY); // 居中平移
// 4.6 绘制前景(保留透明通道,抗锯齿)
Paint fgPaint = new Paint();
fgPaint.setAntiAlias(true);
fgPaint.setDither(true);
canvas.drawBitmap(fgBitmap, matrix, fgPaint);
// 4.6 绘制前景(保留透明通道,抗锯齿)
Paint fgPaint = new Paint();
fgPaint.setAntiAlias(true);
fgPaint.setDither(true);
canvas.drawBitmap(fgBitmap, matrix, fgPaint);
LogUtils.d(TAG, String.format("【drawBitmapOnSolidBackground】合成成功 | 缩放比例=%.2f | 居中位置=(%.1f,%.1f) | 效果=FIT_CENTER",
LogUtils.d(TAG, String.format("【drawBitmapOnSolidBackground】合成成功 | 缩放比例=%.2f | 居中位置=(%.1f,%.1f) | 效果=FIT_CENTER",
scale, translateX, translateY));
return resultBitmap;
} catch (Exception e) {
LogUtils.e(TAG, "【drawBitmapOnSolidBackground】合成失败", e);
if (resultBitmap != null && !resultBitmap.isRecycled()) {
resultBitmap.recycle();
}
return null;
}
}
return resultBitmap;
} catch (Exception e) {
LogUtils.e(TAG, "【drawBitmapOnSolidBackground】合成失败", e);
if (resultBitmap != null && !resultBitmap.isRecycled()) {
resultBitmap.recycle();
}
return null;
}
}
// ================================== 核心压缩方法 =================================
/**

View File

@@ -12,15 +12,12 @@ import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import java.io.File;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.models.BackgroundBean;
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
import cc.winboll.studio.powerbell.utils.ImageUtils;
import java.io.File;
/**
* 基于Java7的BackgroundViewLinearLayout+ImageView保持原图比例居中平铺
@@ -50,28 +47,28 @@ public class BackgroundView extends RelativeLayout {
// ====================================== 构造器Java7兼容按参数重载顺序排列 ======================================
public BackgroundView(Context context) {
super(context);
LogUtils.d(TAG, "BackgroundView() 构造器1启动 | context=" + context.getClass().getSimpleName());
LogUtils.d(TAG, String.format("构造器1启动 | context=%s", context.getClass().getSimpleName()));
this.mContext = context;
initView();
}
public BackgroundView(Context context, AttributeSet attrs) {
super(context, attrs);
LogUtils.d(TAG, "BackgroundView(Context, AttributeSet) 构造器2启动 | context=" + context.getClass().getSimpleName());
LogUtils.d(TAG, String.format("构造器2启动 | context=%s", context.getClass().getSimpleName()));
this.mContext = context;
initView();
}
public BackgroundView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LogUtils.d(TAG, "BackgroundView(Context, AttributeSet, int) 构造器3启动 | context=" + context.getClass().getSimpleName());
LogUtils.d(TAG, String.format("构造器3启动 | context=%s", context.getClass().getSimpleName()));
this.mContext = context;
initView();
}
// ====================================== 初始化方法(按执行顺序:主视图→子容器→图片控件→默认背景) ======================================
private void initView() {
LogUtils.d(TAG, "initView() 启动");
LogUtils.d(TAG, "initView启动");
// 1. 配置当前控件:全屏+透明
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// 2. 初始化主容器LinearLayout
@@ -80,11 +77,11 @@ public class BackgroundView extends RelativeLayout {
initImageView();
// 4. 初始设置透明背景
setDefaultEmptyBackground();
LogUtils.d(TAG, "initView() 完成");
LogUtils.d(TAG, "initView完成");
}
private void initLinearLayout() {
LogUtils.d(TAG, "initLinearLayout() 启动");
LogUtils.d(TAG, "initLinearLayout启动");
mLlContainer = new LinearLayout(mContext);
LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
@@ -94,11 +91,11 @@ public class BackgroundView extends RelativeLayout {
mLlContainer.setOrientation(LinearLayout.VERTICAL);
mLlContainer.setGravity(android.view.Gravity.CENTER);
this.addView(mLlContainer);
LogUtils.d(TAG, "initLinearLayout() 完成");
LogUtils.d(TAG, "initLinearLayout完成");
}
private void initImageView() {
LogUtils.d(TAG, "initImageView() 启动");
LogUtils.d(TAG, "initImageView启动");
mIvBackground = new ImageView(mContext);
LinearLayout.LayoutParams ivParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
@@ -107,7 +104,7 @@ public class BackgroundView extends RelativeLayout {
mIvBackground.setLayoutParams(ivParams);
mIvBackground.setScaleType(ImageView.ScaleType.FIT_CENTER);
mLlContainer.addView(mIvBackground);
LogUtils.d(TAG, "initImageView() 完成");
LogUtils.d(TAG, "initImageView完成");
}
// ====================================== 对外公开方法按功能分类Bean加载→图片加载 ======================================
@@ -116,21 +113,21 @@ public class BackgroundView extends RelativeLayout {
}
public void loadByBackgroundBean(BackgroundBean bean, boolean isRefresh) {
LogUtils.d(TAG, String.format("loadByBackgroundBean() 启动 | isRefresh=%b | bean=%s", isRefresh, bean));
LogUtils.d(TAG, String.format("loadByBackgroundBean启动 | isRefresh=%b | bean=%s", isRefresh, bean));
// 参数校验
if (bean == null) {
LogUtils.e(TAG, "loadByBackgroundBean() 异常BackgroundBean为空");
LogUtils.e(TAG, "loadByBackgroundBean异常BackgroundBean为空");
setDefaultEmptyBackground();
return;
}
// 设置图片背景色
mBgColor = bean.getPixelColor();
LogUtils.d(TAG, String.format("loadByBackgroundBean() 背景色设置为 0x%08X", mBgColor));
LogUtils.d(TAG, String.format("loadByBackgroundBean背景色设置为 0x%08X", mBgColor));
// 判断是否使用背景文件
if (!bean.isUseBackgroundFile()) {
LogUtils.d(TAG, "loadByBackgroundBean() 不使用背景文件,设置透明背景");
LogUtils.d(TAG, "loadByBackgroundBean不使用背景文件,设置透明背景");
setDefaultEmptyBackground();
return;
}
@@ -139,13 +136,14 @@ public class BackgroundView extends RelativeLayout {
String targetPath = bean.isUseBackgroundScaledCompressFile()
? bean.getBackgroundScaledCompressFilePath()
: bean.getBackgroundFilePath();
LogUtils.d(TAG, String.format("loadByBackgroundBean() 目标路径=%s | 使用压缩文件=%b",
LogUtils.d(TAG, String.format("loadByBackgroundBean目标路径=%s | 使用压缩文件=%b",
targetPath, bean.isUseBackgroundScaledCompressFile()));
// 校验文件是否存在
File targetFile = new File(targetPath);
if (!targetFile.exists() || !targetFile.isFile()) {
LogUtils.e(TAG, "loadByBackgroundBean() 异常:图片文件不存在 | path=" + targetPath);
LogUtils.e(TAG, String.format("loadByBackgroundBean异常:图片文件不存在 | path=%s", targetPath));
setDefaultEmptyBackground();
return;
}
@@ -153,7 +151,7 @@ public class BackgroundView extends RelativeLayout {
}
public void loadImage(int bgColor, String imagePath, boolean isRefresh) {
LogUtils.d(TAG, String.format("loadImage() 启动 | bgColor=0x%08X | imagePath=%s | isRefresh=%b",
LogUtils.d(TAG, String.format("loadImage启动 | bgColor=0x%08X | imagePath=%s | isRefresh=%b",
bgColor, imagePath, isRefresh));
// 隐藏ImageView防止闪烁
mIvBackground.setVisibility(View.GONE);
@@ -161,39 +159,36 @@ public class BackgroundView extends RelativeLayout {
// 初始化配置工具类并保存默认相框尺寸
AppConfigUtils appConfigUtils = AppConfigUtils.getInstance(mContext);
appConfigUtils.loadAppConfig();
LogUtils.d(TAG, String.format("W : %d, H : %d", appConfigUtils.mAppConfigBean.getDefaultFrameWidth(),
appConfigUtils.mAppConfigBean.getDefaultFrameHeight()));
LogUtils.d(TAG, String.format("【loadImage】默认相框尺寸 | W=%d | H=%d",
appConfigUtils.mAppConfigBean.getDefaultFrameWidth(),
appConfigUtils.mAppConfigBean.getDefaultFrameHeight()));
// 刷新逻辑:重新解码原始品质图片并更新缓存
if (isRefresh) {
LogUtils.d(TAG, "loadImage() 执行刷新逻辑:重新解码原始品质图片");
LogUtils.d(TAG, "loadImage执行刷新逻辑:重新解码原始品质图片");
File imageFile = new File(imagePath);
Bitmap newBitmap = decodeOriginalBitmap(imageFile);
LogUtils.d(TAG, String.format("loadImage() 原始图片解码完成 | newBitmap=%s",
LogUtils.d(TAG, String.format("loadImage原始图片解码完成 | newBitmap=%s",
newBitmap != null ? newBitmap.getWidth() + "x" + newBitmap.getHeight() : "null"));
// 合成纯色背景图片(固定尺寸1197x2287
Bitmap combinedBitmap = ImageUtils.drawBitmapOnSolidBackground(bgColor,
appConfigUtils.mAppConfigBean.getDefaultFrameWidth(),
appConfigUtils.mAppConfigBean.getDefaultFrameHeight(),
newBitmap);
// Bitmap combinedBitmap = ImageUtils.drawBitmapOnSolidBackground(bgColor,
// 1200,
// 2283,
// newBitmap);
// 合成纯色背景图片(使用配置文件中默认相框尺寸
Bitmap combinedBitmap = ImageUtils.drawBitmapOnSolidBackground(
bgColor,
appConfigUtils.mAppConfigBean.getDefaultFrameWidth(),
appConfigUtils.mAppConfigBean.getDefaultFrameHeight(),
newBitmap
);
if (combinedBitmap == null) {
//ToastUtils.show("合成背景图片失败,使用原始图片");
LogUtils.e(TAG, "loadImage() 纯色背景合成失败使用原始Bitmap");
LogUtils.e(TAG, "【loadImage】纯色背景合成失败使用原始Bitmap");
combinedBitmap = newBitmap;
} else {
//ToastUtils.show("合成背景图片成功");
LogUtils.d(TAG, String.format("loadImage() 纯色背景合成成功 | combinedBitmap=%dx%d",
LogUtils.d(TAG, String.format("【loadImage】纯色背景合成成功 | combinedBitmap=%dx%d",
combinedBitmap.getWidth(), combinedBitmap.getHeight()));
// 回收原始Bitmap避免重复缓存
if (newBitmap != null && !newBitmap.isRecycled() && newBitmap != combinedBitmap) {
newBitmap.recycle();
LogUtils.d(TAG, "loadImage() 原始Bitmap已回收");
LogUtils.d(TAG, "loadImage原始Bitmap已回收");
}
}
@@ -202,20 +197,20 @@ public class BackgroundView extends RelativeLayout {
App.sBitmapCacheUtils.cacheBitmap(imagePath, combinedBitmap);
App.sBitmapCacheUtils.increaseRefCount(imagePath);
mCurrentCachedPath = imagePath;
LogUtils.d(TAG, String.format("loadImage() 刷新缓存成功 | path=%s", imagePath));
LogUtils.d(TAG, String.format("loadImage刷新缓存成功 | path=%s", imagePath));
} else {
LogUtils.e(TAG, String.format("loadImage() 刷新解码失败 | path=%s", imagePath));
LogUtils.e(TAG, String.format("loadImage刷新解码失败 | path=%s", imagePath));
}
}
// 加载缓存图片
Bitmap cachedBitmap = App.sBitmapCacheUtils.getCachedBitmap(imagePath);
LogUtils.d(TAG, String.format("loadImage() 加载缓存图片 | cachedBitmap=%s",
LogUtils.d(TAG, String.format("loadImage加载缓存图片 | cachedBitmap=%s",
cachedBitmap != null ? cachedBitmap.getWidth() + "x" + cachedBitmap.getHeight() : "null"));
mIvBackground.setImageBitmap(cachedBitmap);
mIvBackground.setScaleType(ImageView.ScaleType.FIT_CENTER);
mIvBackground.setVisibility(View.VISIBLE);
LogUtils.d(TAG, "loadImage() 完成");
LogUtils.d(TAG, "loadImage完成");
}
// ====================================== 内部工具方法按功能分类Bitmap校验→比例计算→解码→背景设置 ======================================
@@ -225,7 +220,7 @@ public class BackgroundView extends RelativeLayout {
private boolean isBitmapValid(Bitmap bitmap) {
boolean valid = bitmap != null && !bitmap.isRecycled();
if (!valid) {
LogUtils.w(TAG, "isBitmapValid() 无效Bitmap为空或已回收");
LogUtils.w(TAG, "isBitmapValid无效Bitmap为空或已回收");
}
return valid;
}
@@ -234,7 +229,7 @@ public class BackgroundView extends RelativeLayout {
* 计算图片宽高比
*/
private boolean calculateImageAspectRatio(File file) {
LogUtils.d(TAG, String.format("calculateImageAspectRatio() 启动 | file=%s", file.getAbsolutePath()));
LogUtils.d(TAG, String.format("calculateImageAspectRatio启动 | file=%s", file.getAbsolutePath()));
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
@@ -244,16 +239,16 @@ public class BackgroundView extends RelativeLayout {
int width = options.outWidth;
int height = options.outHeight;
if (width <= 0 || height <= 0) {
LogUtils.e(TAG, String.format("calculateImageAspectRatio() 无效尺寸 | width=%d | height=%d", width, height));
LogUtils.e(TAG, String.format("calculateImageAspectRatio无效尺寸 | width=%d | height=%d", width, height));
return false;
}
// 计算比例
mImageAspectRatio = (float) width / height;
LogUtils.d(TAG, String.format("calculateImageAspectRatio() 完成 | 比例=%.2f", mImageAspectRatio));
LogUtils.d(TAG, String.format("calculateImageAspectRatio完成 | 比例=%.2f", mImageAspectRatio));
return true;
} catch (Exception e) {
LogUtils.e(TAG, "calculateImageAspectRatio() 失败:" + e.getMessage());
LogUtils.e(TAG, String.format("calculateImageAspectRatio失败:%s", e.getMessage()));
return false;
}
}
@@ -262,7 +257,7 @@ public class BackgroundView extends RelativeLayout {
* 移除压缩逻辑:解码原始品质图片(无缩放、无色彩损失)
*/
private Bitmap decodeOriginalBitmap(File file) {
LogUtils.d(TAG, String.format("decodeOriginalBitmap() 启动 | file=%s", file.getAbsolutePath()));
LogUtils.d(TAG, String.format("decodeOriginalBitmap启动 | file=%s", file.getAbsolutePath()));
try {
BitmapFactory.Options options = new BitmapFactory.Options();
// 核心配置:原始品质
@@ -276,13 +271,13 @@ public class BackgroundView extends RelativeLayout {
// 解码图片
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
if (bitmap != null) {
LogUtils.d(TAG, String.format("decodeOriginalBitmap() 成功 | width=%d | height=%d", bitmap.getWidth(), bitmap.getHeight()));
LogUtils.d(TAG, String.format("decodeOriginalBitmap成功 | width=%d | height=%d", bitmap.getWidth(), bitmap.getHeight()));
} else {
LogUtils.e(TAG, "decodeOriginalBitmap() 失败返回null");
LogUtils.e(TAG, "decodeOriginalBitmap失败返回null");
}
return bitmap;
} catch (Exception e) {
LogUtils.e(TAG, "decodeOriginalBitmap() 异常:" + e.getMessage());
LogUtils.e(TAG, String.format("decodeOriginalBitmap异常:%s", e.getMessage()));
return null;
}
}
@@ -291,18 +286,18 @@ public class BackgroundView extends RelativeLayout {
* 设置默认透明背景,仅减少引用计数,不删除缓存
*/
private void setDefaultEmptyBackground() {
LogUtils.d(TAG, "setDefaultEmptyBackground() 启动");
LogUtils.d(TAG, "setDefaultEmptyBackground启动");
// 清空ImageView
mIvBackground.setImageDrawable(null);
mImageAspectRatio = 1.0f;
// 减少引用计数,不删除缓存
if (!TextUtils.isEmpty(mCurrentCachedPath)) {
LogUtils.d(TAG, String.format("setDefaultEmptyBackground() 减少引用计数 | path=%s", mCurrentCachedPath));
LogUtils.d(TAG, String.format("setDefaultEmptyBackground减少引用计数 | path=%s", mCurrentCachedPath));
App.sBitmapCacheUtils.decreaseRefCount(mCurrentCachedPath);
mCurrentCachedPath = "";
}
LogUtils.d(TAG, "setDefaultEmptyBackground() 完成");
LogUtils.d(TAG, "setDefaultEmptyBackground完成");
}
// ====================================== 重写生命周期方法(按执行顺序:绘制→尺寸变化→窗口分离) ======================================
@@ -316,7 +311,7 @@ public class BackgroundView extends RelativeLayout {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
if (!isBitmapValid(bitmap)) {
LogUtils.e(TAG, "onDraw() 检测到已回收Bitmap清空绘制");
LogUtils.e(TAG, "onDraw检测到已回收Bitmap清空绘制");
mIvBackground.setImageDrawable(null);
return;
}
@@ -330,7 +325,7 @@ public class BackgroundView extends RelativeLayout {
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
LogUtils.d(TAG, String.format("onSizeChanged() 尺寸变化 | newW=%d | newH=%d | oldW=%d | oldH=%d", w, h, oldw, oldh));
LogUtils.d(TAG, String.format("onSizeChanged尺寸变化 | newW=%d | newH=%d | oldW=%d | oldH=%d", w, h, oldw, oldh));
}
/**
@@ -339,17 +334,17 @@ public class BackgroundView extends RelativeLayout {
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
LogUtils.d(TAG, "onDetachedFromWindow() 启动");
LogUtils.d(TAG, "onDetachedFromWindow启动");
// 清空ImageView的Drawable释放本地引用
mIvBackground.setImageDrawable(null);
// 减少引用计数,不删除全局缓存
if (!TextUtils.isEmpty(mCurrentCachedPath)) {
LogUtils.d(TAG, String.format("onDetachedFromWindow() 减少引用计数 | path=%s", mCurrentCachedPath));
LogUtils.d(TAG, String.format("onDetachedFromWindow减少引用计数 | path=%s", mCurrentCachedPath));
App.sBitmapCacheUtils.decreaseRefCount(mCurrentCachedPath);
mCurrentCachedPath = "";
}
LogUtils.d(TAG, "onDetachedFromWindow() 完成");
LogUtils.d(TAG, "onDetachedFromWindow完成");
}
}