Commit 2e2aa748 authored by Adam Barth's avatar Adam Barth

Merge pull request #484 from abarth/image_alignment

Add `alignment` to image widgets
parents 5734821f d234c65d
...@@ -582,8 +582,8 @@ void paintImage({ ...@@ -582,8 +582,8 @@ void paintImage({
ImageFit fit, ImageFit fit,
repeat: ImageRepeat.noRepeat, repeat: ImageRepeat.noRepeat,
Rect centerSlice, Rect centerSlice,
double positionX: 0.5, double alignX,
double positionY: 0.5 double alignY
}) { }) {
Size outputSize = rect.size; Size outputSize = rect.size;
Size inputSize = new Size(image.width.toDouble(), image.height.toDouble()); Size inputSize = new Size(image.width.toDouble(), image.height.toDouble());
...@@ -644,8 +644,8 @@ void paintImage({ ...@@ -644,8 +644,8 @@ void paintImage({
Paint paint = new Paint()..isAntiAlias = false; Paint paint = new Paint()..isAntiAlias = false;
if (colorFilter != null) if (colorFilter != null)
paint.colorFilter = colorFilter; paint.colorFilter = colorFilter;
double dx = (outputSize.width - destinationSize.width) * positionX; double dx = (outputSize.width - destinationSize.width) * (alignX ?? 0.5);
double dy = (outputSize.height - destinationSize.height) * positionY; double dy = (outputSize.height - destinationSize.height) * (alignY ?? 0.5);
Point destinationPosition = rect.topLeft + new Offset(dx, dy); Point destinationPosition = rect.topLeft + new Offset(dx, dy);
Rect destinationRect = destinationPosition & destinationSize; Rect destinationRect = destinationPosition & destinationSize;
if (centerSlice == null) if (centerSlice == null)
......
...@@ -24,6 +24,7 @@ class RenderImage extends RenderBox { ...@@ -24,6 +24,7 @@ class RenderImage extends RenderBox {
double height, double height,
ColorFilter colorFilter, ColorFilter colorFilter,
ImageFit fit, ImageFit fit,
FractionalOffset alignment,
repeat: ImageRepeat.noRepeat, repeat: ImageRepeat.noRepeat,
Rect centerSlice Rect centerSlice
}) : _image = image, }) : _image = image,
...@@ -31,6 +32,7 @@ class RenderImage extends RenderBox { ...@@ -31,6 +32,7 @@ class RenderImage extends RenderBox {
_height = height, _height = height,
_colorFilter = colorFilter, _colorFilter = colorFilter,
_fit = fit, _fit = fit,
_alignment = alignment,
_repeat = repeat, _repeat = repeat,
_centerSlice = centerSlice; _centerSlice = centerSlice;
...@@ -86,6 +88,16 @@ class RenderImage extends RenderBox { ...@@ -86,6 +88,16 @@ class RenderImage extends RenderBox {
markNeedsPaint(); markNeedsPaint();
} }
/// How to align the image within its bounds.
FractionalOffset get alignment => _alignment;
FractionalOffset _alignment;
void set alignment (FractionalOffset value) {
if (value == _alignment)
return;
_alignment = value;
markNeedsPaint();
}
/// Not yet implemented. /// Not yet implemented.
ImageRepeat get repeat => _repeat; ImageRepeat get repeat => _repeat;
ImageRepeat _repeat; ImageRepeat _repeat;
...@@ -194,6 +206,8 @@ class RenderImage extends RenderBox { ...@@ -194,6 +206,8 @@ class RenderImage extends RenderBox {
image: _image, image: _image,
colorFilter: _colorFilter, colorFilter: _colorFilter,
fit: _fit, fit: _fit,
alignX: _alignment?.x,
alignY: _alignment?.y,
centerSlice: _centerSlice, centerSlice: _centerSlice,
repeat: _repeat repeat: _repeat
); );
......
...@@ -1021,6 +1021,7 @@ class Image extends LeafRenderObjectWidget { ...@@ -1021,6 +1021,7 @@ class Image extends LeafRenderObjectWidget {
this.height, this.height,
this.colorFilter, this.colorFilter,
this.fit, this.fit,
this.alignment,
this.repeat: ImageRepeat.noRepeat, this.repeat: ImageRepeat.noRepeat,
this.centerSlice this.centerSlice
}) : super(key: key); }) : super(key: key);
...@@ -1030,6 +1031,7 @@ class Image extends LeafRenderObjectWidget { ...@@ -1030,6 +1031,7 @@ class Image extends LeafRenderObjectWidget {
final double height; final double height;
final ColorFilter colorFilter; final ColorFilter colorFilter;
final ImageFit fit; final ImageFit fit;
final FractionalOffset alignment;
final ImageRepeat repeat; final ImageRepeat repeat;
final Rect centerSlice; final Rect centerSlice;
...@@ -1039,6 +1041,7 @@ class Image extends LeafRenderObjectWidget { ...@@ -1039,6 +1041,7 @@ class Image extends LeafRenderObjectWidget {
height: height, height: height,
colorFilter: colorFilter, colorFilter: colorFilter,
fit: fit, fit: fit,
alignment: alignment,
repeat: repeat, repeat: repeat,
centerSlice: centerSlice); centerSlice: centerSlice);
...@@ -1047,6 +1050,7 @@ class Image extends LeafRenderObjectWidget { ...@@ -1047,6 +1050,7 @@ class Image extends LeafRenderObjectWidget {
renderObject.width = width; renderObject.width = width;
renderObject.height = height; renderObject.height = height;
renderObject.colorFilter = colorFilter; renderObject.colorFilter = colorFilter;
renderObject.alignment = alignment;
renderObject.fit = fit; renderObject.fit = fit;
renderObject.repeat = repeat; renderObject.repeat = repeat;
renderObject.centerSlice = centerSlice; renderObject.centerSlice = centerSlice;
...@@ -1061,6 +1065,7 @@ class ImageListener extends StatefulComponent { ...@@ -1061,6 +1065,7 @@ class ImageListener extends StatefulComponent {
this.height, this.height,
this.colorFilter, this.colorFilter,
this.fit, this.fit,
this.alignment,
this.repeat: ImageRepeat.noRepeat, this.repeat: ImageRepeat.noRepeat,
this.centerSlice this.centerSlice
}) : super(key: key) { }) : super(key: key) {
...@@ -1072,6 +1077,7 @@ class ImageListener extends StatefulComponent { ...@@ -1072,6 +1077,7 @@ class ImageListener extends StatefulComponent {
final double height; final double height;
final ColorFilter colorFilter; final ColorFilter colorFilter;
final ImageFit fit; final ImageFit fit;
final FractionalOffset alignment;
final ImageRepeat repeat; final ImageRepeat repeat;
final Rect centerSlice; final Rect centerSlice;
...@@ -1111,6 +1117,7 @@ class _ImageListenerState extends State<ImageListener> { ...@@ -1111,6 +1117,7 @@ class _ImageListenerState extends State<ImageListener> {
height: config.height, height: config.height,
colorFilter: config.colorFilter, colorFilter: config.colorFilter,
fit: config.fit, fit: config.fit,
alignment: config.alignment,
repeat: config.repeat, repeat: config.repeat,
centerSlice: config.centerSlice centerSlice: config.centerSlice
); );
...@@ -1125,6 +1132,7 @@ class NetworkImage extends StatelessComponent { ...@@ -1125,6 +1132,7 @@ class NetworkImage extends StatelessComponent {
this.height, this.height,
this.colorFilter, this.colorFilter,
this.fit, this.fit,
this.alignment,
this.repeat: ImageRepeat.noRepeat, this.repeat: ImageRepeat.noRepeat,
this.centerSlice this.centerSlice
}) : super(key: key); }) : super(key: key);
...@@ -1134,6 +1142,7 @@ class NetworkImage extends StatelessComponent { ...@@ -1134,6 +1142,7 @@ class NetworkImage extends StatelessComponent {
final double height; final double height;
final ColorFilter colorFilter; final ColorFilter colorFilter;
final ImageFit fit; final ImageFit fit;
final FractionalOffset alignment;
final ImageRepeat repeat; final ImageRepeat repeat;
final Rect centerSlice; final Rect centerSlice;
...@@ -1144,6 +1153,7 @@ class NetworkImage extends StatelessComponent { ...@@ -1144,6 +1153,7 @@ class NetworkImage extends StatelessComponent {
height: height, height: height,
colorFilter: colorFilter, colorFilter: colorFilter,
fit: fit, fit: fit,
alignment: alignment,
repeat: repeat, repeat: repeat,
centerSlice: centerSlice centerSlice: centerSlice
); );
...@@ -1178,6 +1188,7 @@ class RawImage extends StatelessComponent { ...@@ -1178,6 +1188,7 @@ class RawImage extends StatelessComponent {
this.height, this.height,
this.colorFilter, this.colorFilter,
this.fit, this.fit,
this.alignment,
this.repeat: ImageRepeat.noRepeat, this.repeat: ImageRepeat.noRepeat,
this.centerSlice this.centerSlice
}) : super(key: key); }) : super(key: key);
...@@ -1187,6 +1198,7 @@ class RawImage extends StatelessComponent { ...@@ -1187,6 +1198,7 @@ class RawImage extends StatelessComponent {
final double height; final double height;
final ColorFilter colorFilter; final ColorFilter colorFilter;
final ImageFit fit; final ImageFit fit;
final FractionalOffset alignment;
final ImageRepeat repeat; final ImageRepeat repeat;
final Rect centerSlice; final Rect centerSlice;
...@@ -1198,6 +1210,7 @@ class RawImage extends StatelessComponent { ...@@ -1198,6 +1210,7 @@ class RawImage extends StatelessComponent {
height: height, height: height,
colorFilter: colorFilter, colorFilter: colorFilter,
fit: fit, fit: fit,
alignment: alignment,
repeat: repeat, repeat: repeat,
centerSlice: centerSlice centerSlice: centerSlice
); );
...@@ -1213,6 +1226,7 @@ class AssetImage extends StatelessComponent { ...@@ -1213,6 +1226,7 @@ class AssetImage extends StatelessComponent {
this.height, this.height,
this.colorFilter, this.colorFilter,
this.fit, this.fit,
this.alignment,
this.repeat: ImageRepeat.noRepeat, this.repeat: ImageRepeat.noRepeat,
this.centerSlice this.centerSlice
}) : super(key: key); }) : super(key: key);
...@@ -1223,6 +1237,7 @@ class AssetImage extends StatelessComponent { ...@@ -1223,6 +1237,7 @@ class AssetImage extends StatelessComponent {
final double height; final double height;
final ColorFilter colorFilter; final ColorFilter colorFilter;
final ImageFit fit; final ImageFit fit;
final FractionalOffset alignment;
final ImageRepeat repeat; final ImageRepeat repeat;
final Rect centerSlice; final Rect centerSlice;
...@@ -1233,6 +1248,7 @@ class AssetImage extends StatelessComponent { ...@@ -1233,6 +1248,7 @@ class AssetImage extends StatelessComponent {
height: height, height: height,
colorFilter: colorFilter, colorFilter: colorFilter,
fit: fit, fit: fit,
alignment: alignment,
repeat: repeat, repeat: repeat,
centerSlice: centerSlice centerSlice: centerSlice
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment