Fix: Images have margin when added to Layout

Resolves #1425
This commit is contained in:
Rossen Hristov
2016-06-14 10:39:44 +03:00
parent 423066ac78
commit 4e678a06ff
4 changed files with 25 additions and 2 deletions

2
.gitignore vendored
View File

@ -42,3 +42,5 @@ tns-core-modules.es6.d.ts
tests/platforms/
tests/lib/
*.log
.DS_Store

Binary file not shown.

After

Width:  |  Height:  |  Size: 808 B

View File

@ -3,6 +3,7 @@ import {StackLayout} from "ui/layouts/stack-layout";
import {GridLayout} from "ui/layouts/grid-layout";
import {isIOS} from "platform";
import {PropertyChangeData} from "data/observable";
import utils = require("utils/utils");
// import {target} from "../../TKUnit";
@ -334,3 +335,23 @@ export var test_SettingImageSourceWhenSizedToContentShouldInvalidate = ios(() =>
TKUnit.assertTrue(called, "image.requestLayout should be called.");
});
export var test_DimensionsAreRoundedAfterScale = function() {
let host = new StackLayout();
let image = new Image();
image.src = "~/ui/image/700x50.png";
let density = utils.layout.getDisplayDensity();
let limit = 414;
host.width = limit / density;
host.height = limit / density;
host.addChild(image);
let mainPage = helper.getCurrentPage();
mainPage.content = host;
TKUnit.waitUntilReady(() => host.isLoaded);
TKUnit.waitUntilReady(() => image.isLayoutValid);
let scale = limit / 700;
let expectedHeight = Math.round(50 * scale);
TKUnit.assertEqual(image.getMeasuredWidth(), limit, "Actual width is different from expected width.");
TKUnit.assertEqual(image.getMeasuredHeight(), expectedHeight, "Actual height is different from expected height.");
};

View File

@ -83,8 +83,8 @@ export class Image extends imageCommon.Image {
if (nativeWidth !== 0 && nativeHeight !== 0 && (finiteWidth || finiteHeight)) {
var scale = Image.computeScaleFactor(width, height, finiteWidth, finiteHeight, nativeWidth, nativeHeight, this.stretch);
var resultW = Math.floor(nativeWidth * scale.width);
var resultH = Math.floor(nativeHeight * scale.height);
var resultW = Math.round(nativeWidth * scale.width);
var resultH = Math.round(nativeHeight * scale.height);
measureWidth = finiteWidth ? Math.min(resultW, width) : resultW;
measureHeight = finiteHeight ? Math.min(resultH, height) : resultH;