@@ -236,54 +236,55 @@ void paintImage({
236
236
sky.Image image,
237
237
sky.ColorFilter colorFilter,
238
238
fit: ImageFit .scaleDown,
239
- repeat: ImageRepeat .noRepeat
239
+ repeat: ImageRepeat .noRepeat,
240
+ double positionX: 0.5 ,
241
+ double positionY: 0.5
240
242
}) {
241
243
Size bounds = rect.size;
242
244
Size imageSize = new Size (image.width.toDouble (), image.height.toDouble ());
243
- Size src ;
244
- Size dst ;
245
+ Size sourceSize ;
246
+ Size destinationSize ;
245
247
switch (fit) {
246
248
case ImageFit .fill:
247
- src = imageSize;
248
- dst = bounds;
249
+ sourceSize = imageSize;
250
+ destinationSize = bounds;
249
251
break ;
250
252
case ImageFit .contain:
251
- src = imageSize;
252
- if (bounds.width / bounds.height > src.width / src.height) {
253
- dst = new Size (bounds.width, src.height * bounds.width / src.width);
254
- } else {
255
- dst = new Size (src.width * bounds.height / src.height, bounds.height);
256
- }
253
+ sourceSize = imageSize;
254
+ if (bounds.width / bounds.height > sourceSize.width / sourceSize.height)
255
+ destinationSize = new Size (sourceSize.width * bounds.height / sourceSize.height, bounds.height);
256
+ else
257
+ destinationSize = new Size (bounds.width, sourceSize.height * bounds.width / sourceSize.width);
257
258
break ;
258
259
case ImageFit .cover:
259
- if (bounds.width / bounds.height > imageSize.width / imageSize.height) {
260
- src = new Size (imageSize.width, imageSize.width * bounds.height / bounds.width);
261
- } else {
262
- src = new Size (imageSize.height * bounds.width / bounds.height, imageSize.height);
263
- }
264
- dst = bounds;
260
+ if (bounds.width / bounds.height > imageSize.width / imageSize.height)
261
+ sourceSize = new Size (imageSize.width, imageSize.width * bounds.height / bounds.width);
262
+ else
263
+ sourceSize = new Size (imageSize.height * bounds.width / bounds.height, imageSize.height);
264
+ destinationSize = bounds;
265
265
break ;
266
266
case ImageFit .none:
267
- src = new Size (math.min (imageSize.width, bounds.width),
268
- math.min (imageSize.height, bounds.height));
269
- dst = src ;
267
+ sourceSize = new Size (math.min (imageSize.width, bounds.width),
268
+ math.min (imageSize.height, bounds.height));
269
+ destinationSize = sourceSize ;
270
270
break ;
271
271
case ImageFit .scaleDown:
272
- src = imageSize;
273
- dst = bounds;
274
- if (src.height > dst.height) {
275
- dst = new Size (src.width * dst.height / src.height, src.height);
276
- }
277
- if (src.width > dst.width) {
278
- dst = new Size (dst.width, src.height * dst.width / src.width);
279
- }
272
+ sourceSize = imageSize;
273
+ destinationSize = bounds;
274
+ if (sourceSize.height > destinationSize.height)
275
+ destinationSize = new Size (sourceSize.width * destinationSize.height / sourceSize.height, sourceSize.height);
276
+ if (sourceSize.width > destinationSize.width)
277
+ destinationSize = new Size (destinationSize.width, sourceSize.height * destinationSize.width / sourceSize.width);
280
278
break ;
281
279
}
282
280
// TODO(abarth): Implement |repeat|.
283
281
Paint paint = new Paint ();
284
282
if (colorFilter != null )
285
283
paint.setColorFilter (colorFilter);
286
- canvas.drawImageRect (image, Point .origin & src, rect.topLeft & dst, paint);
284
+ double dx = (bounds.width - destinationSize.width) * positionX;
285
+ double dy = (bounds.height - destinationSize.height) * positionY;
286
+ Point destinationPosition = rect.topLeft + new Offset (dx, dy);
287
+ canvas.drawImageRect (image, Point .origin & sourceSize, destinationPosition & destinationSize, paint);
287
288
}
288
289
289
290
typedef void BackgroundImageChangeListener ();
0 commit comments