Merge pull request #2306 from NativeScript/issue-1425

Fix: Images have margin when added to Layout
This commit is contained in:
Rossen Hristov
2016-06-14 15:23:28 +03:00
committed by GitHub
4 changed files with 27 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

View File

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,25 @@ 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 imageWidth = 700;
let imageHeight = 50;
let density = utils.layout.getDisplayDensity();
let hostWidth = 320;
host.width = hostWidth / density;
host.height = hostWidth / density;
host.addChild(image);
let mainPage = helper.getCurrentPage();
mainPage.content = host;
TKUnit.waitUntilReady(() => host.isLoaded);
TKUnit.waitUntilReady(() => image.isLayoutValid);
let scale = hostWidth / imageWidth;
let expectedHeight = Math.round(imageHeight * scale);
TKUnit.assertEqual(image.getMeasuredWidth(), hostWidth, "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;