diff --git a/app/build.gradle b/app/build.gradle index 6e11744..591c742 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,7 +12,7 @@ android { minSdkVersion 29 targetSdkVersion 34 versionCode 4 - versionName "3.0-alpha-240421" + versionName "3.0-alpha-240504" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildFeatures { diff --git a/app/src/main/java/com/maary/shareas/WallpaperViewModel.kt b/app/src/main/java/com/maary/shareas/WallpaperViewModel.kt index d7b3685..ca1f0f6 100644 --- a/app/src/main/java/com/maary/shareas/WallpaperViewModel.kt +++ b/app/src/main/java/com/maary/shareas/WallpaperViewModel.kt @@ -27,6 +27,7 @@ import com.hoko.blur.task.AsyncBlurTask import com.maary.shareas.data.ViewerBitmap import com.maary.shareas.helper.SuperResPerformer import com.maary.shareas.helper.Util +import com.maary.shareas.view.ScrollableImageView import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers @@ -365,6 +366,45 @@ class WallpaperViewModel : ViewModel() { ) } + fun isAlignmentNeeded(context: Context): Boolean { + val deviceBounds = Util.getDeviceBounds(context) + val deviceHeight = deviceBounds.y + val deviceWidth = deviceBounds.x + return !(bitmap!!.width == deviceWidth && bitmap!!.height == deviceHeight) + } + + fun getAlignmentIconResource(context: Context): Int { + val deviceBounds = Util.getDeviceBounds(context) + val deviceHeight = deviceBounds.y + val deviceWidth = deviceBounds.x + return if (deviceWidth < bitmap!!.width) { + R.drawable.ic_center_horizontal + } else if (deviceHeight < bitmap!!.height) { + R.drawable.ic_center_vertical + } else { + R.drawable.ic_center_horizontal + } + } + + fun getCenterAlignParam(context: Context): Triple?{ + val deviceBounds = Util.getDeviceBounds(context) + val deviceHeight = deviceBounds.y + val deviceWidth = deviceBounds.x + if (deviceWidth < bitmap!!.width) { + return Triple( + ScrollableImageView.HORIZONTAL, + (bitmap!!.width - deviceWidth)/2, + 0) + } + if (deviceHeight < bitmap!!.height) { + return Triple( + ScrollableImageView.VERTICAL, + 0, + (bitmap!!.height - deviceHeight)/2) + } + return null + } + private fun getInputBitmap(): Bitmap? { return when (currentBitmap) { HOME -> bakBitmap.bitmapHome diff --git a/app/src/main/java/com/maary/shareas/activity/MainActivity.java b/app/src/main/java/com/maary/shareas/activity/MainActivity.java index 72a1df2..53f9a33 100644 --- a/app/src/main/java/com/maary/shareas/activity/MainActivity.java +++ b/app/src/main/java/com/maary/shareas/activity/MainActivity.java @@ -29,7 +29,6 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.view.menu.ActionMenuItemView; import androidx.core.content.ContextCompat; -import androidx.core.content.res.ResourcesCompat; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowCompat; @@ -56,6 +55,8 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import kotlin.Triple; + public class MainActivity extends AppCompatActivity { static final int MENU_RESET = 0; @@ -166,6 +167,11 @@ public void onColorsChanged(@Nullable WallpaperColors colors, int which) { wallpaperManager.addOnColorsChangedListener(wallpaperChangedListener, new Handler(Looper.getMainLooper())); + if (viewModel.isAlignmentNeeded(this)) { + binding.bottomAppBar.getMenu().getItem(2).setIcon(viewModel.getAlignmentIconResource(this)); + } else { + binding.bottomAppBar.getMenu().getItem(2).setVisible(false); + } //set bottomAppBar menu item //tap blur and brightness button will disable other menu item binding.bottomAppBar.setOnMenuItemClickListener(item -> { @@ -174,6 +180,11 @@ public void onColorsChanged(@Nullable WallpaperColors colors, int which) { loadFragment(new EditorFragment()); } else if (item.getItemId() == R.id.reset) { viewModel.restoreChanges(); + } else if (item.getItemId() == R.id.alignment_center) { + Triple param = viewModel.getCenterAlignParam(this); + if (param != null) { + binding.mainView.scrollImageTo(param.getFirst(), param.getSecond(), param.getThird()); + } } return true; }); diff --git a/app/src/main/java/com/maary/shareas/view/ScrollableImageView.kt b/app/src/main/java/com/maary/shareas/view/ScrollableImageView.kt index 5a38365..727ba0a 100644 --- a/app/src/main/java/com/maary/shareas/view/ScrollableImageView.kt +++ b/app/src/main/java/com/maary/shareas/view/ScrollableImageView.kt @@ -3,6 +3,7 @@ package com.maary.shareas.view import android.content.Context import android.graphics.Bitmap import android.graphics.Canvas +import android.graphics.Point import android.graphics.Rect import android.net.Uri import android.util.AttributeSet @@ -82,6 +83,10 @@ class ScrollableImageView @JvmOverloads constructor( } + fun scrollImageTo(target: Int, x: Int, y: Int) { + if (target == VERTICAL) this.smoothScrollTo(x, y) + if (target == HORIZONTAL) horizontalView.smoothScrollTo(x, y) + } inner class CustomHorizontalView @JvmOverloads constructor( context: Context, @@ -94,4 +99,9 @@ class ScrollableImageView @JvmOverloads constructor( } } + + companion object { + val VERTICAL = 0 + val HORIZONTAL = 1 + } } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_center_horizontal.xml b/app/src/main/res/drawable/ic_center_horizontal.xml new file mode 100644 index 0000000..f036e81 --- /dev/null +++ b/app/src/main/res/drawable/ic_center_horizontal.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_center_vertical.xml b/app/src/main/res/drawable/ic_center_vertical.xml new file mode 100644 index 0000000..eb5853f --- /dev/null +++ b/app/src/main/res/drawable/ic_center_vertical.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/menu/bottom_app_bar.xml b/app/src/main/res/menu/bottom_app_bar.xml index 4e06bd7..a01ed8a 100644 --- a/app/src/main/res/menu/bottom_app_bar.xml +++ b/app/src/main/res/menu/bottom_app_bar.xml @@ -14,6 +14,12 @@ android:title="@string/blur" app:showAsAction="ifRoom"/> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9b9ee8b..3a8446d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -71,4 +71,5 @@ Tile Size Use FP16 Disable CPU + Center