Skip to content

Commit

Permalink
feat: more and improved resize options
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Jan 11, 2024
1 parent a54d4e7 commit 0bc9867
Show file tree
Hide file tree
Showing 36 changed files with 104 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/enums/ResizeMethod.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.bnyro.wallpaper.enums

enum class ResizeMethod {
NONE,
CROP,
ZOOM,
FIT_WIDTH,
FIT_HEIGHT,
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.bnyro.wallpaper.R
import com.bnyro.wallpaper.enums.ResizeMethod
import com.bnyro.wallpaper.ui.components.DialogButton
import com.bnyro.wallpaper.ui.components.ImageFilterSlider
import com.bnyro.wallpaper.ui.components.prefs.CheckboxPref
import com.bnyro.wallpaper.ui.components.prefs.ListPreference
import com.bnyro.wallpaper.util.Preferences

@Composable
Expand All @@ -25,6 +27,7 @@ fun ImageFilterDialog(
) {
Preferences.edit { putFloat(Preferences.blurKey, 1f) }
Preferences.edit { putBoolean(Preferences.grayscaleKey, false) }
Preferences.edit { putString(Preferences.resizeMethodKey, ResizeMethod.NONE.name) }
onChange.invoke()
onDismissRequest.invoke()
}
Expand Down Expand Up @@ -56,6 +59,14 @@ fun ImageFilterDialog(
) {
onChange.invoke()
}
val resizeMethods = listOf(R.string.none, R.string.crop, R.string.zoom, R.string.fit_width, R.string.fit_height)
ListPreference(
prefKey = Preferences.resizeMethodKey,
title = stringResource(R.string.resize_method),
entries = resizeMethods.map { stringResource(it) },
values = ResizeMethod.values().map { it.name },
defaultValue = ResizeMethod.ZOOM.name
)
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fun ListPreference(

if (showDialog) {
ListDialog(
title = title,
items = entries,
onDismissRequest = {
showDialog = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ fun SettingsPage(
) {
viewModel.themeMode = ThemeMode.values()[it.toInt()]
}
CheckboxPref(
prefKey = Preferences.cropImagesKey,
title = stringResource(R.string.crop_images)
)
CheckboxPref(
prefKey = Preferences.autoAddToFavoritesKey,
title = stringResource(R.string.auto_add_to_favorites)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper

object Preferences {
const val cropImagesKey = "cropImages"
const val resizeMethodKey = "resizeMethod"
const val diskCacheKey = "diskCache"
const val themeModeKey = "themeModeKey"
const val autoAddToFavoritesKey = "autoAddToFavorites"
Expand Down
76 changes: 38 additions & 38 deletions app/src/main/java/com/bnyro/wallpaper/util/WallpaperHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import android.app.WallpaperManager
import android.content.Context
import android.graphics.Bitmap
import android.os.Build
import android.util.DisplayMetrics
import androidx.annotation.RequiresApi
import com.bnyro.wallpaper.enums.ResizeMethod
import com.bnyro.wallpaper.enums.WallpaperTarget
import kotlin.math.absoluteValue

object WallpaperHelper {
@RequiresApi(Build.VERSION_CODES.N)
Expand All @@ -26,15 +27,12 @@ object WallpaperHelper {

fun setWallpaper(context: Context, bitmap: Bitmap, mode: WallpaperTarget) {
Thread {
val cropImages = Preferences.getBoolean(
Preferences.cropImagesKey,
false
)
val resizedBitmap = if (cropImages) {
getCroppedBitmap(bitmap, context.resources.displayMetrics)
} else {
getResizedBitmap(bitmap, context.resources.displayMetrics)
}
val resizeMethod = Preferences.getString(
Preferences.resizeMethodKey,
ResizeMethod.ZOOM.name
).let { ResizeMethod.valueOf(it) }
val resizedBitmap = processBitmapByResizeMethod(context, bitmap, resizeMethod)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (mode in listOf(WallpaperTarget.BOTH, WallpaperTarget.HOME)) {
setWallpaperUp(context, resizedBitmap, WallpaperManager.FLAG_SYSTEM)
Expand All @@ -48,35 +46,34 @@ object WallpaperHelper {
}.start()
}

private fun getCroppedBitmap(bitmap: Bitmap, displayMetrics: DisplayMetrics): Bitmap {
return Bitmap.createScaledBitmap(
bitmap,
displayMetrics.widthPixels,
displayMetrics.heightPixels,
true
)
private fun processBitmapByResizeMethod(context: Context, bitmap: Bitmap, resizeMethod: ResizeMethod): Bitmap {
val metrics = context.resources.displayMetrics

return when (resizeMethod) {
ResizeMethod.CROP -> getResizedBitmap(bitmap, metrics.widthPixels, metrics.heightPixels)
ResizeMethod.ZOOM -> getZoomedBitmap(bitmap, metrics.widthPixels, metrics.heightPixels)
ResizeMethod.FIT_WIDTH -> getBitmapFitWidth(bitmap, metrics.widthPixels)
ResizeMethod.FIT_HEIGHT -> getBitmapFitHeight(bitmap, metrics.heightPixels)
ResizeMethod.NONE -> bitmap
}
}

private fun getResizedBitmap(bitmap: Bitmap, displayMetrics: DisplayMetrics): Bitmap {
val screenWidth = displayMetrics.widthPixels
val screenHeight = displayMetrics.heightPixels
val bitmapWidth = bitmap.width.toFloat()
val bitmapHeight = bitmap.height.toFloat()
private fun getResizedBitmap(bitmap: Bitmap, width: Int, height: Int, filter: Boolean = true): Bitmap {
return Bitmap.createScaledBitmap(bitmap, width, height, filter)
}

val bitmapRatio = bitmapHeight / bitmapWidth
val screenRatio = screenHeight / screenWidth
private fun getZoomedBitmap(bitmap: Bitmap, screenWidth: Int, screenHeight: Int): Bitmap {
val bitmapRatio = bitmap.height.toFloat() / bitmap.width.toFloat()
val screenRatio = screenHeight.toFloat() / screenWidth.toFloat()

val resizedBitmap = if (screenRatio > bitmapRatio) {
getResizedBitmap(bitmap, screenWidth, (screenWidth * bitmapRatio).toInt())
getResizedBitmap(bitmap, screenWidth, (screenWidth * bitmapRatio).toInt(), false)
} else {
getResizedBitmap(bitmap, (screenHeight / bitmapRatio).toInt(), screenHeight)
getResizedBitmap(bitmap, (screenHeight / bitmapRatio).toInt(), screenHeight, false)
}

val bitmapGapX = ((bitmap.width - screenWidth) / 2f).toInt()
val bitmapGapY = ((bitmap.height - screenHeight) / 2f).toInt()

// prevent crashes due to wrong aspect ratio
if (bitmapGapX <= 0 || bitmapGapY <= 0) return resizedBitmap
val bitmapGapX = ((resizedBitmap.width - screenWidth) / 2).absoluteValue
val bitmapGapY = ((resizedBitmap.height - screenHeight) / 2).absoluteValue

return runCatching {
Bitmap.createBitmap(
Expand All @@ -89,12 +86,15 @@ object WallpaperHelper {
}.getOrDefault(resizedBitmap)
}

private fun getResizedBitmap(bitmap: Bitmap, newWidth: Int, newHeight: Int): Bitmap {
return Bitmap.createScaledBitmap(
bitmap,
newWidth,
newHeight,
false
)
private fun getBitmapFitWidth(bitmap: Bitmap, width: Int): Bitmap {
val heightRatio = width.toFloat() / bitmap.width.toFloat()

return getResizedBitmap(bitmap, width, (bitmap.height * heightRatio).toInt())
}

private fun getBitmapFitHeight(bitmap: Bitmap, height: Int): Bitmap {
val widthRatio = height.toFloat() / bitmap.height.toFloat()

return getResizedBitmap(bitmap, (bitmap.width * widthRatio).toInt(), height)
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<string name="blur">الضبابيَّة</string>
<string name="cache">الخبيئة</string>
<string name="wallpaper_changer">مغيِّر الخلفيَّة</string>
<string name="crop_images">قُصَّ الصور</string>
<string name="crop">قُصَّ الصور</string>
<string name="wallpaper_changer_source">مصدر مغيِّر الخلفية</string>
<string name="online">عبر الشبكة</string>
<string name="local">محليٌّ</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-az/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<string name="reset">Sıfırla</string>
<string name="general">Ümumi</string>
<string name="cache">Keş</string>
<string name="crop_images">Şəkilləri kəs</string>
<string name="crop">Şəkilləri kəs</string>
<string name="coil_cache">Halqa şəkil keşi</string>
<string name="wallpaper_changer">Divar kağızı dəyişdirici</string>
<string name="change_interval">Dəyişiklik intervalı</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-be/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<string name="reset">Скід</string>
<string name="general">Асноўныя</string>
<string name="cache">Кэш</string>
<string name="crop_images">Абрэзаць выявы</string>
<string name="crop">Абрэзаць выявы</string>
<string name="wallpaper_changer">Змена шпалер</string>
<string name="change_interval">Інтэрвал змены</string>
<string name="wallpaper_changer_source">Крыніца змены шпалер</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-bn/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<string name="general">সাধারণ</string>
<string name="cache">ক্যাশ</string>
<!-- Preferences -->
<string name="crop_images">ক্রপ করুন</string>
<string name="crop">ক্রপ করুন</string>
<string name="coil_cache">ক্যাশের সাইজ</string>
<string name="wallpaper_changer">ওয়ালপেপার চেঞ্জার</string>
<string name="change_interval">পরিবর্তনের সময়</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<string name="reset">Obnovit</string>
<string name="general">Obecné</string>
<string name="cache">Mezipaměť</string>
<string name="crop_images">Oříznout obrázky</string>
<string name="crop">Oříznout obrázky</string>
<string name="coil_cache">Mezipaměť obrázků</string>
<string name="wallpaper_changer">Měnič tapet</string>
<string name="change_interval">Interval změn</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="crop_images">Bilder zuschneiden</string>
<string name="crop">Bilder zuschneiden</string>
<string name="category">Kategorie</string>
<string name="source">Quelle</string>
<string name="creationDate">Erstellungsdatum</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<string name="general">General</string>
<string name="cache">Caché</string>
<!-- Preferences -->
<string name="crop_images">Recortar imágenes</string>
<string name="crop">Recortar imágenes</string>
<string name="coil_cache">Caché de imagen de bobina</string>
<string name="wallpaper_changer">Cambiador de papel tapiz</string>
<string name="change_interval">Intervalo de cambio</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-fi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<string name="no_favorites">Ei vielä suosikkeja</string>
<string name="applying_random">Toteutaan satunnainen taustakuva</string>
<string name="filter">Suodatin</string>
<string name="crop_images">Rajaa kuvat</string>
<string name="crop">Rajaa kuvat</string>
<string name="reset">Nollaa</string>
<string name="lockscreen">Lukitusnäyttö</string>
<string name="both">Molemmat</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-fr-rFR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<string name="general">Général</string>
<string name="cache">Cache</string>
<!-- Preferences -->
<string name="crop_images">Recadrer les images</string>
<string name="crop">Recadrer les images</string>
<string name="coil_cache">Cache d\'image du diaporama</string>
<string name="wallpaper_changer">Modification du fond d\'écran</string>
<string name="change_interval">Intervalle de changement</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-id/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<string name="general">Umum</string>
<string name="cache">Cache</string>
<!-- Preferences -->
<string name="crop_images">Pangkas gambar</string>
<string name="crop">Pangkas gambar</string>
<string name="coil_cache">Cache gambar koil</string>
<string name="wallpaper_changer">Pengubah wallpaper</string>
<string name="change_interval">Ubah interval</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<string name="applying_random">Applicando sfondo casuale</string>
<string name="wallpaper_changer">Sorgente cambio sfondo</string>
<string name="grayscale">Scala di grigi</string>
<string name="crop_images">Ritaglia immagini</string>
<string name="crop">Ritaglia immagini</string>
<string name="change_interval">Cambia intervallo</string>
<string name="theme_mode">Tema</string>
<string name="theme_light">Chiaro</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-iw/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<string name="reset">איפוס</string>
<string name="license">רישיון</string>
<string name="version">גרסה</string>
<string name="crop_images">חיתוך תמונות</string>
<string name="crop">חיתוך תמונות</string>
<string name="website">אתר</string>
<string name="online">מקוון</string>
<string name="theme_dark">כהה</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<string name="cache">キャッシュ</string>
<string name="wallpaper_preview">壁紙のプレビュー</string>
<!-- Preferences -->
<string name="crop_images">画像のトリミング</string>
<string name="crop">画像のトリミング</string>
<string name="coil_cache">コイル画像キャッシュ</string>
<string name="wallpaper_changer">壁紙チェンジャー</string>
<string name="change_interval">変更間隔</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-ko/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<string name="website">웹사이트</string>
<string name="favorites">즐겨찾기</string>
<string name="lockscreen">잠금화면</string>
<string name="crop_images">이미지 자르기</string>
<string name="crop">이미지 자르기</string>
<string name="fileSize">파일 크기</string>
<string name="creationDate">생성 날짜</string>
<string name="settings">설정</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-nb-rNO/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<string name="reset">Tilbakestill</string>
<string name="general">Generelt</string>
<string name="cache">Hurtiglager</string>
<string name="crop_images">Beskjær bilder</string>
<string name="crop">Beskjær bilder</string>
<string name="wallpaper_changer">Skjermbildeendrer</string>
<string name="change_interval">Endringsintervall</string>
<string name="theme_mode">Tema</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-or/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<string name="reset">ପୁନଃସେଟ୍ କରନ୍ତୁ</string>
<string name="general">ସାଧାରଣ</string>
<string name="cache">କ୍ୟାଚ୍</string>
<string name="crop_images">ପ୍ରତିଛବିଗୁଡିକ କ୍ରପ୍ କରନ୍ତୁ</string>
<string name="crop">ପ୍ରତିଛବିଗୁଡିକ କ୍ରପ୍ କରନ୍ତୁ</string>
<string name="wallpaper_changer">ୱାଲପେପର ଚେଞ୍ଜର</string>
<string name="theme_light">ଉଜ୍ଜଳ</string>
<string name="theme_dark">ଅନ୍ଧାର</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-pl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<string name="reset">Resetowanie</string>
<string name="general">Główne</string>
<string name="cache">Pamięć podręczna</string>
<string name="crop_images">Przycinaj zdjęcia</string>
<string name="crop">Przycinaj zdjęcia</string>
<string name="change_interval">Czas pomiędzy zmianą</string>
<string name="theme_mode">Motyw</string>
<string name="no_favorites">Nie ma jeszcze ulubionych</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<string name="grayscale">Escala de cinza</string>
<string name="reset">Redefinir</string>
<string name="cache">Cache</string>
<string name="crop_images">Cortar imagens</string>
<string name="crop">Cortar imagens</string>
<string name="coil_cache">Tamanho do cache de imagem</string>
<string name="wallpaper_changer">Mudança de papel de parede</string>
<string name="change_interval">Intervalo de mudança</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<string name="reset">Repor</string>
<string name="general">Geral</string>
<string name="cache">Cache</string>
<string name="crop_images">Imagens de corte</string>
<string name="crop">Imagens de corte</string>
<string name="coil_cache">Cache de imagem da bobina</string>
<string name="wallpaper_changer">Mudança de papel de parede</string>
<string name="change_interval">Intervalo de mudança</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-ro/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<string name="reset">Resetați</string>
<string name="general">General</string>
<string name="cache">Cache</string>
<string name="crop_images">Decupare imagini</string>
<string name="crop">Decupare imagini</string>
<string name="coil_cache">Cache de imagini</string>
<string name="wallpaper_changer">Schimbare fundal</string>
<string name="theme_mode">Temă</string>
Expand Down
Loading

0 comments on commit 0bc9867

Please sign in to comment.