Skip to content

Commit

Permalink
添加图像居中
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve-Mr committed May 4, 2024
1 parent a3a9beb commit 4c4585c
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/java/com/maary/shareas/WallpaperViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Int, Int, Int>?{
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
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/com/maary/shareas/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 -> {
Expand All @@ -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<Integer, Integer, Integer> param = viewModel.getCenterAlignParam(this);
if (param != null) {
binding.mainView.scrollImageTo(param.getFirst(), param.getSecond(), param.getThird());
}
}
return true;
});
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/maary/shareas/view/ScrollableImageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -94,4 +99,9 @@ class ScrollableImageView @JvmOverloads constructor(
}

}

companion object {
val VERTICAL = 0
val HORIZONTAL = 1
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_center_horizontal.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="800dp"
android:height="800dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M16.707,12H23v1h-6.295l2.648,2.647 -0.707,0.707 -3.854,-3.853 3.854,-3.854 0.707,0.707zM5.647,9.354L8.293,12H2v1h6.295l-2.648,2.647 0.707,0.707 3.854,-3.853 -3.854,-3.854zM12,19h1V6h-1z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_center_vertical.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="800dp"
android:height="800dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M12.501,14.792l3.854,3.854 -0.707,0.707L13,16.705V23h-1v-6.293l-2.646,2.646 -0.707,-0.707zM8.647,6.354l3.854,3.854 3.854,-3.854 -0.707,-0.707L13,8.295V2h-1v6.293L9.354,5.647zM6,13h13v-1H6z"/>
</vector>
6 changes: 6 additions & 0 deletions app/src/main/res/menu/bottom_app_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
android:title="@string/blur"
app:showAsAction="ifRoom"/>

<item
android:id="@+id/alignment_center"
android:icon="@drawable/ic_center_vertical"
android:title="@string/center"
app:showAsAction="ifRoom"/>

<!-- <item-->
<!-- android:id="@+id/horizontal"-->
<!-- android:icon="@drawable/ic_horizontal"-->
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@
<string name="tile_size">Tile Size</string>
<string name="fp16">Use FP16</string>
<string name="cpu_disabled">Disable CPU</string>
<string name="center">Center</string>
</resources>

0 comments on commit 4c4585c

Please sign in to comment.