Skip to content

Commit

Permalink
Implement bitmap scale down (#1584)
Browse files Browse the repository at this point in the history
  • Loading branch information
T-Dynamos authored Jan 18, 2024
1 parent 15edad3 commit 22250c7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
5 changes: 3 additions & 2 deletions kivymd/theming.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
OptionProperty,
StringProperty,
)
from kivy import platform
from kivy.utils import get_color_from_hex, rgba, hex_colormap

from kivymd.dynamic_color import DynamicColor
Expand Down Expand Up @@ -147,7 +148,7 @@ def build(self):
and defaults to `None`.
"""

dynamic_color_quality = NumericProperty(10)
dynamic_color_quality = NumericProperty(1 if platform == "android" else 10)
"""
The quality of the generated color scheme from the system wallpaper.
Expand All @@ -157,7 +158,7 @@ def build(self):
generation time of the color scheme.
:attr:`dynamic_color_quality` is an :class:`~kivy.properties.NumericProperty`
and defaults to `10`.
and defaults to `10` if platform is not Android else `1`.
"""

dynamic_color = BooleanProperty(False)
Expand Down
25 changes: 24 additions & 1 deletion kivymd/utils/get_wallpaper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
import math

from kivy import platform, Logger

Expand All @@ -16,9 +17,31 @@ def get_wallpaper(
CompressFormat = autoclass("android.graphics.Bitmap$CompressFormat")
FileOutputStream = autoclass("java.io.FileOutputStream")
WallpaperManager = autoclass("android.app.WallpaperManager")
Bitmap = autoclass("android.graphics.Bitmap")

Context = mActivity.getApplicationContext()
mWallpaperManager = WallpaperManager.getInstance(Context)
mWallpaperManager.getBitmap().compress(
bitmap = mWallpaperManager.getBitmap()

# Scale the bitmap down as needed
# Taken from:
# https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-main/palette/palette/src/main/java/androidx/palette/graphics/Palette.java#890
DEFAULT_RESIZE_BITMAP_AREA = 112 * 112 # Android default
bitmapArea = bitmap.getWidth() * bitmap.getHeight()
scaleRatio = -1

if bitmapArea > DEFAULT_RESIZE_BITMAP_AREA:
scaleRatio = math.sqrt(DEFAULT_RESIZE_BITMAP_AREA / bitmapArea)

if scaleRatio >= 0:
bitmap = Bitmap.createScaledBitmap(
bitmap,
math.ceil(bitmap.getWidth() * scaleRatio),
math.ceil(bitmap.getHeight() * scaleRatio),
False
)

bitmap.compress(
CompressFormat.PNG,
100,
FileOutputStream(f"{user_data_dir}/wallpaper.png"),
Expand Down

0 comments on commit 22250c7

Please sign in to comment.