refactor: 抽象背景选择对话框为独立的BgSelectorDialog类

- 新增BgSelectorDialog对话框类,继承Dialog
- 新增dialog_bg_selector.xml布局文件定义对话框视图
- 重构ImageViewerActivity.switchBg()使用新的BgSelectorDialog
- 重构CropActivity.showBgDialog()使用新的BgSelectorDialog
- 统一两个Activity的背景选择交互方式
This commit is contained in:
2026-05-01 10:25:45 +08:00
parent 6d521fefdb
commit 29e7cfe985
5 changed files with 136 additions and 36 deletions

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle #Created by .winboll/winboll_app_build.gradle
#Fri May 01 09:21:07 HKT 2026 #Fri May 01 10:20:57 CST 2026
stageCount=14 stageCount=14
libraryProject= libraryProject=
baseVersion=15.0 baseVersion=15.0
publishVersion=15.0.13 publishVersion=15.0.13
buildCount=0 buildCount=3
baseBetaVersion=15.0.14 baseBetaVersion=15.0.14

View File

@@ -0,0 +1,64 @@
package cc.winboll.studio.gallery;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class BgSelectorDialog extends Dialog {
public interface OnBgSelectedListener {
void onBgSelected(int bgType);
}
private int currentBgType;
private OnBgSelectedListener listener;
public BgSelectorDialog(Context context, int currentBgType) {
super(context);
this.currentBgType = currentBgType;
setContentView(R.layout.dialog_bg_selector);
initViews();
}
private void initViews() {
RadioGroup radioGroup = findViewById(R.id.radio_group_bg);
RadioButton radioCheckerboard = findViewById(R.id.radio_checkerboard);
RadioButton radioWhite = findViewById(R.id.radio_white);
RadioButton radioBlack = findViewById(R.id.radio_black);
switch (currentBgType) {
case 0:
radioCheckerboard.setChecked(true);
break;
case 1:
radioWhite.setChecked(true);
break;
case 2:
radioBlack.setChecked(true);
break;
}
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int which = 0;
if (checkedId == R.id.radio_white) {
which = 1;
} else if (checkedId == R.id.radio_black) {
which = 2;
}
if (listener != null) {
listener.onBgSelected(which);
}
dismiss();
}
});
}
public void setOnBgSelectedListener(OnBgSelectedListener listener) {
this.listener = listener;
}
}

View File

@@ -147,25 +147,20 @@ public class CropActivity extends AppCompatActivity {
} }
private void showBgDialog() { private void showBgDialog() {
final String[] bgNames = {"灰白相间", "全白", "全黑"};
final int[] bgDrawables = {R.drawable.bg_checkerboard, R.drawable.bg_white, R.drawable.bg_black};
final View colorView = findViewById(R.id.color_view); final View colorView = findViewById(R.id.color_view);
final GradientDrawable colorDrawable = (GradientDrawable) colorView.getBackground(); final GradientDrawable colorDrawable = (GradientDrawable) colorView.getBackground();
final int currentBgType = cropCanvasView.getBackgroundType(); final int currentBgType = cropCanvasView.getBackgroundType();
new AlertDialog.Builder(this) BgSelectorDialog dialog = new BgSelectorDialog(this, currentBgType);
.setTitle("选择背景") dialog.setOnBgSelectedListener(new BgSelectorDialog.OnBgSelectedListener() {
.setSingleChoiceItems(bgNames, currentBgType, new android.content.DialogInterface.OnClickListener() { @Override
@Override public void onBgSelected(int which) {
public void onClick(android.content.DialogInterface dialog, int which) { cropCanvasView.setBackgroundType(which);
cropCanvasView.setBackgroundType(which); prefs.setBgType(which);
prefs.setBgType(which); updateColorView(colorDrawable, which);
updateColorView(colorDrawable, which); }
dialog.dismiss(); });
} dialog.show();
})
.setNegativeButton("取消", null)
.show();
} }
private void loadImage() { private void loadImage() {

View File

@@ -169,28 +169,24 @@ public class ImageViewerActivity extends Activity implements ViewPager.OnPageCha
} }
private void switchBg() { private void switchBg() {
final String[] bgNames = {"灰白相间", "全白", "全黑"};
final int[] bgResources = {R.drawable.bg_checkerboard, R.drawable.bg_white, R.drawable.bg_black}; final int[] bgResources = {R.drawable.bg_checkerboard, R.drawable.bg_white, R.drawable.bg_black};
new AlertDialog.Builder(this) BgSelectorDialog dialog = new BgSelectorDialog(this, bgType);
.setTitle("选择背景") dialog.setOnBgSelectedListener(new BgSelectorDialog.OnBgSelectedListener() {
.setSingleChoiceItems(bgNames, bgType, new android.content.DialogInterface.OnClickListener() { @Override
@Override public void onBgSelected(int which) {
public void onClick(android.content.DialogInterface dialog, int which) { bgType = which;
bgType = which; prefs.setBgType(which);
prefs.setBgType(which); int currentItem = viewPager.getCurrentItem();
int currentItem = viewPager.getCurrentItem(); View container = findViewById(R.id.container);
View container = findViewById(R.id.container); if (container != null) {
if (container != null) { container.setBackgroundResource(bgResources[which]);
container.setBackgroundResource(bgResources[which]);
}
viewPager.setAdapter(new ImagePagerAdapter(imageUrls, bgType));
viewPager.setCurrentItem(currentItem);
dialog.dismiss();
} }
}) viewPager.setAdapter(new ImagePagerAdapter(imageUrls, bgType));
.setNegativeButton("取消", null) viewPager.setCurrentItem(currentItem);
.show(); }
});
dialog.show();
} }
private void showDeleteDialog() { private void showDeleteDialog() {

View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<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="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择背景"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"
android:paddingBottom="16dp" />
<RadioGroup
android:id="@+id/radio_group_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio_checkerboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="灰白相间"
android:padding="12dp" />
<RadioButton
android:id="@+id/radio_white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="全白"
android:padding="12dp" />
<RadioButton
android:id="@+id/radio_black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="全黑"
android:padding="12dp" />
</RadioGroup>
</LinearLayout>