From 6cf5ac20349ddf5334bb6c9a9206c6c74ab322b2 Mon Sep 17 00:00:00 2001 From: LaizyBoy Date: Sat, 2 May 2026 10:48:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0CropBackgroundUtils?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=E5=8F=8A=E5=B0=81=E9=9D=A2=E5=89=AA?= =?UTF-8?q?=E8=A3=81=E8=83=8C=E6=99=AF=E4=BF=AE=E6=94=B9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建CropBackgroundUtils工具类,实现与BackgroundUtils类似的背景管理功能 - 在封面剪裁窗口(CropActivity)添加"修改剪裁背景颜色"按钮 - 按钮点击后弹出Toast提示信息 - 使用独立的偏好设置存储(crop_background_prefs) --- .../winboll/studio/gallery/CropActivity.java | 7 + .../gallery/utils/CropBackgroundUtils.java | 124 ++++++++++++++++++ gallery/src/main/res/layout/activity_crop.xml | 9 ++ 3 files changed, 140 insertions(+) create mode 100644 gallery/src/main/java/cc/winboll/studio/gallery/utils/CropBackgroundUtils.java diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/CropActivity.java b/gallery/src/main/java/cc/winboll/studio/gallery/CropActivity.java index a54aefc..7463937 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/CropActivity.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/CropActivity.java @@ -87,6 +87,13 @@ public class CropActivity extends AppCompatActivity { } }); + findViewById(R.id.btn_change_bg).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(CropActivity.this, "修改剪裁背景颜色", Toast.LENGTH_SHORT).show(); + } + }); + zoomContainer = findViewById(R.id.zoom_container); SeekBar seekBarZoom = findViewById(R.id.seekbar_zoom); diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/utils/CropBackgroundUtils.java b/gallery/src/main/java/cc/winboll/studio/gallery/utils/CropBackgroundUtils.java new file mode 100644 index 0000000..fb399f9 --- /dev/null +++ b/gallery/src/main/java/cc/winboll/studio/gallery/utils/CropBackgroundUtils.java @@ -0,0 +1,124 @@ +package cc.winboll.studio.gallery.utils; + +import android.content.Context; +import android.content.SharedPreferences; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; +import androidx.annotation.ColorInt; +import androidx.annotation.DrawableRes; +import androidx.core.content.ContextCompat; + +public class CropBackgroundUtils { + + public enum DrawableType { + RESOURCE_ID, + COLOR + } + + private static volatile CropBackgroundUtils instance; + + private static final String PREF_NAME = "crop_background_prefs"; + private static final String KEY_TYPE = "crop_bg_type"; + private static final String KEY_RES_ID = "crop_bg_res_id"; + private static final String KEY_COLOR = "crop_bg_color"; + + private Context context; + private Drawable drawable; + private DrawableType drawableType; + private int resId; + private int color; + + private CropBackgroundUtils() { + } + + public static CropBackgroundUtils getInstance() { + if (instance == null) { + synchronized (CropBackgroundUtils.class) { + if (instance == null) { + instance = new CropBackgroundUtils(); + } + } + } + return instance; + } + + public static CropBackgroundUtils initFromResource(Context context, @DrawableRes int resId) { + synchronized (CropBackgroundUtils.class) { + CropBackgroundUtils utils = getInstance(); + utils.context = context.getApplicationContext(); + utils.drawableType = DrawableType.RESOURCE_ID; + utils.resId = resId; + utils.drawable = ContextCompat.getDrawable(utils.context, resId); + utils.saveToPreferences(); + return utils; + } + } + + public static CropBackgroundUtils initFromColor(Context context, @ColorInt int color) { + synchronized (CropBackgroundUtils.class) { + CropBackgroundUtils utils = getInstance(); + utils.context = context.getApplicationContext(); + utils.drawableType = DrawableType.COLOR; + utils.color = color; + utils.drawable = new ColorDrawable(color); + utils.saveToPreferences(); + return utils; + } + } + + public static CropBackgroundUtils initFromPreferences(Context context) { + synchronized (CropBackgroundUtils.class) { + Context appContext = context.getApplicationContext(); + SharedPreferences prefs = appContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); + int type = prefs.getInt(KEY_TYPE, -1); + if (type == 0) { + int resId = prefs.getInt(KEY_RES_ID, 0); + if (resId != 0) { + return initFromResource(appContext, resId); + } + } else if (type == 1) { + int color = prefs.getInt(KEY_COLOR, Color.BLACK); + return initFromColor(appContext, color); + } + return initFromColor(appContext, 0xFF00FF00); + } + } + + public Drawable getDrawable() { + return drawable; + } + + public DrawableType getDrawableType() { + return drawableType; + } + + public int getResId() { + return resId; + } + + public int getColor() { + return color; + } + + public void saveToPreferences() { + if (context == null) return; + SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = prefs.edit(); + if (drawableType == DrawableType.RESOURCE_ID) { + editor.putInt(KEY_TYPE, 0); + editor.putInt(KEY_RES_ID, resId); + editor.remove(KEY_COLOR); + } else { + editor.putInt(KEY_TYPE, 1); + editor.putInt(KEY_COLOR, color); + editor.remove(KEY_RES_ID); + } + editor.apply(); + } + + public static void clearPreferences(Context context) { + SharedPreferences prefs = context.getApplicationContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); + prefs.edit().clear().apply(); + } +} diff --git a/gallery/src/main/res/layout/activity_crop.xml b/gallery/src/main/res/layout/activity_crop.xml index ddec4e4..39e528e 100644 --- a/gallery/src/main/res/layout/activity_crop.xml +++ b/gallery/src/main/res/layout/activity_crop.xml @@ -46,6 +46,15 @@ android:src="@drawable/ic_info" android:background="?attr/selectableItemBackgroundBorderless"/> + +