UNPKG

nativescript-gif

Version:
122 lines 3.6 kB
import { Http, knownFolders } from '@nativescript/core'; import { GifCommon, headersProperty, srcProperty } from './gif.common'; export class Gif extends GifCommon { constructor() { super(); this._headers = null; this._src = null; } createNativeView() { this.nativeView = new pl.droidsonroids.gif.GifImageView(this._context); return this.nativeView; } stop() { if (this._drawable) { this._drawable.stop(); } } start() { if (this._drawable) { this._drawable.start(); } } isPlaying() { if (this._drawable) { const isPlaying = this._drawable.isRunning(); return isPlaying; } else { return false; } } getFrameCount() { let frames; if (this._drawable) { frames = this._drawable.getNumberOfFrames(); } return frames; } reset() { this._drawable.reset(); } getDuration() { if (this._drawable) { const duration = this._drawable.getDuration(); return duration; } else { return 0; } } setSpeed(factor) { if (this._drawable) { this._drawable.setSpeed(factor); } } recycle() { if (this._drawable) { this._drawable.recycle(); } } [headersProperty.setNative](value) { this._setHeader(value ? value : null); } [srcProperty.setNative](value) { this._setSrcProperty(value); } _setSrcProperty(value) { if (value) { value = value.trim(); let isUrl = false; if (value.indexOf('://') !== -1) { if (value.indexOf('res://') === -1) { isUrl = true; } } this._src = value; if (!isUrl) { const currentPath = knownFolders.currentApp().path; if (value[1] === '/' && (value[0] === '.' || value[0] === '~')) { value = value.substr(2); } if (value[0] !== '/') { value = currentPath + '/' + value; } this._drawable = new pl.droidsonroids.gif.GifDrawable(value); this.nativeView.setImageDrawable(this._drawable); } else { const requestOptions = { url: value, method: 'GET' }; if (this._headers !== null) { requestOptions.headers = this._headers; } Http.request(requestOptions).then((r) => { if (r.statusCode === 200) { this._drawable = new pl.droidsonroids.gif.GifDrawable(r.content.raw.toByteArray()); this.nativeView.setImageDrawable(this._drawable); } else { console.log('error getting image: ' + r.statusCode); } }, (err) => { console.log(err); }); } } else { console.log('No src property set for the Gif.'); } } _setHeader(headers) { if (headers) { this._headers = headers; if (this._src && this._src.length > 0) { this._setSrcProperty(this._src); } } else { this._headers = null; } } } //# sourceMappingURL=gif.android.js.map