ElementContainer, ElementWrapper, LayerElement, PixelMapElement, ShadowElement, ShapeElement, VectorElementpublic abstract class Element extends Object
When static images need to be displayed in an application, this class and its child classes can be used to draw shapes and images. These child classes can resolve issues in specific image use situations.
| 限定符和类型 | 类 | 说明 |
|---|---|---|
static class |
Element.ConstantState |
An abstract class of
Element used to store constant states and data shared by Elements
instances. |
static interface |
Element.OnBoundsChangeListener |
Provides a callback to listen for changes of element bounds.
|
static interface |
Element.OnChangeListener |
Provides a callback to listen for element changes.
|
static interface |
Element.OnLayoutDirectionChangeListener |
Provides a callback to listen for element layout direction changes.
|
protected static interface |
Element.OnRankChangeListener |
Provides a callback to listen for
Element rank change. |
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
ALPHA_DEFAULT |
Indicates the default alpha value.
|
static int |
ALPHA_MAX |
Indicates the maximum alpha value.
|
static int |
ALPHA_MIN |
Indicates the minimum alpha value.
|
static Rect |
DEFAULT_BOUNDS_RECT |
Indicates the default bounds in shape of rectangle.
|
static int |
MAX_RANK |
Indicates the maximum rank value.
|
static int |
MIN_RANK |
Indicates the minimum rank value.
|
| 构造器 | 说明 |
|---|---|
Element() |
Default constructor used to create an
Element object. |
Element(Object source) |
A constructor used to create an
Element object based on a specified Object. |
Element(Resource resource,
boolean decodeImmediately) |
A constructor used to create an
Element object based on a specific
Resource. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
clearColorMatrix() |
Clears the color matrix of this
Element object. |
void |
drawToCanvas(Canvas canvas) |
Draws an
Element object on the canvas. |
int |
getAlpha() |
Obtains the alpha value of an element object.
|
Rect |
getBounds() |
Obtains the bounds rectangle of an
Element object. |
Element.OnChangeListener |
getCallback() |
Obtains the listener for element attribute changes
|
ColorMatrix |
getColorMatrix() |
Obtains the color matrix of this
Element object. |
Element |
getCurrentElement() |
Obtains the
Element object in use. |
int |
getHeight() |
Obtains the height of this
Element object. |
Component.LayoutDirection |
getLayoutDirection() |
Obtains the resolved layout direction for this
Element. |
int |
getMinHeight() |
Obtains the minimum
Element height suitable for the layout. |
int |
getMinWidth() |
Obtains the minimum
Element width suitable for the layout. |
Optional<Rect> |
getPadding() |
Obtains the padding of this
Element. |
int |
getRank() |
Obtains the rank of this
Element. |
BlendMode |
getStateColorMode() |
Obtains the
BlendMode instance of this Element. |
boolean |
getVisible() |
已过时。
This change is effective since API version 8. Replaced by
isVisible() |
int |
getWidth() |
Obtains the width of this
Element object. |
boolean |
isInheritDirection() |
Checks whether an element inherits the layout of the component that uses the element as the background
when
layoutDirection of the component is RTL. |
boolean |
isStateful() |
Checks whether the state of an
Element object is changed based on the component state. |
boolean |
isVisible() |
Checks whether this
Element is visible. |
void |
release() |
已过时。
This change is effective since API version 8. Method is no longer used.
|
void |
setAlpha(int alpha) |
Sets the alpha value of an element object.
|
void |
setBounds(int left,
int top,
int right,
int bottom) |
Sets the bounds of an
Element object using a Rect object. |
void |
setBounds(Rect bounds) |
|
void |
setCallback(Element.OnChangeListener listener) |
Sets a listener for element attribute changes, such as the attributes set by
setVisible(boolean, boolean), setBounds(int, int, int, int), and setColorMatrix(ohos.agp.render.ColorMatrix). |
void |
setColorMatrix(ColorMatrix matrix) |
Sets the color matrix for this
Element object. |
void |
setInheritDirection(boolean followRTL) |
Sets whether the element, which is used by a component as the background, inherits the component's layout
when
layoutDirection of the component is RTL (that is, mirror display). |
boolean |
setLayoutDirection(Component.LayoutDirection layoutDirection) |
Sets the layout direction for this
Element. |
protected void |
setOnBoundsChangeListener(Element.OnBoundsChangeListener listener) |
Listens for bounds changes of this
Element. |
protected void |
setOnLayoutDirectionChangeListener(Element.OnLayoutDirectionChangeListener listener) |
Listens for layout direction changes of this
Element. |
protected void |
setOnRankChangeListener(Element.OnRankChangeListener listener) |
Listens for rank changes of this
Element. |
boolean |
setRank(int rank) |
Set the rank value for the
Element. |
void |
setStateColor(StateColor color) |
Sets
StateColor to Element. |
void |
setStateColorList(int[][] colorState,
int[] colors) |
已过时。
Please use
setStateColor(ohos.agp.colors.StateColor) |
void |
setStateColorMode(BlendMode mode) |
Sets a
BlendMode object for this Element object. |
boolean |
setVisible(boolean visible,
boolean restart) |
This method doesn't change the visibility of an
Element. |
void |
skipAnimation() |
Skips the animation of this
Element object. |
public static final int ALPHA_DEFAULT
public static final int ALPHA_MAX
public static final int ALPHA_MIN
public static final Rect DEFAULT_BOUNDS_RECT
public static final int MAX_RANK
public static final int MIN_RANK
public Element()
Element object.public Element(Object source)
Element object based on a specified Object.source - Indicates the resources used for native initialization.public Element(Resource resource, boolean decodeImmediately)
Element object based on a specific
Resource.
This method is mostly used by PixelMapElement created from Resource.
resource - Indicates the resource to decode.decodeImmediately - Specifies whether to decode the resource immediately. The value true means to
decode the resource immediately
(to start decoding upon the creation), and false means the opposite.public void setAlpha(int alpha)
Value 0 indicates complete transparency, and value 255 indicates complete opacity.
alpha - Indicates the alpha value.getAlpha()public int getAlpha()
Value 0 indicates complete transparency, and value 255 indicates complete opacity. This
method is implemented by Element child classes. The default alpha value is 255.
setAlpha(int)public final boolean setRank(int rank)
Element.
This change an element appearance depending on the rank.
rank - Indicates the rank to set, ranging from 0 to 10000.true if the change is applied and OnRankChangeListener is set;
returns false otherwise.public final int getRank()
Element.0 to 10000.setRank(int)protected void setOnRankChangeListener(Element.OnRankChangeListener listener)
Element.listener - Indicates the listener to be used.Element.OnRankChangeListenerpublic void drawToCanvas(Canvas canvas)
Element object on the canvas.canvas - Indicates the canvas on which to draw the element.public boolean setVisible(boolean visible,
boolean restart)
Element.
Mostly used for Elements which supports animation to pause/resume their animation.visible - Specifies whether the Element object is playing animation or not.
Value true indicates that the animation will be resumed
and false indicates that the animation will be paused.restart - Specifies whether to restart the animation of the Element object.
Value true indicates that the animation will be restarted from the first frame
and false indicates the animation will be resumed from the most recent frame.true if the latest visible state is different from the previous state;
returns false otherwise.isVisible()@Deprecated public boolean getVisible()
isVisible()Element object.Element object.setVisible(boolean, boolean)public boolean isVisible()
Element is visible.true if the Element is visible; returns false otherwise.setVisible(boolean, boolean)public boolean isStateful()
Element object is changed based on the component state.true if the Element object state is changed; returns false otherwise.public Element getCurrentElement()
Element object in use.Element object in use in normal cases; returns the child Element object in
use if an ElementContainer is used.@Deprecated public void release()
If a component is using the Element object, a runtime exception is thrown.
When a ElementContainer object is released, all child Element objects are released.
public void setBounds(int left,
int top,
int right,
int bottom)
Element object using a Rect object.
NOTE: Bound values could be ignored by some components and Element object can be adjusted to fill
container for it.
left - Indicates the left bound of the Element object, in pixels.top - Indicates the top bound of the Element object, in pixels.right - Indicates the right bound of the Element object, in pixels.bottom - Indicates the bottom bound of the Element object, in pixels.getBounds(),
setBounds(Rect)public void setBounds(Rect bounds)
Element object using a Rect object.
NOTE: bounds value could be ignored by some components and Element object can be adjusted to fill
container for it.
bounds - Indicates the bounds.getBounds(),
setBounds(int, int, int, int)public final Rect getBounds()
Element object.
NOTE: Bounds value could be ignored by some components and Element object can be adjusted to fill
container for it.
setBounds(Rect),
setBounds(int, int, int, int)protected void setOnBoundsChangeListener(Element.OnBoundsChangeListener listener)
Element.listener - Indicates the listener to be used. (Bounds changes are the result of
setBounds(ohos.agp.utils.Rect).)getBounds(),
setBounds(Rect),
setBounds(int, int, int, int)public int getWidth()
Element object.-1 if the element width was not set.public int getHeight()
Element object.-1 if the element height was not set.public int getMinWidth()
Element width suitable for the layout. If the element is used as
the background of a component,the width of the component must be greater than or equal to the obtained width.0 if the element width was not set.public int getMinHeight()
Element height suitable for the layout. If the element is used as
the background of a component,the height of the component must be greater than or equal to the obtained height.0 if the element height was not set.public void setColorMatrix(ColorMatrix matrix)
Element object.
Element colors and the alpha value can be changed by using the color matrix.
matrix - Indicates the color matrix used as a filter.public ColorMatrix getColorMatrix()
Element object.public void clearColorMatrix()
Element object.@Deprecated public void setStateColorList(int[][] colorState, int[] colors)
setStateColor(ohos.agp.colors.StateColor)Element states and their corresponding colors.
The length of the state list must equal that of the color list.
colorState - Indicates a list of element states.
The value can be obtained from the ComponentState class.colors - Indicates a list of colors corresponding to the element states.
The value can be obtained from the Color class.ComponentState,
Colorpublic void setStateColor(StateColor color)
StateColor to Element.
Note: When this function is called content of StateColor object is copied and any later changes
of this object will not affect Element. If you need to apply these changes, call this function again.color - Indicates a StateColor to set.public void setStateColorMode(BlendMode mode)
BlendMode object for this Element object.
This method applies the colors listed in BlendMode to this Element object.
mode - Indicates the BlendMode object to set.public BlendMode getStateColorMode()
BlendMode instance of this Element.BlendMode instance.public void setCallback(Element.OnChangeListener listener)
setVisible(boolean, boolean), setBounds(int, int, int, int), and setColorMatrix(ohos.agp.render.ColorMatrix).listener - Indicates the listener to set.public Element.OnChangeListener getCallback()
null if the listener was not set.public void skipAnimation()
Element object.public final boolean setLayoutDirection(Component.LayoutDirection layoutDirection)
Element.layoutDirection - Indicates the layout direction to set.true if the Element needs invalidation (re-drawing); returns
false otherwise.IllegalArgumentException - Throws this exception if layoutDirection is not
Component.LayoutDirection#LTR or Component.LayoutDirection#RTL.getLayoutDirection(),
Element.OnLayoutDirectionChangeListener,
Component.LayoutDirectionpublic Component.LayoutDirection getLayoutDirection()
Element.setLayoutDirection(LayoutDirection),
Element.OnLayoutDirectionChangeListener,
Component.LayoutDirectionprotected void setOnLayoutDirectionChangeListener(Element.OnLayoutDirectionChangeListener listener)
Element.listener - Indicates the listener to be used. (Layout direction changes are the result of
setLayoutDirection(ohos.agp.components.Component.LayoutDirection).)getLayoutDirection(),
setLayoutDirection(LayoutDirection),
Component.LayoutDirectionpublic void setInheritDirection(boolean followRTL)
layoutDirection of the component is RTL (that is, mirror display).followRTL - Indicates whether the element inherits the component's layout when layoutDirection of
the component is RTL. Value true indicates that the element inherits the component's
layout;
value false indicates the opposite.Component.LayoutDirectionpublic boolean isInheritDirection()
layoutDirection of the component is RTL.true if the element inherits the layout of the component that uses the element
as the background; returns false otherwise.Component.LayoutDirectionpublic Optional<Rect> getPadding()
Element.Optional object with the padding inside of Rect if the Element has
padding.setBounds(Rect),
getBounds()