Compare commits
3 Commits
8e1d6ba197
...
68d98d4be3
| Author | SHA1 | Date | |
|---|---|---|---|
| 68d98d4be3 | |||
| 4db458dda8 | |||
| 83a8f5dada |
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Tue Dec 16 04:10:23 GMT 2025
|
||||
#Tue Dec 16 08:20:17 GMT 2025
|
||||
stageCount=7
|
||||
libraryProject=
|
||||
baseVersion=15.14
|
||||
publishVersion=15.14.6
|
||||
buildCount=1
|
||||
buildCount=45
|
||||
baseBetaVersion=15.14.7
|
||||
|
||||
@@ -207,6 +207,7 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
|
||||
findViewById(R.id.activitybackgroundpictureAButton6).setOnClickListener(onCropFreePictureClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton7).setOnClickListener(onPixelPickerClickListener);
|
||||
findViewById(R.id.activitybackgroundpictureAButton8).setOnClickListener(onCleanPixelClickListener);
|
||||
findViewById(R.id.activitybackgroundsettingsAButton1).setOnClickListener(onColorPaletteClickListener);
|
||||
}
|
||||
|
||||
// ====================== 按钮点击事件 ======================
|
||||
@@ -330,6 +331,32 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
|
||||
}
|
||||
};
|
||||
|
||||
private View.OnClickListener onColorPaletteClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
LogUtils.d(TAG, "【按钮点击】调色板按钮");
|
||||
|
||||
// 初始颜色(白色,含透明度)
|
||||
//int initialColor = 0xFFFFFFFF;
|
||||
int initialColor = mBgSourceUtils.getPreviewBackgroundBean().getPixelColor();
|
||||
// 显示对话框
|
||||
ColorPaletteDialog dialog = new ColorPaletteDialog(BackgroundSettingsActivity.this, initialColor, new ColorPaletteDialog.OnColorSelectedListener() {
|
||||
@Override
|
||||
public void onColorSelected(int color) {
|
||||
// 回调返回 0xAARRGGBB 格式颜色,直接使用
|
||||
mBgSourceUtils.getPreviewBackgroundBean().setPixelColor(color);
|
||||
mBgSourceUtils.saveSettings();
|
||||
doubleRefreshPreview();
|
||||
isPreviewBackgroundChanged = true;
|
||||
LogUtils.d("选择颜色", String.format("#%08X", color));
|
||||
}
|
||||
});
|
||||
dialog.show();
|
||||
|
||||
LogUtils.d(TAG, "调色板按钮响应完成。");
|
||||
}
|
||||
};
|
||||
|
||||
// ====================== 工具方法 ======================
|
||||
/**
|
||||
* 生成 FileProvider Uri,适配 Android 7.0+
|
||||
@@ -864,22 +891,5 @@ public class BackgroundSettingsActivity extends WinBoLLActivity {
|
||||
doubleRefreshPreview();
|
||||
isPreviewBackgroundChanged = true;
|
||||
}
|
||||
|
||||
public void onColorPaletteDialog(View view) {
|
||||
// 初始颜色(白色,含透明度)
|
||||
//int initialColor = 0xFFFFFFFF;
|
||||
int initialColor = mBgSourceUtils.getPreviewBackgroundBean().getPixelColor();
|
||||
// 显示对话框
|
||||
ColorPaletteDialog dialog = new ColorPaletteDialog(this, initialColor, new ColorPaletteDialog.OnColorSelectedListener() {
|
||||
@Override
|
||||
public void onColorSelected(int color) {
|
||||
// 回调返回 0xAARRGGBB 格式颜色,直接使用
|
||||
mBgSourceUtils.getPreviewBackgroundBean().setPixelColor(color);
|
||||
doubleRefreshPreview();
|
||||
LogUtils.d("选择颜色", String.format("#%08X", color));
|
||||
}
|
||||
});
|
||||
dialog.show();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package cc.winboll.studio.powerbell.dialogs;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.GradientDrawable;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.TextUtils;
|
||||
@@ -13,6 +14,7 @@ import android.view.View;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.EditText;
|
||||
import android.widget.HorizontalScrollView;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.SeekBar;
|
||||
@@ -21,336 +23,656 @@ import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.powerbell.R;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Author ZhanGSKen<zhangsken@qq.com>
|
||||
* @Date 2025/12/16 11:47
|
||||
* @Describe 调色板对话框(支持颜色拾取、RGB输入、透明度/色阶调节,兼容 API29-30+ 小米机型)
|
||||
* @Describe 调色板对话框(支持颜色拾取、RGB输入、透明度/亮度调节,兼容 API29-30+ 小米机型)
|
||||
*/
|
||||
public class ColorPaletteDialog extends Dialog implements View.OnClickListener {
|
||||
// ====================== 常量/成员变量 ======================
|
||||
private static final String TAG = "ColorPaletteDialog";
|
||||
private static final int MAX_PROGRESS = 255; // 透明度/色阶最大值(0-255)
|
||||
private OnColorSelectedListener mListener; // 颜色选择回调接口
|
||||
private int mInitialColor; // 初始颜色(含透明度,默认白色0xFFFFFFFF)
|
||||
private int mCurrentColor; // 当前选择的颜色
|
||||
public class ColorPaletteDialog extends Dialog implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
|
||||
// ====================== 常量定义(首屏可见,统一管理) ======================
|
||||
public static final String TAG = "ColorPaletteDialog";
|
||||
private static final int MAX_RGB_VALUE = 255; // RGB分量最大值(0-255)
|
||||
private static final int DEFAULT_BRIGHTNESS = 100; // 默认亮度百分比(100%,无调节)
|
||||
private static final int BRIGHTNESS_STEP = 5; // 亮度调节步长(每次±5%,精准流畅)
|
||||
private static final int MIN_BRIGHTNESS = 10; // 亮度最小值(10%,避免全黑看不见)
|
||||
private static final int MAX_BRIGHTNESS = 200; // 亮度最大值(200%,避免过曝失真)
|
||||
private static final int MAX_ALPHA_PERCENT = 100; // 透明度最大值(100%=不透明)
|
||||
private static final int MIN_ALPHA_PERCENT = 0; // 透明度最小值(0%=完全透明)
|
||||
|
||||
// 控件引用
|
||||
private ImageView ivColorPicker; // 颜色拾取框(点击选色)
|
||||
private EditText etR, etG, etB; // RGB 颜色输入框
|
||||
private EditText etColorValue; // 颜色值输入框(如 #FFFFFF)
|
||||
private SeekBar sbAlpha; // 透明度进度条(0=全透明,255=不透明)
|
||||
private SeekBar sbBrightness; // 色阶进度条(调节亮度,0=最暗,255=最亮)
|
||||
// ====================== 回调接口(紧跟常量,逻辑关联) ======================
|
||||
public interface OnColorSelectedListener {
|
||||
void onColorSelected(int color); // 返回0xAARRGGBB格式颜色(含透明度)
|
||||
}
|
||||
|
||||
// ====================== 成员变量(按优先级排序:核心数据→控件引用) ======================
|
||||
// 核心数据:原始基准值(用户输入/选择颜色时更新)+ 实时调节值(亮度/透明度变化时更新)
|
||||
private OnColorSelectedListener mListener; // 颜色选择回调(非空校验)
|
||||
private int mInitialColor; // 初始颜色(传入的默认颜色)
|
||||
private int mCurrentColor; // 当前最终颜色(含亮度+透明度调节)
|
||||
private int mCurrentBrightnessPercent; // 当前亮度百分比(10%-200%)
|
||||
// 透明度:百分比(0-100%,用户直观操作)+ 原始/实时值(0-255,颜色计算用)
|
||||
private int mOriginalAlphaPercent; // 原始透明度百分比(基准值,用户输入/选色时更新)
|
||||
private int mCurrentAlphaPercent; // 实时透明度百分比(调节进度条时更新)
|
||||
private int mOriginalAlpha; // 原始透明度(0-255,基准值)
|
||||
private int mCurrentAlpha; // 实时透明度(0-255,计算用)
|
||||
// RGB:原始基准值+实时调节值
|
||||
private int mOriginalR; // 原始R分量(基准值,用户输入/选色时更新)
|
||||
private int mOriginalG; // 原始G分量(基准值,用户输入/选色时更新)
|
||||
private int mOriginalB; // 原始B分量(基准值,用户输入/选色时更新)
|
||||
private int mCurrentR; // 实时R分量(亮度调节后,同步输入框显示)
|
||||
private int mCurrentG; // 实时G分量(亮度调节后,同步输入框显示)
|
||||
private int mCurrentB; // 实时B分量(亮度调节后,同步输入框显示)
|
||||
// 并发控制标记:是否是应用程序自身在更新颜色(避免循环回调/重复触发)
|
||||
private static volatile boolean isAppSelfUpdatingColor = false;
|
||||
|
||||
// 控件引用(新增透明度进度条+文本)
|
||||
private ImageView ivColorPicker; // 颜色预览拾取框
|
||||
private EditText etR; // R分量输入框(显示实时调节值)
|
||||
private EditText etG; // G分量输入框(显示实时调节值)
|
||||
private EditText etB; // B分量输入框(显示实时调节值)
|
||||
private EditText etColorValue; // 颜色值输入框(#AARRGGBB,显示最终值)
|
||||
private SeekBar sbAlpha; // 透明度调节进度条(0-100%)
|
||||
private TextView tvAlphaValue; // 透明度数值显示(X%)
|
||||
private TextView tvBrightnessMinus;// 亮度减少按钮(-)
|
||||
private TextView tvBrightnessValue;// 亮度数值显示(X%,直观易懂)
|
||||
private TextView tvBrightnessPlus; // 亮度增加按钮(+)
|
||||
private TextView tvConfirm; // 确认按钮
|
||||
private TextView tvCancel; // 取消按钮
|
||||
|
||||
|
||||
// ====================== 回调接口 ======================
|
||||
/**
|
||||
* 颜色选择完成回调接口
|
||||
*/
|
||||
public interface OnColorSelectedListener {
|
||||
void onColorSelected(int color); // 返回 0xAARRGGBB 格式颜色
|
||||
}
|
||||
|
||||
|
||||
// ====================== 构造方法 ======================
|
||||
// ====================== 构造方法(初始化核心数据,严格校验) ======================
|
||||
public ColorPaletteDialog(Context context, int initialColor, OnColorSelectedListener listener) {
|
||||
super(context, R.style.CustomDialogStyle);
|
||||
this.mInitialColor = initialColor;
|
||||
this.mCurrentColor = initialColor;
|
||||
this.mListener = listener;
|
||||
|
||||
// 1. 强制回调非空,避免后续空指针(容错)
|
||||
if (mListener == null) {
|
||||
throw new IllegalArgumentException("OnColorSelectedListener 不能为 null!");
|
||||
}
|
||||
throw new IllegalArgumentException("OnColorSelectedListener can not be null!");
|
||||
}
|
||||
|
||||
// 2. 解析初始颜色:原始基准值 = 实时值(初始无调节)
|
||||
// 透明度:初始颜色的alpha(0-255)转百分比(0-100%)
|
||||
this.mOriginalAlpha = Color.alpha(initialColor);
|
||||
this.mOriginalAlphaPercent = alpha2Percent(mOriginalAlpha);
|
||||
this.mCurrentAlpha = mOriginalAlpha;
|
||||
this.mCurrentAlphaPercent = mOriginalAlphaPercent;
|
||||
// RGB:初始颜色的RGB分量
|
||||
this.mOriginalR = Color.red(initialColor);
|
||||
this.mOriginalG = Color.green(initialColor);
|
||||
this.mOriginalB = Color.blue(initialColor);
|
||||
this.mCurrentR = mOriginalR;
|
||||
this.mCurrentG = mOriginalG;
|
||||
this.mCurrentB = mOriginalB;
|
||||
|
||||
// ====================== 生命周期 ======================
|
||||
// 3. 初始化当前状态(默认亮度100%,当前颜色=初始颜色)
|
||||
this.mCurrentBrightnessPercent = DEFAULT_BRIGHTNESS;
|
||||
this.mCurrentColor = initialColor;
|
||||
|
||||
LogUtils.d(TAG, "init dialog success | 初始颜色:" + String.format("#%08X", initialColor)
|
||||
+ " | 原始RGB:" + mOriginalR + "," + mOriginalG + "," + mOriginalB
|
||||
+ " | 原始透明度:" + mOriginalAlphaPercent + "%"
|
||||
+ " | 初始亮度:" + mCurrentBrightnessPercent + "%");
|
||||
}
|
||||
|
||||
// ====================== 生命周期方法(按执行顺序排列,逻辑清晰) ======================
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题栏
|
||||
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_color_palette, null);
|
||||
setContentView(view);
|
||||
initView(view);
|
||||
|
||||
// 初始化流程:控件绑定→数据赋值→监听设置→尺寸适配(小米机型优先适配)
|
||||
initViewBind(view);
|
||||
initData();
|
||||
initListener();
|
||||
adjustDialogSize();
|
||||
LogUtils.d(TAG, "dialog create complete | 适配小米API29-30机型");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dismiss() {
|
||||
super.dismiss();
|
||||
// 释放资源,避免内存泄漏(回调引用置空)
|
||||
mListener = null;
|
||||
LogUtils.d(TAG, "dialog dismiss | 释放资源完成");
|
||||
}
|
||||
|
||||
// ====================== 初始化方法 ======================
|
||||
private void initView(View view) {
|
||||
// 颜色拾取框
|
||||
// ====================== 初始化核心方法(职责单一,便于维护) ======================
|
||||
/**
|
||||
* 控件绑定(新增透明度进度条+文本绑定)
|
||||
*/
|
||||
private void initViewBind(View view) {
|
||||
ivColorPicker = view.findViewById(R.id.iv_color_picker);
|
||||
ivColorPicker.setOnClickListener(this);
|
||||
// RGB 输入框
|
||||
etR = view.findViewById(R.id.et_r);
|
||||
etG = view.findViewById(R.id.et_g);
|
||||
etB = view.findViewById(R.id.et_b);
|
||||
setEditTextWatcher(etR);
|
||||
setEditTextWatcher(etG);
|
||||
setEditTextWatcher(etB);
|
||||
// 颜色值输入框
|
||||
etColorValue = view.findViewById(R.id.et_color_value);
|
||||
etColorValue.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {}
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
parseColorFromStr(s.toString().trim());
|
||||
}
|
||||
});
|
||||
// 透明度进度条
|
||||
sbAlpha = view.findViewById(R.id.sb_alpha);
|
||||
sbAlpha.setMax(MAX_PROGRESS);
|
||||
sbAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
if (fromUser) updateColorByAlpha(progress);
|
||||
}
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {}
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {}
|
||||
});
|
||||
// 色阶进度条
|
||||
sbBrightness = view.findViewById(R.id.sb_brightness);
|
||||
sbBrightness.setMax(MAX_PROGRESS);
|
||||
sbBrightness.setProgress(128);
|
||||
sbBrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
if (fromUser) updateColorByBrightness(progress);
|
||||
}
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {}
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {}
|
||||
});
|
||||
// 按钮
|
||||
tvAlphaValue = view.findViewById(R.id.tv_alpha_value);
|
||||
tvBrightnessMinus = view.findViewById(R.id.tv_brightness_minus);
|
||||
tvBrightnessValue = view.findViewById(R.id.tv_brightness_value);
|
||||
tvBrightnessPlus = view.findViewById(R.id.tv_brightness_plus);
|
||||
tvConfirm = view.findViewById(R.id.tv_confirm);
|
||||
tvCancel = view.findViewById(R.id.tv_cancel);
|
||||
|
||||
// 控件非空校验(小米低版本容错,绑定失败直接关闭对话框)
|
||||
if (ivColorPicker == null || etR == null || etG == null || etB == null || etColorValue == null
|
||||
|| sbAlpha == null || tvAlphaValue == null
|
||||
|| tvBrightnessMinus == null || tvBrightnessValue == null || tvBrightnessPlus == null
|
||||
|| tvConfirm == null || tvCancel == null) {
|
||||
LogUtils.e(TAG, "view bind failed | 请检查布局ID是否正确!");
|
||||
dismiss();
|
||||
return;
|
||||
}
|
||||
LogUtils.d(TAG, "view bind complete | 所有控件绑定成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据初始化(无监听状态下赋值,避免循环回调)
|
||||
*/
|
||||
private void initData() {
|
||||
// 1. 颜色预览(显示当前最终颜色,初始=原始颜色)
|
||||
ivColorPicker.setBackgroundColor(mCurrentColor);
|
||||
|
||||
// 2. RGB输入框(显示「实时分量」,初始=原始值)
|
||||
etR.setText(String.valueOf(mCurrentR));
|
||||
etG.setText(String.valueOf(mCurrentG));
|
||||
etB.setText(String.valueOf(mCurrentB));
|
||||
|
||||
// 3. 颜色值输入框(显示当前最终颜色,格式#AARRGGBB,大写更规范)
|
||||
etColorValue.setText(String.format("#%08X", mCurrentColor));
|
||||
|
||||
// 4. 透明度控件(进度条+文本,初始=原始透明度)
|
||||
sbAlpha.setProgress(mCurrentAlphaPercent);
|
||||
tvAlphaValue.setText(mCurrentAlphaPercent + "%");
|
||||
|
||||
// 5. 亮度控件(显示默认100%,初始化按钮状态)
|
||||
tvBrightnessValue.setText(mCurrentBrightnessPercent + "%");
|
||||
updateBrightnessBtnStatus(); // 禁用边界值按钮(初始100%,都可用)
|
||||
|
||||
LogUtils.d(TAG, "init data complete | 原始透明度:" + mOriginalAlphaPercent + "%");
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听初始化(新增透明度进度条监听)
|
||||
*/
|
||||
private void initListener() {
|
||||
// 点击监听(按钮+颜色拾取框)
|
||||
ivColorPicker.setOnClickListener(this);
|
||||
tvConfirm.setOnClickListener(this);
|
||||
tvCancel.setOnClickListener(this);
|
||||
tvBrightnessMinus.setOnClickListener(this);
|
||||
tvBrightnessPlus.setOnClickListener(this);
|
||||
// 透明度进度条监听
|
||||
sbAlpha.setOnSeekBarChangeListener(this);
|
||||
// 输入框监听(RGB+颜色值,避免循环同步)
|
||||
initTextWatcherListener();
|
||||
LogUtils.d(TAG, "all listener init complete | 监听绑定成功");
|
||||
}
|
||||
|
||||
private void initData() {
|
||||
ivColorPicker.setBackgroundColor(mInitialColor);
|
||||
int alpha = Color.alpha(mInitialColor);
|
||||
int r = Color.red(mInitialColor);
|
||||
int g = Color.green(mInitialColor);
|
||||
int b = Color.blue(mInitialColor);
|
||||
etR.setText(String.valueOf(r));
|
||||
etG.setText(String.valueOf(g));
|
||||
etB.setText(String.valueOf(b));
|
||||
etColorValue.setText(String.format("#%08X", mInitialColor));
|
||||
sbAlpha.setProgress(alpha);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对话框尺寸适配(小米全面屏+软键盘优化,避免输入框被遮挡)
|
||||
*/
|
||||
private void adjustDialogSize() {
|
||||
Window window = getWindow();
|
||||
if (window != null) {
|
||||
WindowManager.LayoutParams lp = window.getAttributes();
|
||||
// 宽度占屏幕80%,高度自适应(适配不同屏幕尺寸)
|
||||
lp.width = (int) (getContext().getResources().getDisplayMetrics().widthPixels * 0.8);
|
||||
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
|
||||
// 软键盘适配:小米虚拟导航栏兼容,避免输入框被遮挡
|
||||
window.setAttributes(lp);
|
||||
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
|
||||
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
|
||||
| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
|
||||
LogUtils.d(TAG, "dialog size adjust complete | 适配全面屏+软键盘");
|
||||
}
|
||||
}
|
||||
|
||||
// ====================== 监听子方法(细分类型,逻辑清晰) ======================
|
||||
/**
|
||||
* 输入框文本监听(RGB+颜色值,传入触发ID避免循环同步)
|
||||
*/
|
||||
private void initTextWatcherListener() {
|
||||
// RGB输入框监听(复用方法,减少冗余)
|
||||
setEditTextWatcher(etR, R.id.et_r);
|
||||
setEditTextWatcher(etG, R.id.et_g);
|
||||
setEditTextWatcher(etB, R.id.et_b);
|
||||
|
||||
// ====================== 颜色逻辑处理 ======================
|
||||
private void parseColorFromStr(String colorStr) {
|
||||
if (TextUtils.isEmpty(colorStr)) return;
|
||||
try {
|
||||
if (!colorStr.startsWith("#")) colorStr = "#" + colorStr;
|
||||
int color = Color.parseColor(colorStr);
|
||||
if (colorStr.length() == 7) { // #RRGGBB 补全透明度
|
||||
color = Color.argb(Color.alpha(mCurrentColor), Color.red(color), Color.green(color), Color.blue(color));
|
||||
}
|
||||
updateCurrentColor(color, true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
LogUtils.e(TAG, "颜色格式错误:" + colorStr, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateColorByRGB() {
|
||||
try {
|
||||
int r = parseInputValue(etR.getText().toString(), 0, MAX_PROGRESS);
|
||||
int g = parseInputValue(etG.getText().toString(), 0, MAX_PROGRESS);
|
||||
int b = parseInputValue(etB.getText().toString(), 0, MAX_PROGRESS);
|
||||
int alpha = Color.alpha(mCurrentColor);
|
||||
int newColor = Color.argb(alpha, r, g, b);
|
||||
updateCurrentColor(newColor, true);
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "RGB 更新失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateColorByAlpha(int alpha) {
|
||||
int r = Color.red(mCurrentColor);
|
||||
int g = Color.green(mCurrentColor);
|
||||
int b = Color.blue(mCurrentColor);
|
||||
updateCurrentColor(Color.argb(alpha, r, g, b), true);
|
||||
}
|
||||
|
||||
private void updateColorByBrightness(int brightness) {
|
||||
float factor = brightness / 128f;
|
||||
int alpha = Color.alpha(mCurrentColor);
|
||||
int r = adjustBrightness(Color.red(mCurrentColor), factor);
|
||||
int g = adjustBrightness(Color.green(mCurrentColor), factor);
|
||||
int b = adjustBrightness(Color.blue(mCurrentColor), factor);
|
||||
updateCurrentColor(Color.argb(alpha, r, g, b), false);
|
||||
}
|
||||
|
||||
private int adjustBrightness(int colorComponent, float factor) {
|
||||
int newComponent = (int) (colorComponent * factor);
|
||||
return Math.min(Math.max(newComponent, 0), MAX_PROGRESS);
|
||||
}
|
||||
|
||||
private void updateCurrentColor(int newColor, boolean updateBrightness) {
|
||||
mCurrentColor = newColor;
|
||||
// 更新拾取框
|
||||
ivColorPicker.setBackgroundColor(newColor);
|
||||
// 更新 RGB 输入框
|
||||
etR.setText(String.valueOf(Color.red(newColor)));
|
||||
etG.setText(String.valueOf(Color.green(newColor)));
|
||||
etB.setText(String.valueOf(Color.blue(newColor)));
|
||||
// 更新颜色值输入框
|
||||
etColorValue.setText(String.format("#%08X", newColor));
|
||||
// 更新透明度进度条(避免循环)
|
||||
sbAlpha.setOnSeekBarChangeListener(null);
|
||||
sbAlpha.setProgress(Color.alpha(newColor));
|
||||
sbAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
if (fromUser) updateColorByAlpha(progress);
|
||||
}
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {}
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {}
|
||||
});
|
||||
// 更新色阶进度条
|
||||
if (updateBrightness) {
|
||||
sbBrightness.setOnSeekBarChangeListener(null);
|
||||
sbBrightness.setProgress(128);
|
||||
sbBrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
if (fromUser) updateColorByBrightness(progress);
|
||||
}
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {}
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private int parseInputValue(String input, int min, int max) {
|
||||
if (TextUtils.isEmpty(input)) return min;
|
||||
try {
|
||||
int value = Integer.parseInt(input);
|
||||
return Math.min(Math.max(value, min), max);
|
||||
} catch (NumberFormatException e) {
|
||||
return min;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ====================== 控件监听 ======================
|
||||
private void setEditTextWatcher(EditText editText) {
|
||||
editText.addTextChangedListener(new TextWatcher() {
|
||||
// 颜色值输入框监听(支持#RRGGBB/#AARRGGBB格式)
|
||||
etColorValue.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
updateColorByRGB();
|
||||
// 关键:判断非应用自身更新,才执行解析(避免循环回调)
|
||||
if (!isAppSelfUpdatingColor) {
|
||||
parseColorFromStr(s.toString().trim(), R.id.et_color_value);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ====================== 透明度进度条监听实现(核心新增) ======================
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
int id = v.getId();
|
||||
if (id == R.id.iv_color_picker) {
|
||||
showSystemColorPicker();
|
||||
} else if (id == R.id.tv_confirm) {
|
||||
if (mListener != null) mListener.onColorSelected(mCurrentColor);
|
||||
dismiss();
|
||||
} else if (id == R.id.tv_cancel) {
|
||||
dismiss();
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
// 仅处理用户手动拖动进度条(避免应用自身更新时触发)
|
||||
if (fromUser && !isAppSelfUpdatingColor) {
|
||||
updateAlphaBySeekBar(progress);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {}
|
||||
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {}
|
||||
|
||||
/**
|
||||
* 拖动透明度进度条更新颜色(核心新增逻辑)
|
||||
*/
|
||||
private synchronized void updateAlphaBySeekBar(int alphaPercent) {
|
||||
if (!isAppSelfUpdatingColor) {
|
||||
isAppSelfUpdatingColor = true; // 标记为应用自身更新
|
||||
try {
|
||||
// 更新实时透明度(百分比+0-255值)
|
||||
mCurrentAlphaPercent = alphaPercent;
|
||||
mCurrentAlpha = percent2Alpha(alphaPercent);
|
||||
// 重新计算最终颜色(基于当前亮度+新透明度)
|
||||
calculateBrightnessAndUpdate();
|
||||
// 同步所有控件
|
||||
updateAllViews();
|
||||
LogUtils.d(TAG, "update alpha by seekbar | 透明度:" + mCurrentAlphaPercent + "%");
|
||||
} finally {
|
||||
// 直接释放标记,避免卡顿
|
||||
isAppSelfUpdatingColor = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ====================== 颜色核心逻辑(新增透明度参数,全功能兼容) ======================
|
||||
/**
|
||||
* 核心计算:基于原始RGB+当前亮度+当前透明度,计算实时RGB+最终颜色
|
||||
* 逻辑:亮度百分比→调节系数→原始RGB×系数→限制0-255→拼接透明度→最终颜色
|
||||
*/
|
||||
private void calculateBrightnessAndUpdate() {
|
||||
// 亮度百分比转调节系数(10%→0.1,100%→1.0,200%→2.0)
|
||||
float brightnessFactor = mCurrentBrightnessPercent / 100.0f;
|
||||
|
||||
// RGB三个分量同时调节(基于原始基准值,避免叠加失真),限制0-255
|
||||
mCurrentR = Math.min(Math.max(Math.round(mOriginalR * brightnessFactor), 0), MAX_RGB_VALUE);
|
||||
mCurrentG = Math.min(Math.max(Math.round(mOriginalG * brightnessFactor), 0), MAX_RGB_VALUE);
|
||||
mCurrentB = Math.min(Math.max(Math.round(mOriginalB * brightnessFactor), 0), MAX_RGB_VALUE);
|
||||
|
||||
// 拼接「实时透明度」+「实时RGB」,得到最终颜色(0xAARRGGBB)
|
||||
mCurrentColor = Color.argb(mCurrentAlpha, mCurrentR, mCurrentG, mCurrentB);
|
||||
}
|
||||
|
||||
/**
|
||||
* 亮度减少(每次减5%,最低10%,防止过暗)
|
||||
*/
|
||||
private void decreaseBrightness() {
|
||||
changeBrightness(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 亮度增加(每次加5%,最高200%,防止过曝)
|
||||
*/
|
||||
private void increaseBrightness() {
|
||||
changeBrightness(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 亮度调节核心方法(统一逻辑,加并发控制,同步所有控件)
|
||||
*/
|
||||
private synchronized void changeBrightness(boolean isIncrease) {
|
||||
// 关键:判断非应用自身更新,才执行调节(避免重复触发/循环)
|
||||
if (!isAppSelfUpdatingColor) {
|
||||
isAppSelfUpdatingColor = true; // 标记为应用自身更新
|
||||
try {
|
||||
if (isIncrease) {
|
||||
if (mCurrentBrightnessPercent >= MAX_BRIGHTNESS) return; // 达到最大值,不处理
|
||||
mCurrentBrightnessPercent += BRIGHTNESS_STEP; // 增加步长
|
||||
} else {
|
||||
if (mCurrentBrightnessPercent <= MIN_BRIGHTNESS) return; // 达到最小值,不处理
|
||||
mCurrentBrightnessPercent -= BRIGHTNESS_STEP; // 减少步长
|
||||
}
|
||||
// 计算亮度调节后的实时RGB+最终颜色(含当前透明度)
|
||||
calculateBrightnessAndUpdate();
|
||||
// 同步所有控件
|
||||
updateAllViews();
|
||||
LogUtils.d(TAG, (isIncrease ? "increase" : "decrease") + " brightness | "
|
||||
+ "亮度:" + mCurrentBrightnessPercent + "% | 实时RGB:" + mCurrentR + "," + mCurrentG + "," + mCurrentB);
|
||||
} finally {
|
||||
// 直接释放标记,避免卡顿
|
||||
isAppSelfUpdatingColor = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修正后:系统颜色选择器(无 API29+ 方法,全版本兼容)
|
||||
* 解析颜色字符串(支持#RRGGBB/#AARRGGBB,容错处理,更新原始基准值+实时值)
|
||||
* 新增:解析颜色的透明度,同步更新透明度进度条
|
||||
*/
|
||||
private void showSystemColorPicker() {
|
||||
final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getContext());
|
||||
builder.setTitle("选择颜色");
|
||||
final int[] systemColors = {
|
||||
0xFFFFFFFF, 0xFF000000, 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
|
||||
0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, 0xFF888888, 0xFFAAAAAA
|
||||
};
|
||||
// 横向布局存放颜色按钮
|
||||
LinearLayout colorView = new LinearLayout(getContext());
|
||||
colorView.setOrientation(LinearLayout.HORIZONTAL);
|
||||
colorView.setGravity(Gravity.CENTER);
|
||||
colorView.setPadding(dp2px(10), dp2px(10), dp2px(10), dp2px(10));
|
||||
// 循环添加颜色按钮(用 LayoutParams 设置边距)
|
||||
for (int i = 0; i < systemColors.length; i++) {
|
||||
ImageView iv = new ImageView(getContext());
|
||||
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
|
||||
dp2px(40), dp2px(40)
|
||||
);
|
||||
// 非最后一个按钮设置右侧边距 10dp
|
||||
if (i != systemColors.length - 1) {
|
||||
lp.setMargins(0, 0, dp2px(10), 0);
|
||||
private void parseColorFromStr(String colorStr, int triggerViewId) {
|
||||
// 关键:判断非应用自身更新,才执行解析(避免循环回调)
|
||||
if (!isAppSelfUpdatingColor) {
|
||||
isAppSelfUpdatingColor = true; // 标记为应用自身更新
|
||||
try {
|
||||
if (TextUtils.isEmpty(colorStr)) return;
|
||||
|
||||
// 补全#前缀(兼容用户输入习惯,如直接输AARRGGBB)
|
||||
if (!colorStr.startsWith("#")) {
|
||||
colorStr = "#" + colorStr;
|
||||
}
|
||||
iv.setLayoutParams(lp);
|
||||
iv.setBackgroundColor(systemColors[i]);
|
||||
iv.setClickable(true);
|
||||
iv.setFocusable(true);
|
||||
// 点击选择颜色
|
||||
final int finalI = i;
|
||||
iv.setOnClickListener(new View.OnClickListener() {
|
||||
|
||||
// 格式校验(仅支持6位RRGGBB/8位AARRGGBB,避免非法格式)
|
||||
if (colorStr.length() != 7 && colorStr.length() != 9) {
|
||||
LogUtils.e(TAG, "parse color failed | 格式错误(需#RRGGBB/#AARRGGBB),输入:" + colorStr);
|
||||
return;
|
||||
}
|
||||
|
||||
// 解析颜色(系统API,安全可靠)
|
||||
int parsedColor = Color.parseColor(colorStr);
|
||||
|
||||
// 更新原始基准值(用户输入颜色,重置基准)
|
||||
// 透明度:解析颜色的alpha(0-255)转百分比(0-100%)
|
||||
mOriginalAlpha = Color.alpha(parsedColor);
|
||||
mOriginalAlphaPercent = alpha2Percent(mOriginalAlpha);
|
||||
// RGB:解析颜色的RGB分量
|
||||
mOriginalR = Color.red(parsedColor);
|
||||
mOriginalG = Color.green(parsedColor);
|
||||
mOriginalB = Color.blue(parsedColor);
|
||||
// 更新实时值(原始值=实时值,无调节)
|
||||
mCurrentAlpha = mOriginalAlpha;
|
||||
mCurrentAlphaPercent = mOriginalAlphaPercent;
|
||||
mCurrentR = mOriginalR;
|
||||
mCurrentG = mOriginalG;
|
||||
mCurrentB = mOriginalB;
|
||||
// 重置亮度为100%
|
||||
mCurrentBrightnessPercent = DEFAULT_BRIGHTNESS;
|
||||
mCurrentColor = parsedColor;
|
||||
|
||||
// 同步所有控件
|
||||
updateAllViews();
|
||||
LogUtils.d(TAG, "parse color success | 解析颜色:" + String.format("#%08X", parsedColor)
|
||||
+ " | 透明度:" + mCurrentAlphaPercent + "% | 重置亮度:" + DEFAULT_BRIGHTNESS + "%");
|
||||
} catch (IllegalArgumentException e) {
|
||||
LogUtils.e(TAG, "parse color failed | 非法颜色格式,输入:" + colorStr, e);
|
||||
} finally {
|
||||
// 直接释放标记,避免卡顿
|
||||
isAppSelfUpdatingColor = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过RGB输入框更新颜色(用户输入后,更新原始基准值+实时值,重置亮度为100%)
|
||||
* 新增:透明度基准值保持不变,仅更新RGB
|
||||
*/
|
||||
private synchronized void updateColorByRGB(int triggerViewId) {
|
||||
// 关键:判断非应用自身更新,才执行更新(避免循环回调)
|
||||
if (!isAppSelfUpdatingColor) {
|
||||
isAppSelfUpdatingColor = true; // 标记为应用自身更新
|
||||
try {
|
||||
// 解析用户输入的RGB值(限制0-255,非法输入设为0)
|
||||
int inputR = parseInputValue(etR.getText().toString());
|
||||
int inputG = parseInputValue(etG.getText().toString());
|
||||
int inputB = parseInputValue(etB.getText().toString());
|
||||
|
||||
// 更新原始基准值(用户手动输入,作为新的调节基准)
|
||||
mOriginalR = inputR;
|
||||
mOriginalG = inputG;
|
||||
mOriginalB = inputB;
|
||||
// 更新实时值(输入值=实时值,无亮度调节)
|
||||
mCurrentR = inputR;
|
||||
mCurrentG = inputG;
|
||||
mCurrentB = inputB;
|
||||
// 重置亮度为100%(透明度保持当前值不变)
|
||||
mCurrentBrightnessPercent = DEFAULT_BRIGHTNESS;
|
||||
// 计算最终颜色(无亮度调节,拼接当前透明度)
|
||||
mCurrentColor = Color.argb(mCurrentAlpha, mCurrentR, mCurrentG, mCurrentB);
|
||||
|
||||
// 同步所有控件
|
||||
updateAllViews();
|
||||
LogUtils.d(TAG, "update color by RGB | 新原始RGB:" + mOriginalR + "," + mOriginalG + "," + mOriginalB
|
||||
+ " | 透明度:" + mCurrentAlphaPercent + "% | 重置亮度:" + DEFAULT_BRIGHTNESS + "%");
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "update color by RGB failed", e);
|
||||
} finally {
|
||||
// 直接释放标记,避免卡顿
|
||||
isAppSelfUpdatingColor = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 核心同步:更新所有控件显示(新增透明度控件同步,统一方法)
|
||||
*/
|
||||
private void updateAllViews() {
|
||||
// 1. 同步颜色预览(显示最终颜色,含透明度+亮度)
|
||||
ivColorPicker.setBackgroundColor(mCurrentColor);
|
||||
|
||||
// 2. 同步RGB输入框(显示实时调节值)
|
||||
etR.setText(String.valueOf(mCurrentR));
|
||||
etG.setText(String.valueOf(mCurrentG));
|
||||
etB.setText(String.valueOf(mCurrentB));
|
||||
|
||||
// 3. 同步颜色值输入框(显示最终颜色,含透明度,格式#AARRGGBB)
|
||||
etColorValue.setText(String.format("#%08X", mCurrentColor));
|
||||
|
||||
// 4. 同步透明度控件(进度条+文本,显示实时透明度)
|
||||
sbAlpha.setProgress(mCurrentAlphaPercent);
|
||||
tvAlphaValue.setText(mCurrentAlphaPercent + "%");
|
||||
|
||||
// 5. 同步亮度控件(数值+按钮状态)
|
||||
tvBrightnessValue.setText(mCurrentBrightnessPercent + "%");
|
||||
updateBrightnessBtnStatus();
|
||||
|
||||
LogUtils.d(TAG, "sync all views complete | 最终颜色:" + String.format("#%08X", mCurrentColor)
|
||||
+ " | 实时RGB:" + mCurrentR + "," + mCurrentG + "," + mCurrentB
|
||||
+ " | 透明度:" + mCurrentAlphaPercent + "% | 亮度:" + mCurrentBrightnessPercent + "%");
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新亮度按钮状态(边界值禁用,提升交互体验)
|
||||
*/
|
||||
private void updateBrightnessBtnStatus() {
|
||||
// 亮度≤10%:禁用减号(文字变浅灰);≥200%:禁用加号(文字变浅灰)
|
||||
boolean canMinus = mCurrentBrightnessPercent > MIN_BRIGHTNESS;
|
||||
boolean canPlus = mCurrentBrightnessPercent < MAX_BRIGHTNESS;
|
||||
|
||||
tvBrightnessMinus.setEnabled(canMinus);
|
||||
tvBrightnessPlus.setEnabled(canPlus);
|
||||
tvBrightnessMinus.setTextColor(canMinus ? Color.BLACK : Color.parseColor("#CCCCCC"));
|
||||
tvBrightnessPlus.setTextColor(canPlus ? Color.BLACK : Color.parseColor("#CCCCCC"));
|
||||
}
|
||||
|
||||
// ====================== 工具方法(新增透明度转换工具,通用复用) ======================
|
||||
/**
|
||||
* 透明度:0-255 → 0-100%(颜色计算值转用户直观百分比)
|
||||
*/
|
||||
private int alpha2Percent(int alpha) {
|
||||
return Math.round((float) alpha / MAX_RGB_VALUE * MAX_ALPHA_PERCENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 透明度:0-100% → 0-255(用户操作百分比转颜色计算值)
|
||||
*/
|
||||
private int percent2Alpha(int percent) {
|
||||
return Math.round((float) percent / MAX_ALPHA_PERCENT * MAX_RGB_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析输入值(限制0-255,非法输入返回0,容错处理)
|
||||
*/
|
||||
private int parseInputValue(String input) {
|
||||
if (TextUtils.isEmpty(input)) return 0;
|
||||
try {
|
||||
int value = Integer.parseInt(input);
|
||||
return Math.min(Math.max(value, 0), MAX_RGB_VALUE); // 限制范围,避免溢出
|
||||
} catch (NumberFormatException e) {
|
||||
LogUtils.e(TAG, "parse input failed | 非法数字,输入:" + input, e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* RGB输入框监听复用(减少冗余代码,统一逻辑)
|
||||
*/
|
||||
private void setEditTextWatcher(EditText editText, final int viewId) {
|
||||
editText.addTextChangedListener(new TextWatcher() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
int selectedColor = systemColors[finalI];
|
||||
int newColor = Color.argb(
|
||||
Color.alpha(mCurrentColor),
|
||||
Color.red(selectedColor),
|
||||
Color.green(selectedColor),
|
||||
Color.blue(selectedColor)
|
||||
);
|
||||
updateCurrentColor(newColor, true);
|
||||
builder.create().dismiss();
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
// 关键:判断非应用自身更新,才执行更新(避免循环回调)
|
||||
if (!isAppSelfUpdatingColor) {
|
||||
updateColorByRGB(viewId); // 输入变化后更新颜色
|
||||
}
|
||||
}
|
||||
});
|
||||
colorView.addView(iv);
|
||||
}
|
||||
builder.setView(colorView)
|
||||
.setNegativeButton("取消", null)
|
||||
.show();
|
||||
}
|
||||
|
||||
|
||||
// ====================== 工具方法 ======================
|
||||
/**
|
||||
* dp转px(适配小米不同分辨率,避免尺寸错乱,通用工具)
|
||||
*/
|
||||
private int dp2px(float dp) {
|
||||
return (int) (dp * getContext().getResources().getDisplayMetrics().density + 0.5f);
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示系统颜色选择器(兼容API29-30,无高版本依赖,小米机型适配)
|
||||
* 核心调整:新增「水平滚动容器+颜色排列容器」二级结构,内置圆形按钮,无额外drawable依赖
|
||||
*/
|
||||
private void showSystemColorPicker() {
|
||||
LogUtils.d(TAG, "show system color picker | 兼容小米API29-30,支持横向滚动");
|
||||
final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getContext());
|
||||
builder.setTitle("选择基础颜色");
|
||||
|
||||
// ====================== 内存优化 ======================
|
||||
// 50种常用颜色:按「红→橙→黄→绿→青→蓝→紫→粉→棕→灰→黑白」彩虹光谱顺序排列
|
||||
final int[] systemColors = {
|
||||
// 红色系(6种:深红→大红→浅红→玫红→暗红→橘红)
|
||||
0xFFCC0000, 0xFFFF0000, 0xFFFF6666, 0xFFFF1493, 0xFF8B0000, 0xFFFF4500,
|
||||
// 橙色系(5种:深橙→橙→浅橙→橙黄→橘橙)
|
||||
0xFFCC6600, 0xFFFF8800, 0xFFFFAA33, 0xFFFFBB00, 0xFFF5A623,
|
||||
// 黄色系(5种:深黄→黄→浅黄→鹅黄→金黄)
|
||||
0xFFCCCC00, 0xFFFFFF00, 0xFFFFEE99, 0xFFFFFACD, 0xFFFFD700,
|
||||
// 绿色系(7种:深绿→绿→浅绿→草绿→薄荷绿→翠绿→墨绿)
|
||||
0xFF006600, 0xFF00FF00, 0xFF99FF99, 0xFF66CC66, 0xFF98FB98, 0xFF00FF99, 0xFF003300,
|
||||
// 青色系(5种:深青→青→浅青→蓝绿→青绿)
|
||||
0xFF006666, 0xFF00FFFF, 0xFF99FFFF, 0xFF00CCCC, 0xFF40E0D0,
|
||||
// 蓝色系(8种:深蓝→藏蓝→蓝→浅蓝→天蓝→宝蓝→湖蓝→靛蓝)
|
||||
0xFF0000CC, 0xFF00008B, 0xFF0000FF, 0xFF6666FF, 0xFF87CEEB, 0xFF0066FF, 0xFF0099FF, 0xFF4B0082,
|
||||
// 紫色系(6种:深紫→紫→浅紫→紫罗兰→紫红→蓝紫)
|
||||
0xFF660099, 0xFF8800FF, 0xFFAA99FF, 0xFF9370DB, 0xFFCBC3E3, 0xFF8A2BE2,
|
||||
// 粉色系(5种:深粉→粉→浅粉→嫩粉→桃粉)
|
||||
0xFFFF00FF, 0xFFFF99CC, 0xFFFFCCDD, 0xFFFFB6C1, 0xFFFFA5A5,
|
||||
// 棕色系(4种:深棕→棕→浅棕→棕黄)
|
||||
0xFF8B4513, 0xFFA0522D, 0xFFD2B48C, 0xFFCD853F,
|
||||
// 灰色系(6种:深灰→灰→浅灰→银灰→淡灰→浅银灰)
|
||||
0xFF333333, 0xFF666666, 0xFF888888, 0xFFAAAAAA, 0xFFCCCCCC, 0xFFE6E6E6,
|
||||
// 黑白系(3种:黑→白→米白)
|
||||
0xFF000000, 0xFFFFFFFF, 0xFFFFFAFA
|
||||
};
|
||||
|
||||
// 1. 第一级:水平滚动容器
|
||||
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(getContext());
|
||||
horizontalScrollView.setHorizontalScrollBarEnabled(true);
|
||||
horizontalScrollView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
|
||||
horizontalScrollView.setPadding(dp2px(5), dp2px(5), dp2px(5), dp2px(5));
|
||||
|
||||
// 2. 第二级:颜色排列容器(横向)
|
||||
LinearLayout colorLayout = new LinearLayout(getContext());
|
||||
colorLayout.setOrientation(LinearLayout.HORIZONTAL);
|
||||
colorLayout.setGravity(Gravity.CENTER_VERTICAL);
|
||||
colorLayout.setPadding(dp2px(10), dp2px(10), dp2px(10), dp2px(10));
|
||||
|
||||
// 3. 循环添加颜色按钮(内置圆形效果,无额外依赖)
|
||||
for (int i = 0; i < systemColors.length; i++) {
|
||||
final int color = systemColors[i];
|
||||
ImageView colorBtn = new ImageView(getContext());
|
||||
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(dp2px(40), dp2px(40));
|
||||
if (i != systemColors.length - 1) {
|
||||
lp.setMargins(0, 0, dp2px(10), 0); // 按钮间距
|
||||
}
|
||||
colorBtn.setLayoutParams(lp);
|
||||
|
||||
// 核心:内置圆形背景(白色边框+圆形形状,无需drawable文件)
|
||||
GradientDrawable circleBg = new GradientDrawable();
|
||||
circleBg.setShape(GradientDrawable.OVAL); // 圆形
|
||||
circleBg.setColor(color); // 按钮颜色
|
||||
circleBg.setStroke(dp2px(2), Color.WHITE); // 白色边框(2dp宽,区分颜色)
|
||||
colorBtn.setBackground(circleBg); // 设置圆形背景
|
||||
|
||||
colorBtn.setClickable(true);
|
||||
colorBtn.setFocusable(true);
|
||||
|
||||
// 点击事件(逻辑不变)
|
||||
colorBtn.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void dismiss() {
|
||||
super.dismiss();
|
||||
mListener = null;
|
||||
public void onClick(View v) {
|
||||
if (!isAppSelfUpdatingColor) {
|
||||
isAppSelfUpdatingColor = true;
|
||||
try {
|
||||
mOriginalAlpha = Color.alpha(color);
|
||||
mOriginalAlphaPercent = alpha2Percent(mOriginalAlpha);
|
||||
mOriginalR = Color.red(color);
|
||||
mOriginalG = Color.green(color);
|
||||
mOriginalB = Color.blue(color);
|
||||
mCurrentAlpha = mOriginalAlpha;
|
||||
mCurrentAlphaPercent = mOriginalAlphaPercent;
|
||||
mCurrentR = mOriginalR;
|
||||
mCurrentG = mOriginalG;
|
||||
mCurrentB = mOriginalB;
|
||||
mCurrentBrightnessPercent = DEFAULT_BRIGHTNESS;
|
||||
mCurrentColor = color;
|
||||
updateAllViews();
|
||||
builder.create().dismiss();
|
||||
LogUtils.d(TAG, "select system color | 选择颜色:" + String.format("#%08X", color)
|
||||
+ " | 透明度:" + mCurrentAlphaPercent + "%");
|
||||
} finally {
|
||||
isAppSelfUpdatingColor = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
colorLayout.addView(colorBtn);
|
||||
}
|
||||
|
||||
// 层级嵌套(滚动容器→颜色容器)
|
||||
horizontalScrollView.addView(colorLayout);
|
||||
builder.setView(horizontalScrollView).setNegativeButton("关闭", null).show();
|
||||
}
|
||||
|
||||
// ====================== 点击事件实现(统一处理,逻辑清晰) ======================
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
int id = v.getId();
|
||||
// 关键:所有点击事件均加判断(避免并发冲突/重复触发)
|
||||
if (!isAppSelfUpdatingColor) {
|
||||
if (id == R.id.iv_color_picker) {
|
||||
showSystemColorPicker(); // 打开系统颜色选择器
|
||||
} else if (id == R.id.tv_confirm) {
|
||||
mListener.onColorSelected(mCurrentColor); // 确认选择,回调颜色
|
||||
LogUtils.d(TAG, "confirm color | 回调颜色:" + String.format("#%08X", mCurrentColor));
|
||||
dismiss();
|
||||
} else if (id == R.id.tv_cancel) {
|
||||
dismiss(); // 取消,关闭对话框
|
||||
LogUtils.d(TAG, "cancel color | 取消选择,关闭对话框");
|
||||
} else if (id == R.id.tv_brightness_minus) {
|
||||
decreaseBrightness(); // 减少亮度
|
||||
} else if (id == R.id.tv_brightness_plus) {
|
||||
increaseBrightness(); // 增加亮度
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
20
powerbell/src/main/res/drawable/btn_brightness_bg.xml
Normal file
20
powerbell/src/main/res/drawable/btn_brightness_bg.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<!-- 按压状态:浅灰色背景 -->
|
||||
<item android:state_pressed="true">
|
||||
<shape android:shape="rectangle">
|
||||
<solid android:color="#E0E0E0" /> <!-- 按压深色 -->
|
||||
<corners android:radius="8dp" /> <!-- 圆角适配小米UI风格 -->
|
||||
<stroke android:width="1dp" android:color="#CCCCCC" /> <!-- 边框 -->
|
||||
</shape>
|
||||
</item>
|
||||
<!-- 正常状态:白色背景 -->
|
||||
<item>
|
||||
<shape android:shape="rectangle">
|
||||
<solid android:color="#FFFFFF" /> <!-- 正常白色 -->
|
||||
<corners android:radius="8dp" />
|
||||
<stroke android:width="1dp" android:color="#CCCCCC" />
|
||||
</shape>
|
||||
</item>
|
||||
</selector>
|
||||
|
||||
18
powerbell/src/main/res/drawable/btn_cancel_bg.xml
Normal file
18
powerbell/src/main/res/drawable/btn_cancel_bg.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<!-- 按压状态:深灰色背景 -->
|
||||
<item android:state_pressed="true">
|
||||
<shape android:shape="rectangle">
|
||||
<solid android:color="#D0D0D0" />
|
||||
<corners android:radius="8dp" /> <!-- 与亮度按钮圆角一致,统一风格 -->
|
||||
</shape>
|
||||
</item>
|
||||
<!-- 正常状态:浅灰色背景 -->
|
||||
<item>
|
||||
<shape android:shape="rectangle">
|
||||
<solid android:color="#F0F0F0" />
|
||||
<corners android:radius="8dp" />
|
||||
</shape>
|
||||
</item>
|
||||
</selector>
|
||||
|
||||
20
powerbell/src/main/res/drawable/btn_common.xml
Normal file
20
powerbell/src/main/res/drawable/btn_common.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<!-- 按压状态:深灰色 -->
|
||||
<item android:state_pressed="true">
|
||||
<shape android:shape="rectangle">
|
||||
<solid android:color="#CCCCCC" /> <!-- 按压时颜色 -->
|
||||
<corners android:radius="8dp" /> <!-- 圆角(可按需调整) -->
|
||||
<stroke android:width="1dp" android:color="#EEEEEE" /> <!-- 边框(可选,不加可删除) -->
|
||||
</shape>
|
||||
</item>
|
||||
<!-- 正常状态:浅灰色 -->
|
||||
<item>
|
||||
<shape android:shape="rectangle">
|
||||
<solid android:color="#F5F5F5" /> <!-- 正常时颜色 -->
|
||||
<corners android:radius="8dp" /> <!-- 圆角(和按压状态一致) -->
|
||||
<stroke android:width="1dp" android:color="#EEEEEE" /> <!-- 边框(可选) -->
|
||||
</shape>
|
||||
</item>
|
||||
</selector>
|
||||
|
||||
18
powerbell/src/main/res/drawable/btn_confirm_bg.xml
Normal file
18
powerbell/src/main/res/drawable/btn_confirm_bg.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<!-- 按压状态:加深深色背景 -->
|
||||
<item android:state_pressed="true">
|
||||
<shape android:shape="rectangle">
|
||||
<solid android:color="#2D7CFF" /> <!-- 按压深蓝 -->
|
||||
<corners android:radius="8dp" />
|
||||
</shape>
|
||||
</item>
|
||||
<!-- 正常状态:主色背景(可改为项目主题色) -->
|
||||
<item>
|
||||
<shape android:shape="rectangle">
|
||||
<solid android:color="#4096FF" /> <!-- 正常浅蓝,适配小米系统UI -->
|
||||
<corners android:radius="8dp" />
|
||||
</shape>
|
||||
</item>
|
||||
</selector>
|
||||
|
||||
@@ -1,50 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="20dp"
|
||||
android:background="@drawable/dialog_bg_radius">
|
||||
|
||||
<!-- 标题 -->
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="调色板"
|
||||
android:textSize="18sp"
|
||||
android:textColor="@color/black"
|
||||
android:textStyle="bold"
|
||||
android:layout_marginBottom="20dp"/>
|
||||
|
||||
<!-- 颜色拾取框 -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:layout_marginBottom="15dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="颜色拾取:"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/gray_700"
|
||||
android:layout_marginRight="10dp"/>
|
||||
android:background="#FFFFFFFF">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/iv_color_picker"
|
||||
android:layout_width="50dp"
|
||||
android:layout_height="50dp"
|
||||
android:background="@color/white"
|
||||
android:scaleType="fitXY"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:background="#FF0000"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:padding="2dp"
|
||||
android:backgroundTint="@color/gray_200"/>
|
||||
</LinearLayout>
|
||||
android:focusable="true"/>
|
||||
|
||||
<!-- RGB 输入框 -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
@@ -56,151 +28,156 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="RGB:"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/gray_700"
|
||||
android:layout_marginRight="10dp"/>
|
||||
android:textSize="16sp"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/et_r"
|
||||
android:layout_width="60dp"
|
||||
android:layout_height="40dp"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:hint="R"
|
||||
android:inputType="number"
|
||||
android:maxLength="3"
|
||||
android:gravity="center"
|
||||
android:textSize="14sp"
|
||||
android:background="@drawable/edittext_bg"
|
||||
android:padding="5dp"
|
||||
android:layout_marginRight="10dp"/>
|
||||
android:maxLength="3"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/et_g"
|
||||
android:layout_width="60dp"
|
||||
android:layout_height="40dp"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:hint="G"
|
||||
android:inputType="number"
|
||||
android:maxLength="3"
|
||||
android:gravity="center"
|
||||
android:textSize="14sp"
|
||||
android:background="@drawable/edittext_bg"
|
||||
android:padding="5dp"
|
||||
android:layout_marginRight="10dp"/>
|
||||
android:maxLength="3"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/et_b"
|
||||
android:layout_width="60dp"
|
||||
android:layout_height="40dp"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:hint="B"
|
||||
android:inputType="number"
|
||||
android:maxLength="3"
|
||||
android:gravity="center"
|
||||
android:textSize="14sp"
|
||||
android:background="@drawable/edittext_bg"
|
||||
android:padding="5dp"/>
|
||||
android:maxLength="3"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- 颜色值输入框 -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:layout_marginBottom="15dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="颜色值:"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/gray_700"
|
||||
android:layout_marginRight="10dp"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/et_color_value"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="40dp"
|
||||
android:hint="#AARRGGBB"
|
||||
android:inputType="text"
|
||||
android:maxLength="9"
|
||||
android:gravity="center"
|
||||
android:textSize="14sp"
|
||||
android:background="@drawable/edittext_bg"
|
||||
android:padding="5dp"/>
|
||||
</LinearLayout>
|
||||
android:maxLength="9"
|
||||
android:layout_marginBottom="15dp"/>
|
||||
|
||||
<!-- 透明度进度条 -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_marginBottom="15dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:layout_marginBottom="5dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="透明度(0-255)"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/gray_700"
|
||||
android:layout_marginBottom="5dp"/>
|
||||
android:text="透明度:"
|
||||
android:textSize="16sp"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_alpha_value"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="100%"
|
||||
android:textSize="16sp"
|
||||
android:layout_marginLeft="10dp"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<SeekBar
|
||||
android:id="@+id/sb_alpha"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:progressDrawable="@drawable/seekbar_progress"
|
||||
android:thumb="@drawable/seekbar_thumb"/>
|
||||
android:max="100"
|
||||
android:progress="100"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- 色阶进度条 -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_marginBottom="20dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="色阶(亮度)"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/gray_700"
|
||||
android:layout_marginBottom="5dp"/>
|
||||
|
||||
<SeekBar
|
||||
android:id="@+id/sb_brightness"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:progressDrawable="@drawable/seekbar_progress"
|
||||
android:thumb="@drawable/seekbar_thumb"/>
|
||||
</LinearLayout>
|
||||
|
||||
<!-- 按钮区域 -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="end">
|
||||
android:gravity="center_horizontal"
|
||||
android:layout_marginBottom="20dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_brightness_minus"
|
||||
android:layout_width="40dp"
|
||||
android:layout_height="40dp"
|
||||
android:text="-"
|
||||
android:textSize="20sp"
|
||||
android:gravity="center"
|
||||
android:background="@drawable/btn_common"
|
||||
android:clickable="true"
|
||||
android:focusable="true"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_brightness_value"
|
||||
android:layout_width="80dp"
|
||||
android:layout_height="40dp"
|
||||
android:text="100%"
|
||||
android:textSize="16sp"
|
||||
android:gravity="center"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_brightness_plus"
|
||||
android:layout_width="40dp"
|
||||
android:layout_height="40dp"
|
||||
android:text="+"
|
||||
android:textSize="20sp"
|
||||
android:gravity="center"
|
||||
android:background="@drawable/btn_common"
|
||||
android:clickable="true"
|
||||
android:focusable="true"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_horizontal">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_cancel"
|
||||
android:layout_width="80dp"
|
||||
android:layout_height="40dp"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="45dp"
|
||||
android:text="取消"
|
||||
android:textSize="16sp"
|
||||
android:gravity="center"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/gray_700"
|
||||
android:background="@drawable/btn_bg_gray"
|
||||
android:layout_marginRight="10dp"/>
|
||||
android:background="@drawable/btn_common"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:layout_marginRight="20dp"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_confirm"
|
||||
android:layout_width="80dp"
|
||||
android:layout_height="40dp"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="45dp"
|
||||
android:text="确认"
|
||||
android:textSize="16sp"
|
||||
android:gravity="center"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/white"
|
||||
android:background="@drawable/btn_bg_primary"/>
|
||||
android:background="@drawable/btn_common"
|
||||
android:clickable="true"
|
||||
android:focusable="true"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
Reference in New Issue
Block a user