Class Lattice
A grid over an image for DrawImageLattice(Raster, Lattice, RectF, SamplingOptions?, Paint?), as Skia's lattice: the x and y divisions cut the image into columns and rows that alternate fixed and stretchable, starting with fixed, so a division at the image's left or top edge makes the first column or row stretchable. Fixed columns and rows keep their size when the destination is large enough and shrink in proportion when it is not; stretchable ones share what is left. A nine-patch is the lattice with two divisions each way.
public sealed record Lattice : IEquatable<Lattice>
- Inheritance
-
Lattice
- Implements
- Inherited Members
Examples
using (RasterCanvas<Rgba8> canvas = raster.CreateCanvas())
{
// An Android .9.png: its one-pixel border marks what stretches. The corners stay sharp at any size.
(Raster inner, Lattice lattice, Rect? padding) = NinePatch.FromAndroid(file);
canvas.DrawImageLattice(inner, lattice, new RectF(10, 10, 180, 48));
inner.Dispose();
// A nine-patch from a plain image: the 8-pixel corners keep their size, the edges and center stretch.
canvas.DrawImageNinePatch(frame, new Rect(8, 8, 8, 8), new RectF(10, 70, 120, 40));
// A lattice can leave cells out or fill them with a color: here the center becomes a flat panel.
LatticeCellKind[] kinds = [.. Enumerable.Repeat(LatticeCellKind.Default, 9)];
kinds[4] = LatticeCellKind.FixedColor;
RgbaF32[] colors = new RgbaF32[9];
colors[4] = new RgbaF32(0.95f, 0.95f, 0.9f, 1);
canvas.DrawImageLattice(frame, new Lattice([8, 16], [8, 16]) { CellKinds = kinds, CellColors = colors }, new RectF(150, 70, 140, 40));
}
Constructors
Lattice(ReadOnlySpan<int>, ReadOnlySpan<int>)
Initializes a new instance of the Lattice class.
public Lattice(ReadOnlySpan<int> xDivs, ReadOnlySpan<int> yDivs)
Parameters
xDivsReadOnlySpan<int>The x coordinates, in image pixels, between columns: increasing, inside the bounds' width.
yDivsReadOnlySpan<int>The y coordinates between rows.
Exceptions
- ArgumentException
The divisions are not increasing or a division is negative.
Properties
Bounds
Gets the part of the image the lattice covers, in its pixels; the whole image when null. Divisions are relative to the image, not to the bounds.
public Rect? Bounds { get; init; }
Property Value
- Rect?
Exceptions
- ArgumentException
The bounds are empty.
CellColors
Gets the colors of FixedColor cells, one per cell in the order of CellKinds.
public IReadOnlyList<RgbaF32>? CellColors { get; init; }
Property Value
CellCount
Gets the number of cells: (XDivs.Count + 1) · (YDivs.Count + 1).
public int CellCount { get; }
Property Value
CellKinds
Gets what each cell draws, row by row from the top, (XDivs.Count + 1) · (YDivs.Count + 1) of them; every cell draws its part of the image when null.
public IReadOnlyList<LatticeCellKind>? CellKinds { get; init; }
Property Value
XDivs
Gets the x divisions.
public IReadOnlyList<int> XDivs { get; }
Property Value
YDivs
Gets the y divisions.
public IReadOnlyList<int> YDivs { get; }
Property Value
Methods
Equals(Lattice?)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(Lattice? other)
Parameters
otherLatticeAn object to compare with this object.
Returns
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
NinePatch(Rect)
Creates the lattice of a nine-patch: the center stretches both ways, the edges one way, and the corners keep their size.
public static Lattice NinePatch(Rect center)
Parameters
centerRectThe stretchable center, in image pixels.
Returns
- Lattice
The lattice.
Exceptions
- ArgumentException
centeris empty or negative.