forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.android.ts
More file actions
171 lines (145 loc) · 5.61 KB
/
image.android.ts
File metadata and controls
171 lines (145 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {
ImageSource, ImageAsset, ImageBase, stretchProperty, imageSourceProperty, srcProperty, tintColorProperty, Color,
isDataURI, isFileOrResourcePath, RESOURCE_PREFIX
} from "./image-common";
import { knownFolders } from "../../file-system";
export * from "./image-common";
const FILE_PREFIX = "file:///";
const ASYNC = "async";
let AndroidImageView: typeof org.nativescript.widgets.ImageView;
interface ImageLoadedListener {
new (owner: Image): org.nativescript.widgets.image.Worker.OnImageLoadedListener;
}
let ImageLoadedListener: ImageLoadedListener;
function initializeImageLoadedListener() {
if (ImageLoadedListener) {
return;
}
@Interfaces([org.nativescript.widgets.image.Worker.OnImageLoadedListener])
class ImageLoadedListenerImpl extends java.lang.Object implements org.nativescript.widgets.image.Worker.OnImageLoadedListener {
constructor(public owner: Image) {
super();
return global.__native(this);
}
onImageLoaded(success: boolean): void {
const owner = this.owner;
if (owner) {
owner.isLoading = false;
}
}
}
ImageLoadedListener = ImageLoadedListenerImpl;
}
export class Image extends ImageBase {
nativeViewProtected: org.nativescript.widgets.ImageView;
public decodeWidth = 0;
public decodeHeight = 0;
public useCache = true;
public createNativeView() {
if (!AndroidImageView) {
AndroidImageView = org.nativescript.widgets.ImageView;
}
initializeImageLoadedListener();
const imageView = new AndroidImageView(this._context);
const listener = new ImageLoadedListener(this);
imageView.setImageLoadedListener(listener);
(<any>imageView).listener = listener;
return imageView;
}
public initNativeView(): void {
super.initNativeView();
(<any>this.nativeViewProtected).listener.owner = this;
}
public disposeNativeView() {
(<any>this.nativeViewProtected).listener.owner = null;
super.disposeNativeView();
}
public resetNativeView(): void {
super.resetNativeView();
this.nativeViewProtected.setImageMatrix(new android.graphics.Matrix());
}
public _createImageSourceFromSrc(value: string | ImageSource | ImageAsset) {
const imageView = this.nativeViewProtected;
if (!imageView) {
return;
}
if (!value) {
imageView.setUri(null, 0, 0, false, true);
return;
}
const async = this.loadMode === ASYNC;
if (typeof value === "string" || value instanceof String) {
value = value.trim();
this.isLoading = true;
if (isDataURI(value)) {
// TODO: Check with runtime what should we do in case of base64 string.
super._createImageSourceFromSrc(value);
} else if (isFileOrResourcePath(value)) {
if (value.indexOf(RESOURCE_PREFIX) === 0) {
imageView.setUri(value, this.decodeWidth, this.decodeHeight, this.useCache, async);
} else {
let fileName = value;
if (fileName.indexOf("~/") === 0) {
fileName = knownFolders.currentApp().path + "/" + fileName.replace("~/", "");
}
imageView.setUri(FILE_PREFIX + fileName, this.decodeWidth, this.decodeHeight, this.useCache, async);
}
} else {
// For backwards compatibility http always use async loading.
imageView.setUri(value, this.decodeWidth, this.decodeHeight, this.useCache, true);
}
} else {
super._createImageSourceFromSrc(value);
}
}
[stretchProperty.getDefault](): "aspectFit" {
return "aspectFit";
}
[stretchProperty.setNative](value: "none" | "aspectFill" | "aspectFit" | "fill") {
switch (value) {
case "aspectFit":
this.nativeViewProtected.setScaleType(android.widget.ImageView.ScaleType.FIT_CENTER);
break;
case "aspectFill":
this.nativeViewProtected.setScaleType(android.widget.ImageView.ScaleType.CENTER_CROP);
break;
case "fill":
this.nativeViewProtected.setScaleType(android.widget.ImageView.ScaleType.FIT_XY);
break;
case "none":
default:
this.nativeViewProtected.setScaleType(android.widget.ImageView.ScaleType.MATRIX);
break;
}
}
[tintColorProperty.getDefault](): Color {
return undefined;
}
[tintColorProperty.setNative](value: Color) {
if (value === undefined) {
this.nativeViewProtected.clearColorFilter();
} else {
this.nativeViewProtected.setColorFilter(value.android);
}
}
[imageSourceProperty.getDefault](): ImageSource {
return undefined;
}
[imageSourceProperty.setNative](value: ImageSource) {
const nativeView = this.nativeViewProtected;
if (value && value.android) {
const rotation = value.rotationAngle ? value.rotationAngle : 0;
nativeView.setRotationAngle(rotation);
nativeView.setImageBitmap(value.android);
} else {
nativeView.setRotationAngle(0);
nativeView.setImageBitmap(null);
}
}
[srcProperty.getDefault](): any {
return undefined;
}
[srcProperty.setNative](value: any) {
this._createImageSourceFromSrc(value);
}
}