Skip to content

Commit 66c2b91

Browse files
committed
feat(image): new noRatioEnforce property
1 parent 792ca67 commit 66c2b91

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

packages/image/platforms/android/java/com/nativescript/image/DraweeView.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class DraweeView extends SimpleDraweeView {
3838
public int imageWidth = 0;
3939
public int imageHeight = 0;
4040
public boolean isUsingOutlineProvider = false;
41+
public boolean noRatioEnforce = false;
4142
private static Paint clipPaint;
4243

4344
private boolean mLegacyVisibilityHandlingEnabled = false;
@@ -166,10 +167,11 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
166167
int width = MeasureSpec.getSize(widthMeasureSpec);
167168
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
168169
int height = MeasureSpec.getSize(heightMeasureSpec);
170+
171+
boolean finiteWidth = widthMode == android.view.View.MeasureSpec.EXACTLY;
172+
boolean finiteHeight = heightMode == android.view.View.MeasureSpec.EXACTLY;
169173
float aspectRatio = this.getAspectRatio();
170-
if (aspectRatio > 0) {
171-
boolean finiteWidth = widthMode == android.view.View.MeasureSpec.EXACTLY;
172-
boolean finiteHeight = heightMode == android.view.View.MeasureSpec.EXACTLY;
174+
if (aspectRatio > 0 && !this.noRatioEnforce) {
173175
Object scaleType = getHierarchy().getActualImageScaleType();
174176
if (scaleType instanceof AbstractScaleType) {
175177
final float rotation = ((AbstractScaleType)scaleType).getImageRotation();
@@ -195,6 +197,13 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
195197
}
196198
}
197199
}
200+
} else {
201+
if (!finiteWidth && finiteHeight) {
202+
widthMeasureSpec = android.view.View.MeasureSpec.makeMeasureSpec.makeMeasureSpec(0, android.view.View.MeasureSpec.AT_MOST);
203+
}
204+
else if (!finiteHeight && finiteWidth) {
205+
heightMeasureSpec = android.view.View.MeasureSpec.makeMeasureSpec.makeMeasureSpec(0, android.view.View.MeasureSpec.AT_MOST);
206+
}
198207
}
199208
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
200209
}

src/image/index-common.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ export const tintColorProperty = new Property<ImageBase, Color>({ name: 'tintCol
186186
export const alwaysFadeProperty = new Property<ImageBase, boolean>({ name: 'alwaysFade', valueConverter: booleanConverter, defaultValue: false });
187187
export const fadeDurationProperty = new Property<ImageBase, number>({ name: 'fadeDuration', valueConverter: (v) => parseFloat(v) });
188188
export const noCacheProperty = new Property<ImageBase, boolean>({ name: 'noCache', defaultValue: false, valueConverter: booleanConverter });
189+
export const noRatioEnforceProperty = new Property<ImageBase, boolean>({ name: 'noRatioEnforce', affectsLayout: true, defaultValue: false, valueConverter: booleanConverter });
189190
export const roundTopLeftRadiusProperty = new Property<ImageBase, CoreTypes.LengthType>({
190191
name: 'roundTopLeftRadius',
191192
defaultValue: 0,
@@ -300,6 +301,7 @@ export abstract class ImageBase extends View {
300301
public loadMode: 'sync' | 'async';
301302
public alwaysFade: boolean;
302303
public noCache: boolean;
304+
public noRatioEnforce: boolean;
303305
public tintColor: Color;
304306
headers: Record<string, string>;
305307

@@ -432,6 +434,7 @@ noCacheProperty.register(ImageBase);
432434
clipToBoundsProperty.register(ImageBase);
433435
animatedImageViewProperty.register(ImageBase);
434436
loadModeProperty.register(ImageBase);
437+
noRatioEnforceProperty.register(ImageBase);
435438
// roundRadiusProperty.register(ImageBase as any);
436439

437440
// ImageBase.blendingModeProperty.register(ImageBase);

src/image/index.android.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
imageRotationProperty,
2424
lowerResSrcProperty,
2525
needRequestImage,
26+
noRatioEnforceProperty,
2627
placeholderImageUriProperty,
2728
progressBarColorProperty,
2829
roundAsCircleProperty,
@@ -587,6 +588,10 @@ export class Img extends ImageBase {
587588
this.nativeViewProtected.setClipToOutline(value?.hasBorderRadius());
588589
}
589590

591+
[noRatioEnforceProperty.setNative](value: boolean) {
592+
this.nativeViewProtected.noRatioEnforce = value;
593+
}
594+
590595
// [ImageBase.blendingModeProperty.setNative](value: string) {
591596
// switch (value) {
592597
// case 'multiply':
@@ -627,7 +632,7 @@ export class Img extends ImageBase {
627632
// if (identifier >= 0 && isVectorDrawable(this._context, identifier)) {
628633
// drawable = getBitmapFromVectorDrawable(this._context, identifier);
629634
// }
630-
// } else
635+
// } else
631636
if (Utils.isFontIconURI(src)) {
632637
const fontIconCode = src.split('//')[1];
633638
if (fontIconCode !== undefined) {

src/image/index.ios.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ export class Img extends ImageBase {
340340
// if (Trace.isEnabled()) {
341341
// CLog(CLogTypes.info, 'onMeasure', this.src, widthMeasureSpec, heightMeasureSpec, width, height, this.aspectRatio, image && image.imageOrientation);
342342
// }
343-
if (image || this.aspectRatio > 0) {
343+
if (image || this.aspectRatio > 0 || !this.noRatioEnforce) {
344344
const nativeWidth = image ? layout.toDevicePixels(image.size.width) : 0;
345345
const nativeHeight = image ? layout.toDevicePixels(image.size.height) : 0;
346346
const imgRatio = nativeWidth / nativeHeight;
@@ -365,6 +365,12 @@ export class Img extends ImageBase {
365365
if (Trace.isEnabled()) {
366366
CLog(CLogTypes.info, 'onMeasure', this.src, this.aspectRatio, finiteWidth, finiteHeight, width, height, nativeWidth, nativeHeight, widthMeasureSpec, heightMeasureSpec);
367367
}
368+
} else {
369+
if (!finiteWidth && finiteHeight) {
370+
widthMeasureSpec = layout.makeMeasureSpec(0, layout.AT_MOST);
371+
} else if (!finiteHeight && finiteWidth) {
372+
heightMeasureSpec = layout.makeMeasureSpec(0, layout.AT_MOST);
373+
}
368374
}
369375
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
370376
}

src/image/typings/android.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ declare namespace com {
55
export namespace nativescript {
66
export namespace image {
77
class DraweeView extends facebook.drawee.view.SimpleDraweeView {
8+
noRatioEnforce: boolean;
89
imageWidth: number;
910
imageHeight: number;
1011
setUri(uri: globalAndroid.net.Uri, options: string, listener: facebook.drawee.controller.ControllerListener);
@@ -20,9 +21,7 @@ declare namespace com {
2021
class BaseDataSubscriber extends facebook.datasource.BaseDataSubscriber<any> {
2122
public constructor(listener: BaseDataSubscriberListener);
2223
}
23-
class OkHttpNetworkFetcher extends facebook.imagepipeline.backends.okhttp3.OkHttpNetworkFetcher {
24-
25-
}
24+
class OkHttpNetworkFetcher extends facebook.imagepipeline.backends.okhttp3.OkHttpNetworkFetcher {}
2625
}
2726
}
2827
}

0 commit comments

Comments
 (0)