Class NinePatch
An image with its Lattice, as Android's nine-patch (.9.png) files hold them.
public sealed record NinePatch : IEquatable<NinePatch>
- Inheritance
-
NinePatch
- 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
NinePatch(Raster, Lattice, Rect?)
An image with its Lattice, as Android's nine-patch (.9.png) files hold them.
public NinePatch(Raster Image, Lattice Lattice, Rect? Padding)
Parameters
ImageRasterThe image without its marker border.
LatticeLatticeThe stretchable columns and rows the markers set.
PaddingRect?The content area the right and bottom markers set, in the image's pixels, or null when the file has none.
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));
}
Properties
Image
The image without its marker border.
public Raster Image { get; init; }
Property Value
Lattice
The stretchable columns and rows the markers set.
public Lattice Lattice { get; init; }
Property Value
Padding
The content area the right and bottom markers set, in the image's pixels, or null when the file has none.
public Rect? Padding { get; init; }
Property Value
- Rect?
Methods
FromAndroid(Raster)
Reads an Android nine-patch: the image has a one-pixel border whose opaque black pixels mark, on the top edge, the stretchable columns, on the left edge the stretchable rows, and on the right and bottom edges the content area. Other border pixels must be fully transparent, as Android requires.
public static NinePatch FromAndroid(Raster raster)
Parameters
rasterRasterThe whole .9.png image, border included; its first frame is read.
Returns
- NinePatch
The image inside the border, in the raster's pixel type, with its lattice and padding.
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));
}
Exceptions
- ArgumentException
The image is smaller than 3×3, or a border pixel is neither opaque black nor transparent.