A NativeScript Plugin for Android apps.
This plugin is deprecated. Feel free to use the Image Plugin for efficiently displaying images on Android and iOS in your NativeScript app. If you already have an app that use the Fresco Plugin, read the migrate-to-image doc for initial guidance.
nativescript-fresco
?nativescript-fresco
is a NativeScript plugin that exposes the Fresco library used for efficiently displaying images on Android. More information about what Fresco is and how it works is available on its official website here.
The nativescript-fresco
plugin enables NativeScript developers to use the FrescoDrawee
class which is extends the traditional Android ImageView component and adds the smart Fresco image management algorithms. The plugin exposes the drawee as a NativeScript view so you basically put it in the XML definition of your page and provide the URI to the image you would like to use.
nativescript-fresco
?tns plugin add nativescript-fresco
.nativescript-fresco
in the launch
event of your {N} application by using the following code:tsc
to transpile the .ts
sources: tsc -p
.tns plugin add <path-to-fresco-repo-dir>
.nativescript-fresco
in the launch
event of your {N} application by using the following code:JavaScript:
var application = require("application");
var fresco = require("nativescript-fresco");
if (application.android) {
application.on("launch", function () {
fresco.initialize();
});
}
TypeScript:
import application = require("application");
import fresco = require("nativescript-fresco");
if (application.android) {
application.on("launch", () => {
fresco.initialize();
});
}
When working with "downsampling" you will need to pass a configuration to the
initialize
function:
fresco.initialize({ isDownsampleEnabled: true });
Use fresco
in the XML definition of the page as follows:
<Page
xmlns="http://www.nativescript.org/tns.xsd"
xmlns:nativescript-fresco="nativescript-fresco">
<nativescript-fresco:FrescoDrawee width="250" height="250"
imageUri="<uri-to-a-photo-from-the-web-or-a-local-resource>"/>
</Page>
TNSFrescoModule
from nativescript-fresco/angular
and add it to the imports
of your initial @NgModule
, like shown here.nativescript-fresco
plugin in the launch
event of your {N} application.You can refer the demo-angular folder of the repo for runnable {N} project that demonstrates the nativescript-fresco plugin with all of its features in action.
AnimatedImage
class you can continue to cast to this type just note that it is now an interface rather than a classIAnimatedImage
interface simply change all of your references to be AnimatedImage
IImageInfo
interface simply change all of your references to be ImageInfo
IError
interface simply change all of your references to be FrescoError
If you find yourself in the need to access directly the native library of the nativescript-fresco plugin you can import the android.d.ts from the plugin in your references.d.ts
:
/// <reference path="./node_modules/nativescript-fresco/android.d.ts" />
As documented by the Fresco library setting the height and width are mandatory, more details on this topic could be found here. So the first this you should do when declaring the FrescoDrawee is set its width and height attributes or set only one of them and set the FrescoDrawee's aspectRatio. The width and height of the FrescoDrawee in your {N} application supports percentages which makes it possible to declare for example width="50%" and aspectRatio="1.33" achieving exactly 50% width with dynamically calculated height based on the aspect ration of the loaded image from the imageUri.
String value used for the image URI. You can use this property to set the image to be loaded from remote location (http, https), from the resources and local files of your {N} application.
<nativescript-fresco:FrescoDrawee imageUri="https://docs.nativescript.org/angular/img/cli-getting-started/angular/chapter0/NativeScript_logo.png"/>
String value used for the placeholder image URI. You can use this property to set a placeholder image loaded from the local and resources files of your {N} application.
*Note: Currently there are limitations on how many different Images can be set to as 'placeholderImage' before OutOfMemoryError is thrown. For best results its recommended to use a single image for all placeholderImageUri
of your FrescoDrawee instances.
<nativescript-fresco:FrescoDrawee placeholderImageUri="~/placeholder.jpg"/>
String value used for the failure image URI. You can use this property to set a failure image loaded from the local and resources files of your {N} application that will be shown if the loading of the imageUri is not successful.
<nativescript-fresco:FrescoDrawee failureImageUri="~/failure.jpg"/>
There are a couple of optional attributes that could be set on the FrescoDrawee instance to achieve advanced behaviors:
String value used for the background image URI. Using this property has similar effect as the placeholderImageUri but the image is stretched to the size of the FrescoDrawee.
*Note: Currently there are limitations on how many different Images can be set to as 'background' before OutOfMemoryError is thrown. For best results its recommended to use a single image for all backgroundUri
of your FrescoDrawee instances.
<nativescript-fresco:FrescoDrawee backgroundUri="~/image.jpg"/>
String value used by FrescoDrawee image scale type. This property can be set to:
'center' - Performs no scaling.
'centerCrop' - Scales the child so that both dimensions will be greater than or equal to the corresponding dimension of the parent.
'centerInside' - Scales the child so that it fits entirely inside the parent.
'fitCenter' - Scales the child so that it fits entirely inside the parent.
'fitStart' - Scales the child so that it fits entirely inside the parent.
'fitEnd' - Scales the child so that it fits entirely inside the parent.
'fitXY' - Scales width and height independently, so that the child matches the parent exactly.
'focusCrop' - Scales the child so that both dimensions will be greater than or equal to the corresponding dimension of the parent.
<nativescript-fresco:FrescoDrawee actualImageScaleType="centerInside"/>
Number value used for the fade-in duration. This value is in milliseconds.
<nativescript-fresco:FrescoDrawee fadeDuration="3000"/>
Number value greater than zero, used as input for the blur function. Larger value means slower processing. For example a value of 10
means that each pixel in the image will be blurred using all adjacent pixels up to a distance of 10.
<nativescript-fresco:FrescoDrawee blurRadius="25"/>
Number value greater than zero, used to scale the image before applying the blur function. Larger value means faster processing. For example a value of 2
means that the image will be scaled by a factor of two before applying blur.
<nativescript-fresco:FrescoDrawee blurDownSampling="2"/>
Number value used as the aspect ratio of the image. This property is useful when you are working with different aspect ratio images and want to have a fixed Width or Height. The ratio of an image is calculated by dividing its width by its height.
Note: In some layout scenarios it is necessary to set the verticalAlignment
of the FrescoDrawee to 'top' or 'bottom' in order to "anchor" the drawee and achieve dynamic sizing.
<nativescript-fresco:FrescoDrawee aspectRatio="1.33" verticalAlignment="top"/>
Number value used as the downsampled width of the fresco drawable.
<nativescript-fresco:FrescoDrawee decodeWidth="100"/>
Number value used as the downsampled width of the fresco drawable.
<nativescript-fresco:FrescoDrawee decodeHeight="100"/>
Boolean value used for enabling or disabling the streaming of progressive JPEG images. This property is set to 'false' by default. Setting this property to 'true' while loading JPEG images not encoded in progressive format will lead to a standard loading of those images.
<nativescript-fresco:FrescoDrawee progressiveRenderingEnabled="true"/>
Boolean value used for showing or hiding the progress bar.
<nativescript-fresco:FrescoDrawee showProgressBar="true"/>
String value used for setting the color of the progress bar. You can set it to hex values ("#FF0000") and/or predefined colors ("green").
<nativescript-fresco:FrescoDrawee progressBarColor="blue"/>
Boolean value used for determining if the image will be rounded as a circle. Its default value is false. If set to true the image will be rounder to a circle.
<nativescript-fresco:FrescoDrawee roundAsCircle="true"/>
Number value used as radius for rounding the image's corners.
<nativescript-fresco:FrescoDrawee roundedCornerRadius="50"/>
Boolean value used for determining if the image's bottom right corner will be rounded. The roundedCornerRadius is used as the rounding radius.
<nativescript-fresco:FrescoDrawee roundBottomRight="true"/>
Boolean value used for determining if the image's bottom left corner will be rounded. The roundedCornerRadius is used as the rounding radius.
<nativescript-fresco:FrescoDrawee roundBottomLeft="true"/>
Boolean value used for determining if the image's top left corner will be rounded. The roundedCornerRadius is used as the rounding radius.
<nativescript-fresco:FrescoDrawee roundTopLeft="true"/>
Boolean value used for determining if the image's top right corner should be rounded. The roundedCornerRadius is used as the rounding radius.
<nativescript-fresco:FrescoDrawee roundTopRight="true"/>
Boolean value used for enabling the automatic playing of animated images. Note that rounding of such images is not supported and will be ignored.
<nativescript-fresco:FrescoDrawee autoPlayAnimations="true"/>
Boolean value used for enabling/disabling a tap to retry action for the download of the FrescoDrawee image.
<nativescript-fresco:FrescoDrawee tapToRetryEnabled="true"/>
This event is fired after the final image has been set. When working with animated images you could use this event to start the animation by calling the FinalEventData.animatable.start() function.
<nativescript-fresco:FrescoDrawee finalImageSet="onFinalImageSet"/>
JavaScript:
function onFinalImageSet(args) {
var frescoEventData = args;
var drawee = frescoEventData.object;
}
exports.onFinalImageSet = onFinalImageSet;
TypeScript:
import {FrescoDrawee, FinalEventData } from "nativescript-fresco";
export function onFinalImageSet(args: FinalEventData) {
var drawee = args.object as FrescoDrawee;
}
This event is fired after the fetch of the final image failed.
<nativescript-fresco:FrescoDrawee failure="onFailure"/>
JavaScript:
function onFailure(args) {
var drawee = args.object;
}
exports.onFailure = onFailure;
TypeScript:
import {FrescoDrawee, FailureEventData } from "nativescript-fresco";
export function onFailure(args: FailureEventData) {
var drawee = args.object as FrescoDrawee;
}
This event is fired after any intermediate image has been set.
<nativescript-fresco:FrescoDrawee intermediateImageSet="onIntermediateImageSet"/>
JavaScript:
function onIntermediateImageSet(args) {
var drawee = args.object;
}
exports.onIntermediateImageSet = onIntermediateImageSet;
TypeScript:
import {FrescoDrawee, IntermediateEventData } from "nativescript-fresco";
export function onIntermediateImageSet(args: IntermediateEventData) {
var drawee = args.object as FrescoDrawee;
}
This event is fired after the fetch of the intermediate image failed.
<nativescript-fresco:FrescoDrawee intermediateImageFailed="onIntermediateImageFailed"/>
JavaScript:
function intermediateImageFailed(args) {
var drawee = args.object;
}
exports.intermediateImageFailed = intermediateImageFailed;
TypeScript:
import {FrescoDrawee, FailureEventData } from "nativescript-fresco";
export function intermediateImageFailed(args: FailureEventData) {
var drawee = args.object as FrescoDrawee;
}
This event is fired before the image request is submitted.
<nativescript-fresco:FrescoDrawee submit="onSubmit"/>
JavaScript:
function onSubmit(args) {
var drawee = args.object;
}
exports.onSubmit = onSubmit;
TypeScript:
import {FrescoDrawee, EventData } from "nativescript-fresco";
export function onSubmit(args: EventData) {
var drawee = args.object as FrescoDrawee;
}
This event is fired after the controller released the fetched image.
<nativescript-fresco:FrescoDrawee release="onRelease"/>
JavaScript:
function onRelease(args) {
var drawee = args.object;
}
exports.onRelease = onRelease;
TypeScript:
import {FrescoDrawee, EventData } from "nativescript-fresco";
export function onRelease(args: EventData) {
var drawee = args.object as FrescoDrawee;
}
All events exposed by 'nativescript-fresco' provide additional information to their handlers that is needed to properly handle them. Here's a brief description of the event arguments coming with each of the events:
Instances of this class are provided to the handlers of the finalImageSet.
import {FrescoDrawee, FinalEventData, ImageInfo, AnimatedImage } from "nativescript-fresco";
export function onFinalImageSet(args: FinalEventData) {
var info: ImageInfo = args.imageInfo;
var animatable: AnimatedImage = args.animatable;
var quality: number = info.getQualityInfo().getQuality();
var isFullQuality: boolean = info.getQualityInfo().isOfFullQuality();
var isOfGoodEnoughQuality: boolean = info.getQualityInfo().isOfGoodEnoughQuality();
}
Instances of this class are provided to the handlers of the failure and intermediateImageFailed.
import {FrescoDrawee, FailureEventData, FrescoError } from "nativescript-fresco";
export function onFailure(args: FailureEventData) {
var error: FrescoError = args.error;
var message: string = error.getMessage();
var type: string = error.getErrorType();
var fullError: string = error.toString();
}
Instances of this class are provided to the handlers of the intermediateImageSet.
import {FrescoDrawee, IntermediateEventData, ImageInfo } from "nativescript-fresco";
export function onIntermediateImageSet(args: IntermediateEventData) {
var info: ImageInfo = args.imageInfo;
var quality: number = info.getQualityInfo().getQuality();
var isFullQuality: boolean = info.getQualityInfo().isOfFullQuality();
var isOfGoodEnoughQuality: boolean = info.getQualityInfo().isOfGoodEnoughQuality();}
Instances of this class are provided to the handlers of the release and submit.
import {FrescoDrawee, EventData } from "nativescript-fresco";
export function onSubmit(args: EventData) {
var drawee = args.object as FrescoDrawee;
}
The nativescript-fresco {N} plugin has built-in cache mechanism which handles managing the images in the memory. There are two types of cache mechanisms memory
and disk
, you can manually manage both of them with the following functionality.
Not so rarely you may have a scenario where the actual image on your remote service from the imageUri
of the FrescoDrawee
has changed but the {N} app already has an image in its internal cache. In such scenario you can easily 'refresh' the imageUri
by calling the updateImageUri()
:
// 'drawee' is the instance the 'FrescoDrawee' in the project.
drawee.updateImageUri();
Managing the caches in nativescript-fresco is done via the ImagePipeline
. In order to get the reference of the ImagePipeline simply call the getImagePipeline()
function:
var frescoModel = require("nativescript-fresco");
var imagePipeLine = frescoModel.getImagePipeline();
imagePipeLine.clearCaches();
imagePipeLine.clearMemoryCaches();
imagePipeLine.clearDiskCaches();
If clearing the entire cache is not what you desired, you can clear only the images linked with a specific URI (imageUri
). Evicting is done again via the ImagePipeline:
var frescoModel = require("nativescript-fresco");
var imagePipeLine = frescoModel.getImagePipeline();
imagePipeLine.evictFromCache("<uri-to-a-photo-from-the-web>");
imagePipeLine.evictFromMemoryCache("<uri-to-a-photo-from-the-web>");
imagePipeLine.evictFromDiskCache("<uri-to-a-photo-from-the-web>");
In very very rare occasions the native Android Fresco library may experience strange memory leak issues, in such scenarios as a last resort you may want to "shut down" the library forcing all of the managed memory to possibly be released. You can do that by calling the shutDown
function exposed by the nativescript-fresco module, one good application lifecycle event to call it inside may be in the exit
event of the application:
import * as app from "application";
import * as frescoModule from "nativescript-fresco";
if (app.android) {
app.on(app.exitEvent, (args) => {
frescoModule.shutDown();
});
}
All of the images are sample images for showcasing purposes.
Sample 1 - Placeholder image | Sample 2 - Transition (fade-in animation) |
---|---|
Sample 3 - Image shown successfully from imageUri | Sample 4 - 'Failure' image shown |
---|---|
We love PRs! Check out the contributing guidelines. If you want to contribute, but you are not sure where to start - look for issues labeled help wanted
.
Please, use github issues strictly for reporting bugs or requesting features. For general questions and support, check out Stack Overflow or ask our experts in NativeScript community Slack channel.
NativeScript 可以使用 Javascript,CSS, XML 创建真正的 Native 跨平台应用,支持 iOS Android,NativeScript 将您的跨平台代码翻译成目标平台的代码。 UI 使用 XML 描述,CSS 样式,在编译时将 UI 转化成本地原生代码,最终得到正在的 Native 原生应用。 Telerik 公开了用于创建安卓、iOS和Windows Unive
NativeScript Command-Line Interface The NativeScript CLI lets you create, build, and deploy NativeScript-based apps on iOS and Android devices. Get it using: npm install -g nativescript What is Native
NativeScript-Snackbar �� �� �� NativeScript plugin for Material Design SnackBar component. Installation: NativeScript 7+:tns plugin add @nstudio/nativescript-snackbar NativeScript version prior to 7:t
Nativescript-Ripple This plugin aims to bring a native (or close to native) ripple implementation on Android and iOS. The android version uses a RippleDrawable and conserves the previous background, a
NativeScript-FloatingActionButton NativeScript plugin for Material Design Floating Action Button UI component. Installation Nativescript 7+: ns plugin add @nstudio/nativescript-floatingactionbutton Na
NativeScript CardView A NativeScript plugin to provide an XML widget to implement the Material Design CardView component. Installation NativeScript 7+: ns plugin add @nstudio/nativescript-cardview Nat