blob: acdfabd6ddac1edf9b214bcb6f1946441772c39c [file] [log] [blame] [view]
David Morrisseya0bf8022014-05-30 14:08:31 +01001Subsampling Zoom Image View
2===========================
davemorrisseyb064b0b2013-08-26 09:45:26 -07003
David Morrissey5fa74e72014-07-31 00:11:06 +01004This library includes two classes, `ScaleImageView` and `SubsamplingScaleImageView`. `SubsamplingScaleImageView` is best for large images but doesn't support display of `Bitmap` objects or resources, and `ScaleImageView` supports `Bitmap` objects but not subsampling or large images. To decide which is best for you, see below.
David Morrissey365ccab2014-07-31 00:08:41 +01005
David Morrisseyef41d8a2014-06-10 01:00:09 +01006#### Download the sample app
7
8[![Get it on Google Play](https://developer.android.com/images/brand/en_generic_rgb_wo_60.png)](https://play.google.com/store/apps/details?id=com.davemorrissey.labs.subscaleview.sample)
9
David Morrissey0e895c22013-08-26 20:07:35 +010010
David Morrissey365ccab2014-07-31 00:08:41 +010011Custom image views for Android with pinch to zoom, panning, rotation and animation support, with easy extension so you can add your own overlays and touch event detection.
12
13`SubsamplingScaleImageView` uses subsampling and tiles to support large images. While zooming in, the
14low resolution, full size base layer is overlaid with smaller tiles at least as high resolution as the screen, and
15tiles are loaded and discarded during panning to avoid holding too much bitmap data in memory. This is ideal for use in image gallery apps where the size of the images may be large enough to require subsampling, and where
David Morrissey260aec22013-08-29 23:31:20 +010016pinch to zoom is required to view the high resolution detail.
17
David Morrissey365ccab2014-07-31 00:08:41 +010018*These views don't extend `ImageView` and aren't intended as a general purpose replacement for it. They're specialised for the display of photos and other large images, not the display of 9-patches, shapes and the other types of drawable that ImageView supports.*
David Morrissey0e895c22013-08-26 20:07:35 +010019
David Morrisseya0bf8022014-05-30 14:08:31 +010020#### Image display
David Morrissey365ccab2014-07-31 00:08:41 +010021
David Morrisseya0bf8022014-05-30 14:08:31 +010022* Display images from assets or the file system
23* Automatically rotate images from the file system (e.g. the camera or gallery) according to EXIF
24* Manually rotate images in 90° increments
25* Swap images at runtime
David Morrissey0e895c22013-08-26 20:07:35 +010026
David Morrissey365ccab2014-07-31 00:08:41 +010027*`SubsamplingScaleImageView` only:*
28
29* Display huge images, larger than can be loaded into memory
30* Show high resolution detail on zooming in
31* Tested up to 20,000x13,000px, though larger images are slower
32
33
David Morrisseya0bf8022014-05-30 14:08:31 +010034#### Gesture detection
35* One finger pan
36* Two finger pinch to zoom
37* Pan while zooming
38* Seamless switch between pan and zoom
39* Fling momentum after panning
David Morrissey77096ba2014-06-05 21:22:44 +010040* Double tap to zoom in and out
David Morrissey02ceb3d2014-05-30 20:48:51 +010041* Options to disable pan and/or zoom gestures
David Morrisseya0bf8022014-05-30 14:08:31 +010042
David Morrissey9f3fad12014-06-08 10:16:49 +010043#### Animation
44* Public methods for animating the scale and center
45* Customisable duration and easing
46* Optional uninterruptible animations
47
David Morrisseya0bf8022014-05-30 14:08:31 +010048#### Overridable event detection
49* Supports `OnClickListener` and `OnLongClickListener`
50* Supports interception of events using `GestureDetector` and `OnTouchListener`
51* Extend to add your own gestures
52
53#### Easy integration
54* Use within a `ViewPager` to create a photo gallery
55* Easily restore scale, center and orientation after screen rotation
56* Can be extended to add overlay graphics that move and scale with the image
57* Handles view resizing and `wrap_content` layout
58
David Morrisseya0bf8022014-05-30 14:08:31 +010059#### Limitations
David Morrissey365ccab2014-07-31 00:08:41 +010060* `SubsamplingScaleImageView` requires SDK 10 (Gingerbread).
61* `SubsamplingScaleImageView` cannot decode an image from resources or display a `Bitmap` object - the image file needs to be in assets or external storage.
62* These views do not extend ImageView so attributes including android:tint, android:scaleType and android:src are not supported.
David Morrisseya0bf8022014-05-30 14:08:31 +010063* Images stored in assets cannot be rotated based on EXIF, you'll need to do it manually. You probably know the orientation of your own assets :-)
64
David Morrissey365ccab2014-07-31 00:08:41 +010065## Which view is best?
66
67Use `SubsamplingScaleImageView` if:
68
69* You want to zoom into very large images without losing detail.
70* You need to display images of unknown size e.g. from the camera or gallery.
71* You don't know if the images may be too large to fit in memory on some devices.
72* You need to display images larger than 2048px.
73* You don't need to support devices older than SDK 10.
74
75Use `ScaleImageView` if:
76
77* You know the size of the images you're displaying.
78* You know the images are small enough to fit in memory on all your target devices.
79* Your images are no larger than 2048px, or you are able to scale them down.
80* You need to support devices older than SDK 10.
81
David Morrissey8e742a72014-06-06 20:23:03 +010082## Quality notes
83
84Images are decoded as dithered RGB_565 bitmaps by default, because this requires half as much memory as ARGB_8888. For most
85JPGs you won't notice the difference in quality. If you are displaying large PNGs with alpha channels, Android will probably
86decode them as ARGB_8888, and this may cause `OutOfMemoryError`s. **If possible, remove the alpha channel from PNGs larger than about 2,000x2,000.**
87This allows them to be decoded as RGB_565.
88
David Morrisseya0bf8022014-05-30 14:08:31 +010089## Basic setup
90
91Checkout the project and import the library project as a module in your app. Alternatively you can just copy the classes in `com.davemorrissey.labs.subscaleview` to your project.
92
93Add the view to your layout XML as shown below. Normally you should set width and height to `match_parent`.
94
95 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
96 android:layout_width="match_parent"
97 android:layout_height="match_parent" >
98
99 <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
100 android:id="@+id/imageView"
David Morrisseye11ee3e2014-05-30 14:09:41 +0100101 android:layout_width="match_parent"
102 android:layout_height="match_parent"/>
David Morrisseya0bf8022014-05-30 14:08:31 +0100103
104 </RelativeLayout>
105
106Now, in your fragment or activity, set the image asset name or file path.
107
108 SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)findViewById(id.imageView);
109 imageView.setImageAsset("map.png");
110 // ... or ...
111 imageView.setImageFile("/sdcard/DCIM/DSCM00123.JPG");
112
113That's it! Keep reading for some more options.
114
115## Define asset name in XML
116
117For a zero code approach to showing an image from your assets, you need to define the custom namespace in your layout.
118
119 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
120 xmlns:ssiv="http://schemas.android.com/apk/res-auto"
121 android:layout_width="match_parent"
122 android:layout_height="match_parent" >
123
124 <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
125 ssiv:assetName="map.png"
David Morrisseye11ee3e2014-05-30 14:09:41 +0100126 android:layout_width="match_parent"
127 android:layout_height="match_parent"/>
David Morrisseya0bf8022014-05-30 14:08:31 +0100128
129 </RelativeLayout>
130
131**This method doesn't support restoring state after a screen orientation change.**
132
133## Handle screen orientation changes
134
135If you want the current scale, center and orientation to be preserved when the screen is rotated, you can request it from the view's `getState` method, and restore it after rotation, by passing it to the view along with the image asset name or file path. Here's a simple example of how you might do this in a fragment.
136
137 private static final String BUNDLE_STATE = "ImageViewState";
138
139 @Override
140 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
141 View rootView = inflater.inflate(R.layout.my_fragment, container, false);
142
143 ImageViewState imageViewState = null;
144 if (savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_STATE)) {
145 imageViewState = (ImageViewState)savedInstanceState.getSerializable(BUNDLE_STATE);
146 }
147 SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)rootView.findViewById(id.imageView);
148 imageView.setImageAsset("map.png", imageViewState);
149
150 return rootView;
151 }
152
153 @Override
154 public void onSaveInstanceState(Bundle outState) {
155 View rootView = getView();
156 if (rootView != null) {
157 SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)rootView.findViewById(id.imageView);
158 ImageViewState state = imageView.getState();
159 if (state != null) {
160 outState.putSerializable(BUNDLE_STATE, imageView.getState());
161 }
162 }
163 }
164
165## Extending functionality
166
David Morrissey365ccab2014-07-31 00:08:41 +0100167Take a look at the sample app for examples of classes that overlay graphics on top of the image so that they move and scale with it. `FreehandView` adds event detection, capturing only the touch events it needs so pan and zoom still work normally.
David Morrissey5833e302014-06-06 22:08:14 +0100168
169## About
170
171Copyright 2014 David Morrissey, and licensed under the Apache License, Version 2.0. No attribution is necessary but it's very much appreciated. Star this project to show your gratitude.
172
173This project started life as a way of showing very large images (e.g. a large building floor plan) with gestures to pan and zoom, with support for extensions that showed overlays (location pins, annotations) aligned with the image. It's grown massively, but for the moment I am keeping everything in one class to prevent subclasses and extensions breaking the assumptions (or violating invariants) on which the class depends.