feat: 添加CropBackgroundUtils工具类及封面剪裁背景修改功能
- 创建CropBackgroundUtils工具类,实现与BackgroundUtils类似的背景管理功能 - 在封面剪裁窗口(CropActivity)添加"修改剪裁背景颜色"按钮 - 按钮点击后弹出Toast提示信息 - 使用独立的偏好设置存储(crop_background_prefs)
This commit is contained in:
@@ -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);
|
zoomContainer = findViewById(R.id.zoom_container);
|
||||||
|
|
||||||
SeekBar seekBarZoom = findViewById(R.id.seekbar_zoom);
|
SeekBar seekBarZoom = findViewById(R.id.seekbar_zoom);
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -46,6 +46,15 @@
|
|||||||
android:src="@drawable/ic_info"
|
android:src="@drawable/ic_info"
|
||||||
android:background="?attr/selectableItemBackgroundBorderless"/>
|
android:background="?attr/selectableItemBackgroundBorderless"/>
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/btn_change_bg"
|
||||||
|
android:layout_width="40dp"
|
||||||
|
android:layout_height="40dp"
|
||||||
|
android:padding="5dp"
|
||||||
|
android:src="@drawable/ic_color_pick"
|
||||||
|
android:background="?attr/selectableItemBackgroundBorderless"
|
||||||
|
android:contentDescription="修改剪裁背景颜色"/>
|
||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/btn_done"
|
android:id="@+id/btn_done"
|
||||||
android:layout_width="40dp"
|
android:layout_width="40dp"
|
||||||
|
|||||||
Reference in New Issue
Block a user