Skip to content

Commit 778bd83

Browse files
committed
fix(ui-canvas): Use screen scale for android canvas scaling
1 parent 1a4b754 commit 778bd83

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

src/ui-canvas/canvas.android.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ function createColorParam(param) {
3333
function drawViewOnCanvas(canvas: android.graphics.Canvas, view: View, rect?: android.graphics.Rect) {
3434
if (!view.nativeView) {
3535
const activity = Application.android.foregroundActivity;
36-
(view as any)._setupAsRootView(activity);
37-
(view as any)._isAddedToNativeVisualTree = true;
38-
(view as any).callLoaded();
36+
view._setupAsRootView(activity);
37+
view._isAddedToNativeVisualTree = true;
38+
view.callLoaded();
3939
}
4040
if (view.nativeView) {
4141
if (rect) {
@@ -46,7 +46,7 @@ function drawViewOnCanvas(canvas: android.graphics.Canvas, view: View, rect?: an
4646
canvas.save();
4747
canvas.translate(rect.left, rect.top);
4848
}
49-
view.nativeView.draw(canvas as any);
49+
view.nativeView.draw(canvas);
5050
if (rect) {
5151
canvas.restore();
5252
}
@@ -287,7 +287,7 @@ export class Paint extends ProxyClass<android.graphics.Paint> {
287287
}
288288
}
289289
set color(color) {
290-
(this as any).setColor(color);
290+
this['setColor'](color);
291291
}
292292
set strokeWidth(value: number) {
293293
this.mNative.setStrokeWidth(value);

src/ui-canvas/canvas.ios.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1762,9 +1762,9 @@ export class Canvas implements ICanvas {
17621762
@paint
17631763
drawView(view: View, rect?: Rect) {
17641764
if (!view.nativeView) {
1765-
(view as any)._setupAsRootView({});
1766-
(view as any)._isAddedToNativeVisualTree = true;
1767-
(view as any).callLoaded();
1765+
view._setupAsRootView({});
1766+
view._isAddedToNativeVisualTree = true;
1767+
view.callLoaded();
17681768
}
17691769
if (view.nativeView) {
17701770
const uiView = view.nativeView as UIView;
@@ -2233,13 +2233,13 @@ export class LinearGradient {
22332233
}
22342234

22352235
const cgColors = this.colors.map((c) => (c instanceof Color ? c : new Color(c)).ios.CGColor);
2236-
this.mGradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), cgColors as any, stopsRef);
2236+
this.mGradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), cgColors, stopsRef);
22372237
if (this.mGradient) {
22382238
CFRetain(this.mGradient);
22392239
}
22402240
} else {
22412241
const cgColors = [this.colors, this.stops].map((c) => (c instanceof Color ? c : new Color(c)).ios.CGColor);
2242-
this.mGradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), cgColors as any, null);
2242+
this.mGradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), cgColors, null);
22432243
if (this.mGradient) {
22442244
CFRetain(this.mGradient);
22452245
}

src/ui-canvas/index.android.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CSSType, Utils } from '@nativescript/core';
22
import { Canvas, Paint } from './canvas';
3-
import { CanvasBase, hardwareAcceleratedProperty } from './index.common';
3+
import { CanvasBase, DEFAULT_SCALE, hardwareAcceleratedProperty } from './index.common';
44
import { sdkVersion } from './canvas.common';
55

66
export * from './canvas';
@@ -59,19 +59,22 @@ export class CanvasView extends CanvasBase {
5959
this.nativeViewProtected.drawListener = new com.akylas.canvas.DrawListener({
6060
onDraw: (canvas: android.graphics.Canvas) => {
6161
const drawFrameRate = this.drawFrameRate;
62+
const scale = DEFAULT_SCALE;
6263
let startTime;
64+
6365
if (drawFrameRate) {
6466
startTime = Date.now();
6567
}
66-
const scale = this.density;
68+
6769
canvas.save();
68-
canvas.scale(scale, scale); // always scale to device density to work with dp
70+
canvas.scale(scale, scale); // always scale to device screen density to work with dip
6971
this.augmentedCanvas.setNative(canvas);
7072
this.onDraw(this.augmentedCanvas as any);
73+
7174
if (drawFrameRate) {
7275
const end = Date.now();
7376
if (!this.frameRatePaint) {
74-
this.frameRatePaint = new Paint() as any;
77+
this.frameRatePaint = new Paint();
7578
this.frameRatePaint.color = 'blue';
7679
this.frameRatePaint.setTextSize(12);
7780
}

tools

0 commit comments

Comments
 (0)