Skip to content

Commit

Permalink
Center images in their bounds when painting
Browse files Browse the repository at this point in the history
Eventually we'll plumb positionX and positionY out so that developers can
control them.
  • Loading branch information
abarth committed Aug 19, 2015
1 parent 232d718 commit d4417d1
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions sky/packages/sky/lib/painting/box_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,54 +236,59 @@ void paintImage({
sky.Image image,
sky.ColorFilter colorFilter,
fit: ImageFit.scaleDown,
repeat: ImageRepeat.noRepeat
repeat: ImageRepeat.noRepeat,
double positionX: 0.5,
double positionY: 0.5
}) {
Size bounds = rect.size;
Size imageSize = new Size(image.width.toDouble(), image.height.toDouble());
Size src;
Size dst;
Size sourceSize;
Size destinationSize;
switch(fit) {
case ImageFit.fill:
src = imageSize;
dst = bounds;
sourceSize = imageSize;
destinationSize = bounds;
break;
case ImageFit.contain:
src = imageSize;
if (bounds.width / bounds.height > src.width / src.height) {
dst = new Size(bounds.width, src.height * bounds.width / src.width);
sourceSize = imageSize;
if (bounds.width / bounds.height > sourceSize.width / sourceSize.height) {
destinationSize = new Size(sourceSize.width * bounds.height / sourceSize.height, bounds.height);
} else {
dst = new Size(src.width * bounds.height / src.height, bounds.height);
destinationSize = new Size(bounds.width, sourceSize.height * bounds.width / sourceSize.width);
}
break;
case ImageFit.cover:
if (bounds.width / bounds.height > imageSize.width / imageSize.height) {
src = new Size(imageSize.width, imageSize.width * bounds.height / bounds.width);
sourceSize = new Size(imageSize.width, imageSize.width * bounds.height / bounds.width);
} else {
src = new Size(imageSize.height * bounds.width / bounds.height, imageSize.height);
sourceSize = new Size(imageSize.height * bounds.width / bounds.height, imageSize.height);
}
dst = bounds;
destinationSize = bounds;
break;
case ImageFit.none:
src = new Size(math.min(imageSize.width, bounds.width),
sourceSize = new Size(math.min(imageSize.width, bounds.width),
math.min(imageSize.height, bounds.height));
dst = src;
destinationSize = sourceSize;
break;
case ImageFit.scaleDown:
src = imageSize;
dst = bounds;
if (src.height > dst.height) {
dst = new Size(src.width * dst.height / src.height, src.height);
sourceSize = imageSize;
destinationSize = bounds;
if (sourceSize.height > destinationSize.height) {
destinationSize = new Size(sourceSize.width * destinationSize.height / sourceSize.height, sourceSize.height);
}
if (src.width > dst.width) {
dst = new Size(dst.width, src.height * dst.width / src.width);
if (sourceSize.width > destinationSize.width) {
destinationSize = new Size(destinationSize.width, sourceSize.height * destinationSize.width / sourceSize.width);
}
break;
}
// TODO(abarth): Implement |repeat|.
Paint paint = new Paint();
if (colorFilter != null)
paint.setColorFilter(colorFilter);
canvas.drawImageRect(image, Point.origin & src, rect.topLeft & dst, paint);
double dx = math.max(0.0, bounds.width - destinationSize.width) * positionX;
double dy = math.max(0.0, bounds.height - destinationSize.height) * positionY;
Point destinationPosition = rect.topLeft + new Offset(dx, dy);
canvas.drawImageRect(image, Point.origin & sourceSize, destinationPosition & destinationSize, paint);
}

typedef void BackgroundImageChangeListener();
Expand Down

0 comments on commit d4417d1

Please sign in to comment.