bradmartin / nativescript-gif

NativeScript plugin to use native gifs
Other
50 stars 18 forks source link
android gif ios nativescript nativescript-gif nativescript-plugin typescript

NativeScript-Gif

NativeScript plugin to use GIFs in your application.

Action Build npm npm


Installation

NativeScript Version 7+: tns plugin add nativescript-gif NativeScript Version prior to 7: tns plugin add nativescript-gif@4.0.6

The native libraries used to handle rendering GIFs for Android & iOS.

Android Library iOS CocoaPod
Koral-- / android-gif-drawable FLAnimatedImage by Flipboard
Android Screen iOS Screen
GifExample iOSSample

Usage

Plain NativeScript

IMPORTANT: Make sure you include xmlns:Gif="nativescript-gif" on the Page element.

<Page xmlns="http://schemas.nativescript.org/tns.xsd"
      xmlns:Gif="nativescript-gif" loaded="pageLoaded">
  <StackLayout>
    <Gif:Gif headers="{{headersJSON}}" src="https://github.com/bradmartin/nativescript-gif/raw/master/~/gifs/bill.gif" height="100" />
    <Gif:Gif src="https://media4.giphy.com/media/3uyIgVxP1qAjS/200.gif" height="200" />
  </StackLayout>
</Page>

NativeScript Angular

import { registerElement } from 'nativescript-angular/element-registry';
import { Gif } from 'nativescript-gif';
registerElement('Gif', () => Gif);
HTML
<StackLayout>
    <Gif [headers]="headersJSON" src="https://github.com/bradmartin/nativescript-gif/raw/master/~/gifs/bill.gif" height="100" ></Gif>
    <Gif src="https://media4.giphy.com/media/3uyIgVxP1qAjS/200.gif" height="200" ></Gif>
</StackLayout>

Bundling Note:

Be sure that you have your .gifs added to the globs of the CopyWebpackPlugin as part of your webpack.config.

Prior to Webpack 5

Demo app sample: https://github.com/bradmartin/nativescript-gif/blob/master/demo/webpack.config.js#L282

{
  from: {
    glob: '**/*.gif';
  }
}

Webpack 5+

const webpack = require('@nativescript/webpack')

module.exports = env => {
  webpack.init(env)

  webpack.Utils.addCopyRule('**/*.gif')

  return webpack.resolveConfig()
}

Properties

API

start()
stop()
getFrameCount()
isPlaying()

Android Only
getDuration()
reset()
setSpeed(speedFactor: Number)
recycle()