diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/BgSelectorDialog.java b/gallery/src/main/java/cc/winboll/studio/gallery/BgSelectorDialog.java deleted file mode 100644 index 7dd5473..0000000 --- a/gallery/src/main/java/cc/winboll/studio/gallery/BgSelectorDialog.java +++ /dev/null @@ -1,64 +0,0 @@ -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; - } -} 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 b08f012..a54aefc 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/CropActivity.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/CropActivity.java @@ -3,7 +3,6 @@ package cc.winboll.studio.gallery; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.RectF; -import android.graphics.drawable.GradientDrawable; import android.net.Uri; import android.os.Bundle; import android.content.DialogInterface; @@ -74,15 +73,6 @@ public class CropActivity extends AppCompatActivity { } }); - final ImageView btnBg = findViewById(R.id.btn_color_pick); - btnBg.setImageResource(R.drawable.ic_bg); - btnBg.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - showBgDialog(); - } - }); - findViewById(R.id.btn_done).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -118,11 +108,7 @@ public class CropActivity extends AppCompatActivity { cropCanvasView = findViewById(R.id.crop_canvas_view); - final View colorView = findViewById(R.id.color_view); - final GradientDrawable colorDrawable = (GradientDrawable) colorView.getBackground(); - cropCanvasView.setBackgroundType(bgType); - updateColorView(colorDrawable, bgType); cropCanvasView.setOnBackgroundColorChangedListener(new CropCanvasView.OnBackgroundColorChangedListener() { @Override @@ -133,38 +119,6 @@ public class CropActivity extends AppCompatActivity { loadImage(); } - private void updateColorView(GradientDrawable drawable, int bgType) { - switch (bgType) { - case 0: - drawable.setColor(0xFF808080); - break; - case 1: - drawable.setColor(0xFFFFFFFF); - break; - case 2: - default: - drawable.setColor(0xFF000000); - break; - } - } - - private void showBgDialog() { - final View colorView = findViewById(R.id.color_view); - final GradientDrawable colorDrawable = (GradientDrawable) colorView.getBackground(); - final int currentBgType = cropCanvasView.getBackgroundType(); - - BgSelectorDialog dialog = new BgSelectorDialog(this, currentBgType); - dialog.setOnBgSelectedListener(new BgSelectorDialog.OnBgSelectedListener() { - @Override - public void onBgSelected(int which) { - cropCanvasView.setBackgroundType(which); - prefs.setBgType(which); - updateColorView(colorDrawable, which); - } - }); - dialog.show(); - } - private void loadImage() { try { if (imagePath != null && new File(imagePath).exists()) { diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/GlobalWinBoLLApplication.java b/gallery/src/main/java/cc/winboll/studio/gallery/GlobalWinBoLLApplication.java index c48b23b..a79f82d 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/GlobalWinBoLLApplication.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/GlobalWinBoLLApplication.java @@ -4,6 +4,7 @@ import cc.winboll.studio.libaes.utils.WinBoLLActivityManager; import cc.winboll.studio.libappbase.GlobalApplication; import cc.winboll.studio.libappbase.LogUtils; import cc.winboll.studio.libappbase.ToastUtils; +import cc.winboll.studio.gallery.utils.BackgroundUtils; /** * @Author 豆包&ZhanGSKen @@ -23,6 +24,8 @@ public class GlobalWinBoLLApplication extends GlobalApplication { WinBoLLActivityManager.init(this); + BackgroundUtils.initFromPreferences(this); + // 初始化 Toast 框架 ToastUtils.init(this); // 设置 Toast 布局样式 diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/ImagePagerAdapter.java b/gallery/src/main/java/cc/winboll/studio/gallery/ImagePagerAdapter.java index 8e46532..e420f9b 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/ImagePagerAdapter.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/ImagePagerAdapter.java @@ -16,25 +16,10 @@ import cc.winboll.studio.libappbase.LogUtils; public class ImagePagerAdapter extends PagerAdapter { public static final String TAG = "ImagePagerAdapter"; private ArrayList imageUrls; - private int bgType; - public ImagePagerAdapter(ArrayList imageUrls, int bgType) { + public ImagePagerAdapter(ArrayList imageUrls) { this.imageUrls = imageUrls; - this.bgType = bgType; - LogUtils.d(TAG, "ImagePagerAdapter created with " + imageUrls.size() + " images, bgType=" + bgType); - } - - private int getBgRes() { - switch (bgType) { - case 0: - return R.drawable.bg_checkerboard; - case 1: - return R.drawable.bg_white; - case 2: - return R.drawable.bg_black; - default: - return R.drawable.bg_checkerboard; - } + LogUtils.d(TAG, "ImagePagerAdapter created with " + imageUrls.size() + " images"); } @Override @@ -47,7 +32,7 @@ public class ImagePagerAdapter extends PagerAdapter { public Object instantiateItem(@NonNull ViewGroup container, int position) { View view = LayoutInflater.from(container.getContext()) .inflate(R.layout.item_image_pager, container, false); - view.setBackgroundResource(getBgRes()); + view.setBackgroundResource(R.color.black); ImageView imageView = view.findViewById(R.id.image); Glide.with(imageView.getContext()) diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/ImageViewerActivity.java b/gallery/src/main/java/cc/winboll/studio/gallery/ImageViewerActivity.java index c95f736..2bb7aac 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/ImageViewerActivity.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/ImageViewerActivity.java @@ -34,9 +34,7 @@ public class ImageViewerActivity extends Activity implements ViewPager.OnPageCha private ImageButton btnDelete; private ImageButton btnShare; private ImageButton btnInfo; - private ImageButton btnBg; private ImageButton btnGallery; - private int bgType = 0; private GestureDetector gestureDetector; private TrashManager trashManager; private Preferences prefs; @@ -55,7 +53,6 @@ public class ImageViewerActivity extends Activity implements ViewPager.OnPageCha trashManager = new TrashManager(this); prefs = new Preferences(this); - bgType = prefs.getBgType(); viewPager = findViewById(R.id.view_pager); toolbar = findViewById(R.id.toolbar); @@ -63,13 +60,10 @@ public class ImageViewerActivity extends Activity implements ViewPager.OnPageCha btnDelete = findViewById(R.id.btn_delete); btnShare = findViewById(R.id.btn_share); btnInfo = findViewById(R.id.btn_info); - btnBg = findViewById(R.id.btn_bg); btnGallery = findViewById(R.id.btn_gallery); - applyBg(); - - ImagePagerAdapter adapter = new ImagePagerAdapter(imageUrls, bgType); + ImagePagerAdapter adapter = new ImagePagerAdapter(imageUrls); viewPager.setAdapter(adapter); viewPager.setCurrentItem(currentPosition); viewPager.addOnPageChangeListener(this); @@ -117,13 +111,6 @@ public class ImageViewerActivity extends Activity implements ViewPager.OnPageCha } }); - btnBg.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - switchBg(); - } - }); - btnGallery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -147,48 +134,6 @@ public class ImageViewerActivity extends Activity implements ViewPager.OnPageCha } } - private void applyBg() { - int bgRes; - switch (bgType) { - case 0: - bgRes = R.drawable.bg_checkerboard; - break; - case 1: - bgRes = R.drawable.bg_white; - break; - case 2: - bgRes = R.drawable.bg_black; - break; - default: - bgRes = R.drawable.bg_checkerboard; - } - View container = findViewById(R.id.container); - if (container != null) { - container.setBackgroundResource(bgRes); - } - } - - private void switchBg() { - final int[] bgResources = {R.drawable.bg_checkerboard, R.drawable.bg_white, R.drawable.bg_black}; - - BgSelectorDialog dialog = new BgSelectorDialog(this, bgType); - dialog.setOnBgSelectedListener(new BgSelectorDialog.OnBgSelectedListener() { - @Override - public void onBgSelected(int which) { - bgType = which; - prefs.setBgType(which); - int currentItem = viewPager.getCurrentItem(); - View container = findViewById(R.id.container); - if (container != null) { - container.setBackgroundResource(bgResources[which]); - } - viewPager.setAdapter(new ImagePagerAdapter(imageUrls, bgType)); - viewPager.setCurrentItem(currentItem); - } - }); - dialog.show(); - } - private void showDeleteDialog() { new AlertDialog.Builder(this) .setMessage("Delete to trash?") @@ -253,7 +198,7 @@ public class ImageViewerActivity extends Activity implements ViewPager.OnPageCha if (currentPosition >= imageUrls.size()) { currentPosition = imageUrls.size() - 1; } - viewPager.setAdapter(new ImagePagerAdapter(imageUrls, bgType)); + viewPager.setAdapter(new ImagePagerAdapter(imageUrls)); viewPager.setCurrentItem(currentPosition); } } diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/MainActivity.java b/gallery/src/main/java/cc/winboll/studio/gallery/MainActivity.java index d9e5ece..5ca2b77 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/MainActivity.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/MainActivity.java @@ -28,6 +28,7 @@ import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.floatingactionbutton.FloatingActionButton; import cc.winboll.studio.gallery.AlbumAdapter.OnAlbumClickListener; +import cc.winboll.studio.gallery.utils.BackgroundUtils; import cc.winboll.studio.libappbase.LogUtils; import cc.winboll.studio.libappbase.LogActivity; import java.io.File; @@ -50,6 +51,11 @@ public class MainActivity extends AppCompatActivity { setContentView(R.layout.activity_main); LogUtils.d(TAG, "onCreate"); + View content = findViewById(android.R.id.content); + if (content != null) { + content.setBackground(BackgroundUtils.getInstance().getDrawable()); + } + Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); @@ -295,6 +301,9 @@ private void loadAlbums() { intent.addCategory(Intent.CATEGORY_APP_GALLERY); startActivity(intent); return true; + } else if (id == R.id.action_change_bg_color) { + Toast.makeText(this, "修改背景颜色", Toast.LENGTH_SHORT).show(); + return true; } else if (id == R.id.action_settings) { startActivity(new Intent(this, SettingsActivity.class)); return true; diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/utils/BackgroundUtils.java b/gallery/src/main/java/cc/winboll/studio/gallery/utils/BackgroundUtils.java new file mode 100644 index 0000000..bfcf1e3 --- /dev/null +++ b/gallery/src/main/java/cc/winboll/studio/gallery/utils/BackgroundUtils.java @@ -0,0 +1,116 @@ +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 BackgroundUtils { + + public enum DrawableType { + RESOURCE_ID, + COLOR + } + + private static volatile BackgroundUtils instance; + + private static final String PREF_NAME = "background_prefs"; + private static final String KEY_TYPE = "bg_type"; + private static final String KEY_RES_ID = "bg_res_id"; + private static final String KEY_COLOR = "bg_color"; + + private Context context; + private Drawable drawable; + private DrawableType drawableType; + private int resId; + private int color; + + private BackgroundUtils() { + } + + public static BackgroundUtils getInstance() { + if (instance == null) { + synchronized (BackgroundUtils.class) { + if (instance == null) { + instance = new BackgroundUtils(); + } + } + } + return instance; + } + + public static BackgroundUtils initFromResource(Context context, @DrawableRes int resId) { + synchronized (BackgroundUtils.class) { + BackgroundUtils utils = getInstance(); + utils.context = context.getApplicationContext(); + utils.drawableType = DrawableType.RESOURCE_ID; + utils.resId = resId; + utils.drawable = ContextCompat.getDrawable(utils.context, resId); + return utils; + } + } + + public static BackgroundUtils initFromColor(Context context, @ColorInt int color) { + synchronized (BackgroundUtils.class) { + BackgroundUtils utils = getInstance(); + utils.context = context.getApplicationContext(); + utils.drawableType = DrawableType.COLOR; + utils.color = color; + utils.drawable = new ColorDrawable(color); + return utils; + } + } + + public static BackgroundUtils initFromPreferences(Context context) { + synchronized (BackgroundUtils.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); + } + BackgroundUtils utils = initFromColor(appContext, 0xFF00FF00); + utils.saveToPreferences(); + return utils; + } + } + + public Drawable getDrawable() { + return drawable; + } + + public DrawableType getDrawableType() { + return drawableType; + } + + 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 4fd1cc4..ddec4e4 100644 --- a/gallery/src/main/res/layout/activity_crop.xml +++ b/gallery/src/main/res/layout/activity_crop.xml @@ -33,30 +33,6 @@ android:gravity="center" android:layout_marginEnd="8dp"/> - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/gallery/src/main/res/menu/menu_main.xml b/gallery/src/main/res/menu/menu_main.xml index 7fec054..3c96f85 100644 --- a/gallery/src/main/res/menu/menu_main.xml +++ b/gallery/src/main/res/menu/menu_main.xml @@ -8,6 +8,11 @@ android:icon="@drawable/ic_mi_gallery" app:showAsAction="ifRoom"/> + +