test-kivy-app/kivy_venv/lib/python3.11/site-packages/kivymd/uix/fitimage/fitimage.py

113 lines
3.2 KiB
Python
Raw Normal View History

2024-09-15 12:12:16 +00:00
"""
Components/FitImage
===================
2024-09-15 17:57:02 +00:00
Example
=======
2024-09-15 12:12:16 +00:00
.. tabs::
.. tab:: Declarative KV styles
.. code-block:: python
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDScreen:
2024-09-15 17:57:02 +00:00
md_bg_color: self.theme_cls.backgroundColor
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDBoxLayout:
radius: "36dp"
2024-09-15 12:12:16 +00:00
pos_hint: {"center_x": .5, "center_y": .5}
size_hint: .4, .8
2024-09-15 17:57:02 +00:00
md_bg_color: self.theme_cls.onSurfaceVariantColor
2024-09-15 12:12:16 +00:00
FitImage:
2024-09-15 17:57:02 +00:00
source: "image.png"
2024-09-15 12:12:16 +00:00
size_hint_y: .35
pos_hint: {"top": 1}
2024-09-15 17:57:02 +00:00
radius: "36dp", "36dp", 0, 0
2024-09-15 12:12:16 +00:00
'''
class Example(MDApp):
def build(self):
return Builder.load_string(KV)
Example().run()
2024-09-15 17:57:02 +00:00
2024-09-15 12:12:16 +00:00
.. tab:: Declarative python styles
.. code-block:: python
2024-09-15 17:57:02 +00:00
from kivy.metrics import dp
2024-09-15 12:12:16 +00:00
from kivymd.app import MDApp
from kivymd.uix.card import MDCard
from kivymd.uix.fitimage import FitImage
from kivymd.uix.screen import MDScreen
class Example(MDApp):
def build(self):
return (
MDScreen(
2024-09-15 17:57:02 +00:00
MDBoxLayout(
2024-09-15 12:12:16 +00:00
FitImage(
2024-09-15 17:57:02 +00:00
source="image.png",
2024-09-15 12:12:16 +00:00
size_hint_y=0.35,
pos_hint={"top": 1},
2024-09-15 17:57:02 +00:00
radius=(dp(36), dp(36), 0, 0),
2024-09-15 12:12:16 +00:00
),
2024-09-15 17:57:02 +00:00
radius=dp(36),
md_bg_color=self.theme_cls.onSurfaceVariantColor,
pos_hint={"center_x": 0.5, "center_y": 0.5},
2024-09-15 12:12:16 +00:00
size_hint=(0.4, 0.8),
),
)
)
Example().run()
2024-09-15 17:57:02 +00:00
.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/fitimage-round-corners.png
:align: center
2024-09-15 12:12:16 +00:00
"""
__all__ = ("FitImage",)
2024-09-15 17:57:02 +00:00
from kivy.properties import OptionProperty
2024-09-15 12:12:16 +00:00
from kivy.uix.image import AsyncImage
from kivymd.uix.behaviors import StencilBehavior
2024-09-15 17:57:02 +00:00
from kivymd.uix.behaviors import DeclarativeBehavior
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
class FitImage(DeclarativeBehavior, StencilBehavior, AsyncImage):
2024-09-15 12:12:16 +00:00
"""
Fit image class.
For more information, see in the
2024-09-15 17:57:02 +00:00
:class:`~kivymd.uix.behaviors.declarative_behavior.DeclarativeBehavior` and
:class:`~kivy.uix.image.AsyncImage` and
:class:`~kivymd.uix.behaviors.stencil_behavior.StencilBehavior`
classes documentation.
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
fit_mode = OptionProperty(
"cover", options=["scale-down", "fill", "contain", "cover"]
)
2024-09-15 12:12:16 +00:00
"""
2024-09-15 17:57:02 +00:00
Image will be stretched horizontally or vertically to fill the widget box,
**maintaining its aspect ratio**. If the image has a different aspect ratio
than the widget, then the image will be clipped to fit.
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
:attr:`fit_mode` is a :class:`~kivy.properties.OptionProperty` and
defaults to `'cover'`.
2024-09-15 12:12:16 +00:00
"""