修复背景图片右边有缝隙,以及像素背景设置的问题。

This commit is contained in:
2025-11-27 14:24:55 +08:00
parent af53216af3
commit e76427eac8
4 changed files with 70 additions and 26 deletions

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle #Created by .winboll/winboll_app_build.gradle
#Thu Nov 27 05:33:36 GMT 2025 #Thu Nov 27 06:21:31 GMT 2025
stageCount=9 stageCount=9
libraryProject= libraryProject=
baseVersion=15.11 baseVersion=15.11
publishVersion=15.11.8 publishVersion=15.11.8
buildCount=6 buildCount=18
baseBetaVersion=15.11.9 baseBetaVersion=15.11.9

View File

@@ -19,8 +19,11 @@ import android.widget.TextView;
import cc.winboll.studio.libappbase.LogUtils; import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.App; import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.R; import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.activities.PixelPickerActivity;
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
import cc.winboll.studio.powerbell.services.ControlCenterService; import cc.winboll.studio.powerbell.services.ControlCenterService;
import cc.winboll.studio.powerbell.utils.AppConfigUtils; import cc.winboll.studio.powerbell.utils.AppConfigUtils;
import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils;
import cc.winboll.studio.powerbell.utils.ServiceUtils; import cc.winboll.studio.powerbell.utils.ServiceUtils;
import cc.winboll.studio.powerbell.views.BackgroundView; import cc.winboll.studio.powerbell.views.BackgroundView;
import cc.winboll.studio.powerbell.views.BatteryDrawable; import cc.winboll.studio.powerbell.views.BatteryDrawable;
@@ -79,6 +82,10 @@ public class MainViewFragment extends Fragment {
// 获取指定ID的View实例 // 获取指定ID的View实例
bvPreviewBackground = mView.findViewById(R.id.fragmentmainviewBackgroundView1); bvPreviewBackground = mView.findViewById(R.id.fragmentmainviewBackgroundView1);
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(getActivity());
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
int nPixelColor = bean.getPixelColor();
bvPreviewBackground.setBackgroundColor(nPixelColor);
/*final View mainImageView = mView.findViewById(R.id.fragmentmainviewImageView1); /*final View mainImageView = mView.findViewById(R.id.fragmentmainviewImageView1);
// 注册OnGlobalLayoutListener // 注册OnGlobalLayoutListener
@@ -141,6 +148,17 @@ public class MainViewFragment extends Fragment {
return mView; return mView;
} }
@Override
public void onResume() {
super.onResume();
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(getActivity());
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
int nPixelColor = bean.getPixelColor();
bvPreviewBackground.setBackgroundColor(nPixelColor);
}
void setViewData() { void setViewData() {
int nChargeReminderValue = mAppConfigUtils.getChargeReminderValue(); int nChargeReminderValue = mAppConfigUtils.getChargeReminderValue();
int nUsegeReminderValue = mAppConfigUtils.getUsegeReminderValue(); int nUsegeReminderValue = mAppConfigUtils.getUsegeReminderValue();

View File

@@ -4,6 +4,7 @@ import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.widget.ImageView; import android.widget.ImageView;
@@ -17,7 +18,7 @@ import java.io.FileOutputStream;
/** /**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com> * @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/19 18:01 * @Date 2025/11/19 18:01
* @Describe 背景图片视图控件(支持预览临时图片 + 外部刷新 * @Describe 背景图片视图控件(全透明背景 + 不拉伸居中平铺 + 完全填充父视图
*/ */
public class BackgroundView extends RelativeLayout { public class BackgroundView extends RelativeLayout {
@@ -30,8 +31,8 @@ public class BackgroundView extends RelativeLayout {
private static String BACKGROUND_IMAGE_FILENAME = "current.data"; private static String BACKGROUND_IMAGE_FILENAME = "current.data";
private static String BACKGROUND_IMAGE_PREVIEW_FILENAME = "current_preview.data"; private static String BACKGROUND_IMAGE_PREVIEW_FILENAME = "current_preview.data";
private static String backgroundSourceFilePath; private static String backgroundSourceFilePath;
private float imageAspectRatio = 1.0f; // 默认 1:1 private float imageAspectRatio = 1.0f; // 图片原始宽高比(控制不拉伸)
// 标记当前是否处于预览状态 // 标记当前是否处于预览模式
private boolean isPreviewMode = false; private boolean isPreviewMode = false;
public BackgroundView(Context context) { public BackgroundView(Context context) {
@@ -59,18 +60,32 @@ public class BackgroundView extends RelativeLayout {
} }
void initView() { void initView() {
initBackgroundImageView(); // 1. 控件本身:完全填充父视图 + 全透明背景 + 无内边距
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setPadding(0, 0, 0, 0); // 取消自身内边距
setBackgroundColor(0x00000000); // 全透明背景(#00000000
setBackground(new ColorDrawable(0x00000000)); // 双重保障同时设置Background为透明兼容低版本
initBackgroundImageView(); // 初始化内部ImageView全透明 + 不拉伸居中平铺)
initBackgroundImagePath(); initBackgroundImagePath();
loadAndSetImageViewBackground(); loadAndSetImageViewBackground();
} }
private void initBackgroundImageView() { private void initBackgroundImageView() {
ivBackground = new ImageView(mContext); ivBackground = new ImageView(mContext);
// 2. ImageView初始宽高WRAP_CONTENT + 居中 + 无内边距 + 全透明背景
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); layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); // 居中显示
layoutParams.setMargins(0, 0, 0, 0); // 取消边距
ivBackground.setLayoutParams(layoutParams); ivBackground.setLayoutParams(layoutParams);
// 3. 缩放模式FIT_CENTER不拉伸按比例显示
ivBackground.setScaleType(ImageView.ScaleType.FIT_CENTER); ivBackground.setScaleType(ImageView.ScaleType.FIT_CENTER);
ivBackground.setPadding(0, 0, 0, 0); // 取消内部padding
ivBackground.setBackgroundColor(0x00000000); // ImageView背景全透明
ivBackground.setBackground(new ColorDrawable(0x00000000)); // 双重保障(兼容低版本)
this.addView(ivBackground); this.addView(ivBackground);
} }
@@ -160,35 +175,39 @@ public class BackgroundView extends RelativeLayout {
} }
/** /**
* 【新增公共函数】预览临时图片(不修改正式背景文件 * 预览临时图片(全透明背景 + 不拉伸居中平铺
* @param previewImagePath 临时预览图片的路径 * @param previewImagePath 临时预览图片的路径
*/ */
public void previewBackgroundImage(String previewImagePath) { public void previewBackgroundImage(String previewImagePath) {
if (previewImagePath == null || previewImagePath.isEmpty()) { if (previewImagePath == null || previewImagePath.isEmpty()) {
LogUtils.e(TAG, "预览图片路径为空"); LogUtils.e(TAG, "预览图片路径为空");
setDefaultImageViewBackground();
return; return;
} }
File previewFile = new File(previewImagePath); File previewFile = new File(previewImagePath);
if (!previewFile.exists() || !previewFile.isFile()) { if (!previewFile.exists() || !previewFile.isFile()) {
LogUtils.e(TAG, "预览图片不存在或不是文件:" + previewImagePath); LogUtils.e(TAG, "预览图片不存在或不是文件:" + previewImagePath);
setDefaultImageViewBackground();
return; return;
} }
// 计算预览图片宽高比 // 计算图片原始宽高比
if (!calculateImageAspectRatio(previewFile)) { if (!calculateImageAspectRatio(previewFile)) {
LogUtils.e(TAG, "预览图片尺寸无效,无法预览"); LogUtils.e(TAG, "预览图片尺寸无效,无法预览");
setDefaultImageViewBackground();
return; return;
} }
// 压缩加载预览图片 // 压缩加载预览图片(保持比例)
Bitmap previewBitmap = decodeBitmapWithCompress(previewFile, 1080, 1920); Bitmap previewBitmap = decodeBitmapWithCompress(previewFile, 1080, 1920);
if (previewBitmap == null) { if (previewBitmap == null) {
LogUtils.e(TAG, "预览图片加载失败"); LogUtils.e(TAG, "预览图片加载失败");
setDefaultImageViewBackground();
return; return;
} }
// 设置预览图片ImageView // 设置预览图片(保持ImageView透明背景)
Drawable previewDrawable = new BitmapDrawable(mContext.getResources(), previewBitmap); Drawable previewDrawable = new BitmapDrawable(mContext.getResources(), previewBitmap);
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(previewDrawable); ivBackground.setBackground(previewDrawable);
@@ -196,14 +215,14 @@ public class BackgroundView extends RelativeLayout {
ivBackground.setBackgroundDrawable(previewDrawable); ivBackground.setBackgroundDrawable(previewDrawable);
} }
// 调整 ImageView 尺寸以匹配预览图片宽高比 // 调整ImageView尺寸(居中平铺,不拉伸)
adjustImageViewSize(); adjustImageViewSize();
isPreviewMode = true; isPreviewMode = true;
LogUtils.d(TAG, "进入预览模式,预览图片路径:" + previewImagePath); LogUtils.d(TAG, "进入预览模式,预览图片路径:" + previewImagePath + ",宽高比:" + imageAspectRatio);
} }
/** /**
* 【新增公共函数】退出预览模式,恢复显示正式背景图片 * 退出预览模式,恢复显示正式背景图片
*/ */
public void exitPreviewMode() { public void exitPreviewMode() {
if (isPreviewMode) { if (isPreviewMode) {
@@ -227,7 +246,7 @@ public class BackgroundView extends RelativeLayout {
} }
/** /**
* 加载正式背景图片并设置到 ImageView * 加载正式背景图片并设置到 ImageView(全透明背景 + 不拉伸居中平铺)
*/ */
private void loadAndSetImageViewBackground() { private void loadAndSetImageViewBackground() {
if (backgroundSourceFilePath == null) { if (backgroundSourceFilePath == null) {
@@ -242,11 +261,13 @@ public class BackgroundView extends RelativeLayout {
return; return;
} }
// 计算图片原始宽高比
if (!calculateImageAspectRatio(backgroundFile)) { if (!calculateImageAspectRatio(backgroundFile)) {
setDefaultImageViewBackground(); setDefaultImageViewBackground();
return; return;
} }
// 压缩加载 Bitmap保持比例
Bitmap bitmap = decodeBitmapWithCompress(backgroundFile, 1080, 1920); Bitmap bitmap = decodeBitmapWithCompress(backgroundFile, 1080, 1920);
if (bitmap == null) { if (bitmap == null) {
LogUtils.e(TAG, "图片加载失败,无法解析为 Bitmap"); LogUtils.e(TAG, "图片加载失败,无法解析为 Bitmap");
@@ -254,6 +275,7 @@ public class BackgroundView extends RelativeLayout {
return; return;
} }
// 设置图片保持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); ivBackground.setBackground(backgroundDrawable);
@@ -261,12 +283,13 @@ public class BackgroundView extends RelativeLayout {
ivBackground.setBackgroundDrawable(backgroundDrawable); ivBackground.setBackgroundDrawable(backgroundDrawable);
} }
// 调整ImageView尺寸居中平铺不拉伸
adjustImageViewSize(); adjustImageViewSize();
LogUtils.d(TAG, "ImageView 背景加载成功,宽高比:" + imageAspectRatio); LogUtils.d(TAG, "ImageView 背景加载成功(全透明+不拉伸),宽高比:" + imageAspectRatio);
} }
/** /**
* 计算图片宽高比(宽/高) * 计算图片原始宽高比(宽/高)
*/ */
private boolean calculateImageAspectRatio(File file) { private boolean calculateImageAspectRatio(File file) {
try { try {
@@ -291,11 +314,11 @@ public class BackgroundView extends RelativeLayout {
} }
/** /**
* 动态调整 ImageView 尺寸以匹配图片宽高比 * 调整ImageView尺寸(不拉伸,居中平铺)
*/ */
private void adjustImageViewSize() { private void adjustImageViewSize() {
int parentWidth = getWidth(); int parentWidth = getWidth(); // 控件宽度(已填充父视图)
int parentHeight = getHeight(); int parentHeight = getHeight(); // 控件高度(已填充父视图)
if (parentWidth == 0 || parentHeight == 0) { if (parentWidth == 0 || parentHeight == 0) {
post(new Runnable() { post(new Runnable() {
@@ -316,14 +339,17 @@ public class BackgroundView extends RelativeLayout {
imageViewWidth = (int) (imageViewHeight * imageAspectRatio); imageViewWidth = (int) (imageViewHeight * imageAspectRatio);
} }
// 应用尺寸
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ivBackground.getLayoutParams(); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ivBackground.getLayoutParams();
layoutParams.width = imageViewWidth; layoutParams.width = imageViewWidth;
layoutParams.height = imageViewHeight; layoutParams.height = imageViewHeight;
ivBackground.setLayoutParams(layoutParams); ivBackground.setLayoutParams(layoutParams);
LogUtils.d(TAG, "ImageView 尺寸调整完成:宽=" + imageViewWidth + ", 高=" + imageViewHeight);
} }
/** /**
* 带压缩的 Bitmap 解码(避免 OOM * 带压缩的 Bitmap 解码(保持比例,避免 OOM
*/ */
private Bitmap decodeBitmapWithCompress(File file, int maxWidth, int maxHeight) { private Bitmap decodeBitmapWithCompress(File file, int maxWidth, int maxHeight) {
try { try {
@@ -349,13 +375,14 @@ public class BackgroundView extends RelativeLayout {
} }
/** /**
* 设置默认背景(图片加载失败时兜底 * 设置默认背景(全透明兜底,避免空白
*/ */
private void setDefaultImageViewBackground() { private void setDefaultImageViewBackground() {
ivBackground.setBackgroundResource(R.drawable.default_background); // 关键:默认背景设为全透明(而非默认图,避免遮挡下层视图)
ivBackground.setBackground(new ColorDrawable(0x00000000)); // 全透明背景
imageAspectRatio = 1.0f; imageAspectRatio = 1.0f;
adjustImageViewSize(); adjustImageViewSize();
LogUtils.d(TAG, "已设置 ImageView 默认背景"); LogUtils.d(TAG, "已设置默认透明背景");
} }
@Override @Override

View File

@@ -21,8 +21,7 @@
<RelativeLayout <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:id="@+id/activitymainRelativeLayout1" android:id="@+id/activitymainRelativeLayout1"/>
android:background="#FFB7B7B7"/>
<FrameLayout <FrameLayout
android:layout_width="match_parent" android:layout_width="match_parent"