<image>
Used to display different types of images, including web images, static resources, and locally stored images.
This feature depends on the image loading service provided by Lynx Service.
<image>
is an empty element and no longer supports child nodes.
The following examples show how the <image>
element is used in different scenarios.
Supports controlling the image cropping/scaling mode using mode.
You can set the image's borders, rounded corners, and background colors using CSS styles.
Supports special drawing effects like Gaussian blur.
Use auto-size to automatically adjust the <image>
size to match the original image aspect ratio.
You can bind events to listen for the image load state.
src
RequiredSpecifies the image URL to display.
The supported image formats are: png
, jpg
, jpeg
, bmp
, gif
, and webp
.
svg
format is currently not supported. If you need to render svg
images, you can refer to Custom Element to implement it yourself.
For front-end built-in resources, please refer to static resource handling for configuration, or the image may not display correctly after deployment.
mode
Specifies the image cropping/scaling mode:
scaleToFill
(default): The image is stretched to fill the <image>
element without maintaining its aspect ratio.aspectFit
: Scales the image to maintain its aspect ratio, ensuring the entire image is visible.aspectFill
: Scales the image to maintain its aspect ratio, causing the shorter side to fill the <image>
element, which may crop part of the image.placeholder
Specifies the path to the placeholder image. The usage and limitations are the same as for the src
attribute.
blur-radius
Specifies the Gaussian blur radius for the image.
prefetch-width/prefetch-height
Allows initiating a request when the image has a width/height of 0. This is typically used when preloading images. It's recommended to set the sizes to match the actual layout width/height.
cap-insets
Specifies the 9patch image scaling area with four values representing the top, right, bottom, and left edges. Values must be specific numbers and do not support percentages or decimals.
Using cap-insets
does not require the original image to be a 9patch image.
cap-insets-scale
Works with cap-insets
to adjust the pixel positions when stretching the image.
loop-count
Specifies the number of times to play an animated image. The default is to loop indefinitely.
image-config
Android onlySpecifies the image data format. There are two options:
ARGB_8888
: Each pixel uses 32 bits of memory, including an alpha channel for transparency.RGB_565
: Each pixel uses 16 bits of memory, which reduces memory usage but loses transparency.This affects the actual memory size of the bitmap on Android.
For an image with a resolution of 1024x768, the memory size used would be calculated as: (1024 _ 768 _ pixel bits / 8) bytes.
auto-size
When set to true
and the <image>
element has no width or height, the size of the <image>
will be automatically adjusted to match the image's original dimensions after the image is successfully loaded, ensuring that the aspect ratio is maintained.
defer-src-invalidation
When set to true
, the <image>
will only clear the previously displayed image resource after a new image has successfully loaded.
The default behavior is to clear the image resource before starting a new load.
This can resolve flickering issues when the image src
is switched and reloaded. It is not recommended to enable this in scenarios where there is node reuse in views like lists.
autoplay
Specifies whether the animated image should start playing automatically once it is loaded. The default is to autoplay.
tint-color
Changes the color of all non-transparent pixels to the tint-color
specified. The value is a color.
Frontend can bind corresponding event callbacks to listen for runtime behaviors of the element, as shown below.
bindload
Triggered when the image request succeeds, outputting the image's width and height.
binderror
Triggered when the image request fails, outputting the error message and code.
Frontend can invoke component methods via the SelectorQuery API.
startAnimate
Restarts the animation. The animation progress and loop-count are both reset.
pauseAnimation
Pauses the animation, without resetting the loop-count.
resumeAnimation
Resumes the animation, without resetting the loop-count.
stopAnimation
Stops the animation, resetting the loop-count.
By implementing a URL redirection mechanism, developers can intercept specific image URLs and map them to specific resource paths. This ability is useful for the following scenarios:
This feature is implemented based on the MediaResourceFetcher
interface, with the core process divided into two stages:
Resource Type Detection (isLocalResource
)
Path Conversion (shouldRedirectUrl
)
The following example shows how to map a URL like http://localhost/xxx
to an app's built-in resource path:
Read the API reference of MediaResourceFetcher
for more details.
LCD tables only load in the browser