# Titanium.UI.ImageView
A view to display a single image or series of animated images.
# Overview
Use the Titanium.UI.createImageView method or <ImageView>
Alloy element to create an ImageView
.
Specifying either a width
or height
property for this view will scale its image(s) with
the aspect ratio maintained, up to a maximum size that does not exceed its parent view.
# Remote Images
You can display both local and remote images in an ImageView. When loading remote images, you should set the defaultImage property to a local image, which will be displayed while the remote image is being downloaded. Remote images are cached automatically on the iOS-, Android- and Windows platform.
Android Note: Android 6 and later uses runtime permissions to secure the user's privacy. Therefore, you should call Titanium.Filesystem.requestStoragePermissions before attempting to load remote images.
Read more about remote images and general best practices in the Image Best Practices Guide (opens new window).
# Android 9-Patch Scaled Images
A nine-patch (9-patch) image is simply a standard png
image with a transparent border of a
single pixel, containing "guides" defined by solid black (#000
) color fills. Guides added to the
top and left edges determine that the image may be stretched in horizontal and vertical planes
respectively. Optionally, the content area may be defined by guides added to the bottom and
right edges.
Although any image editor may be used to create the guides, certain settings can cause the edges
of the graphic to "bleed" into the transparent border, consequently making it non-transparent.
Thus, the draw9patch
utility provided in the Android SDK tools directory is recommended for
this purpose.
For further information about nine-patch images and the Android utility, see the Nine-patch (opens new window) and Draw 9-patch (opens new window) sections of the Android Developer website.
The nine-patch technique may be used with any of Titanium's static image properties. See the "Button with Nine-patch Background Image" example for a demonstration.
# Examples
# Basic Image View
In this example, we create a simple image view:
Ti.UI.backgroundColor = 'white';
var win = Ti.UI.createWindow();
var image = Ti.UI.createImageView({
image:'/images/myimage.png'
});
win.add(image);
win.open();
# Button with Nine-patch Background Image
Create a button with a nine-patch image background, that swaps with another image when selected, and generates a message in the console when clicked.
Note that images are referenced in the code without the .9
part of the filename, and that
the project should be cleaned to ensure that the files are correctly copied to the project.
var win = Ti.UI.createWindow({
backgroundColor: 'white',
exitOnClose: true,
fullscreen: false,
title: 'Click button to test'
});
var button = Ti.UI.createButton({
backgroundImage: '/images/custom-slider-right.png',
backgroundSelectedImage:'/images/custom-slider-left.png',
title: 'Click me!',
top: 10,
width: 300,
height: 200
});
button.addEventListener('click',function(e){
Ti.API.info("You clicked the button");
});
win.add(button);
win.open();
# Alloy XML Markup
Previous basic image view example as an Alloy view.
<Alloy>
<Window id="win" backgroundColor="white">
<ImageView id="image" image="/images/myimage.png" />
</Window>
</Alloy>
# Properties
# animating READONLY
Indicates whether animation is running.
Default: false on creation, true on load (Android), false (iPhone, iPad)
# autorotate CREATION ONLY
Indicates whether the image should be rotated based on EXIF orientation data.
Before Titanium 10.1.0, this defaulted to false
on Android.
Default: true
# decodeRetries
Number of times to retry decoding the bitmap at a URL.
Defaults to 2 on Titanium 10.1.0 and higher. Defaults to 5 on older SDK versions.
Default: 2
# defaultImage
Local path to the default image to display while loading a remote image.
# duration
Amount of time in milliseconds to animate one cycle.
Starting with release 2.1.1, the minimum duration is 30 ms and the default duration is 200 ms.
On Android, prior to release 2.1.1, if any images are assigned to the images
property when the animation is
started, duration will be set to the number of images multiplied by 33 ms. If no images
are assigned, duration will be set to 100 ms.
On iOS, prior to release 2.1.1, the default is 30 ms.
On Android, if the value of duration is changed, stop and start need to be called for the new value to take effect.
Default: 200 ms in Release 2.1.1 and later. Platform-specific default in earlier releases.
# enableZoomControls
Show zoom controls when the user touches the image view.
Default: false
# hires
Set to true
to prevent scaling of 2x/3x-resolution remote images for retina
displays.
Causes images to display at 50%, where one pixel of the graphic maps to one physical pixel on-screen. This functionality is automatically determined for local images via their filenames, and thus this property is only applicable to remote images.
Default: false
# image
Image to display.
Image to display, defined using a local filesystem path, a File
object, a remote URL,
or a Blob
object containing image data.
For Android, if there is a redirect and change in protocol, it will not follow. For example, from http to https and vice versa. See Java Bug Database for more information.
# images
Array of images to animate, defined using local filesystem paths, File
objects,
remote URLs (Android only), or Blob
objects containing image data.
When using this property, an initial start()
needs to be called upon the ImageView before any image will show in this imageview.
Related properties/methods to look at: start
, stop
, pause
, reverse
, resume
and repeatCount
# imageTouchFeedback
Enables a ripple effect when the foreground image is touched.
Note that the touchFeedback property applies a ripple effect to the
view's background. If the image is opaque and completely covers the view (such as a photo),
then the background's ripple won't be visible. In this case, you should use this imageTouchFeedback
property to apply a ripple effect to the foreground image assigned via the image
property.
Default: false
# imageTouchFeedbackColor
Optional touch feedback ripple color. This has no effect unless imageTouchFeedback
is true.
Defaults to provided theme color.
# preventDefaultImage
Prevent the default image from being displayed while loading a remote image. This property
is ignored when the defaultImage
property is set.
Default: false
# repeatCount
Number of times to repeat the image animation.
Default: 0 (infinite)
# scalingMode
Determines how the image is scaled within the view.
Default: Titanium.Media.IMAGE_SCALING_AUTO
# tintColor
The view's tintColor. Android does not support setting instances of Titanium.UI.Color
This property is a direct correspondant of the tintColor property of UIView on iOS. If no value is specified, the tintColor of the View is inherited from its superview.
Default: null
# Methods
# pause
Pauses a running animation. Use resume
method to continue.
This method only works if multiple images are specified.
Returns
- Type
- void
# resume
Resumes an animation from a pause
state.
This method only works if multiple images are specified.
Returns
- Type
- void
# start
Starts the image animation. On Android, also resets index
to the first image.
This method only works if multiple images are specified.
Returns
- Type
- void
# stop
Stops a running animation. On iOS, also resets index
to the first image.
This method only works if multiple images are specified.
Returns
- Type
- void
# Events
# change
Fired for each frame change during an animation.
Properties
Name | Type | Description |
---|---|---|
index | Number | Index of the image frame being displayed. |
source | Object | Source object that fired the event. |
type | String | Name of the event fired. |
bubbles | Boolean | True if the event will try to bubble up if possible. |
cancelBubble | Boolean | Set to true to stop the event from bubbling. |
# load
Fired when either the initial image and/or all of the images in an animation are loaded.
Properties
Name | Type | Description |
---|---|---|
state | String | |
source | Object | Source object that fired the event. |
type | String | Name of the event fired. |
bubbles | Boolean | True if the event will try to bubble up if possible. |
cancelBubble | Boolean | Set to true to stop the event from bubbling. |
# error
Fired when an image fails to load.
Properties
Name | Type | Description |
---|---|---|
success | Boolean | Indicates a successful operation. Returns |
error | String | Error message, if any returned. May be undefined. |
code | Number | Error code. If the error was generated by the operating system, that system's error value is used. Otherwise, this value will be -1. |
image | String | URL of the image that failed to load. |
source | Object | Source object that fired the event. |
type | String | Name of the event fired. |
bubbles | Boolean | True if the event will try to bubble up if possible. |
cancelBubble | Boolean | Set to true to stop the event from bubbling. |
# pause
Fired when the animation pauses.