blob: 094b21c4ff0ebb21747312d80161177d074e2edd [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 Morrisseye7d25ef2014-10-18 19:11:40 +01006#### Share your app
7
8**Are you using this library in your app? Let me know and I'll add it to this readme.**
9
10
David Morrisseyef41d8a2014-06-10 01:00:09 +010011#### Download the sample app
12
13[![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)
14
David Morrissey0e895c22013-08-26 20:07:35 +010015
David Morrissey365ccab2014-07-31 00:08:41 +010016Custom 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.
17
18`SubsamplingScaleImageView` uses subsampling and tiles to support large images. While zooming in, the
19low resolution, full size base layer is overlaid with smaller tiles at least as high resolution as the screen, and
20tiles 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 +010021pinch to zoom is required to view the high resolution detail.
22
David Morrissey365ccab2014-07-31 00:08:41 +010023*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 +010024
David Morrisseya0bf8022014-05-30 14:08:31 +010025#### Image display
David Morrissey365ccab2014-07-31 00:08:41 +010026
David Morrisseya0bf8022014-05-30 14:08:31 +010027* Display images from assets or the file system
28* Automatically rotate images from the file system (e.g. the camera or gallery) according to EXIF
29* Manually rotate images in 90° increments
30* Swap images at runtime
David Morrissey0e895c22013-08-26 20:07:35 +010031
David Morrissey365ccab2014-07-31 00:08:41 +010032*`SubsamplingScaleImageView` only:*
33
34* Display huge images, larger than can be loaded into memory
35* Show high resolution detail on zooming in
36* Tested up to 20,000x13,000px, though larger images are slower
37
38
David Morrisseya0bf8022014-05-30 14:08:31 +010039#### Gesture detection
40* One finger pan
41* Two finger pinch to zoom
42* Pan while zooming
43* Seamless switch between pan and zoom
44* Fling momentum after panning
David Morrissey77096ba2014-06-05 21:22:44 +010045* Double tap to zoom in and out
David Morrissey02ceb3d2014-05-30 20:48:51 +010046* Options to disable pan and/or zoom gestures
David Morrisseya0bf8022014-05-30 14:08:31 +010047
David Morrissey9f3fad12014-06-08 10:16:49 +010048#### Animation
49* Public methods for animating the scale and center
50* Customisable duration and easing
51* Optional uninterruptible animations
52
David Morrisseya0bf8022014-05-30 14:08:31 +010053#### Overridable event detection
54* Supports `OnClickListener` and `OnLongClickListener`
55* Supports interception of events using `GestureDetector` and `OnTouchListener`
56* Extend to add your own gestures
57
58#### Easy integration
59* Use within a `ViewPager` to create a photo gallery
60* Easily restore scale, center and orientation after screen rotation
61* Can be extended to add overlay graphics that move and scale with the image
62* Handles view resizing and `wrap_content` layout
63
David Morrisseya0bf8022014-05-30 14:08:31 +010064#### Limitations
David Morrissey365ccab2014-07-31 00:08:41 +010065* `SubsamplingScaleImageView` requires SDK 10 (Gingerbread).
66* `SubsamplingScaleImageView` cannot decode an image from resources or display a `Bitmap` object - the image file needs to be in assets or external storage.
67* 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 +010068* 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 :-)
69
David Morrissey365ccab2014-07-31 00:08:41 +010070## Which view is best?
71
72Use `SubsamplingScaleImageView` if:
73
74* You want to zoom into very large images without losing detail.
75* You need to display images of unknown size e.g. from the camera or gallery.
76* You don't know if the images may be too large to fit in memory on some devices.
77* You need to display images larger than 2048px.
78* You don't need to support devices older than SDK 10.
79
80Use `ScaleImageView` if:
81
82* You know the size of the images you're displaying.
83* You know the images are small enough to fit in memory on all your target devices.
84* Your images are no larger than 2048px, or you are able to scale them down.
85* You need to support devices older than SDK 10.
86
David Morrissey8e742a72014-06-06 20:23:03 +010087## Quality notes
88
89Images are decoded as dithered RGB_565 bitmaps by default, because this requires half as much memory as ARGB_8888. For most
90JPGs you won't notice the difference in quality. If you are displaying large PNGs with alpha channels, Android will probably
91decode them as ARGB_8888, and this may cause `OutOfMemoryError`s. **If possible, remove the alpha channel from PNGs larger than about 2,000x2,000.**
92This allows them to be decoded as RGB_565.
93
David Morrisseya0bf8022014-05-30 14:08:31 +010094## Basic setup
95
96Checkout 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.
97
98Add the view to your layout XML as shown below. Normally you should set width and height to `match_parent`.
99
100 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
101 android:layout_width="match_parent"
102 android:layout_height="match_parent" >
103
104 <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
105 android:id="@+id/imageView"
David Morrisseye11ee3e2014-05-30 14:09:41 +0100106 android:layout_width="match_parent"
107 android:layout_height="match_parent"/>
David Morrisseya0bf8022014-05-30 14:08:31 +0100108
109 </RelativeLayout>
110
111Now, in your fragment or activity, set the image asset name or file path.
112
113 SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)findViewById(id.imageView);
114 imageView.setImageAsset("map.png");
115 // ... or ...
116 imageView.setImageFile("/sdcard/DCIM/DSCM00123.JPG");
117
118That's it! Keep reading for some more options.
119
120## Define asset name in XML
121
122For a zero code approach to showing an image from your assets, you need to define the custom namespace in your layout.
123
124 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
125 xmlns:ssiv="http://schemas.android.com/apk/res-auto"
126 android:layout_width="match_parent"
127 android:layout_height="match_parent" >
128
129 <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
130 ssiv:assetName="map.png"
David Morrisseye11ee3e2014-05-30 14:09:41 +0100131 android:layout_width="match_parent"
132 android:layout_height="match_parent"/>
David Morrisseya0bf8022014-05-30 14:08:31 +0100133
134 </RelativeLayout>
135
136**This method doesn't support restoring state after a screen orientation change.**
137
138## Handle screen orientation changes
139
140If 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.
141
142 private static final String BUNDLE_STATE = "ImageViewState";
143
144 @Override
145 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
146 View rootView = inflater.inflate(R.layout.my_fragment, container, false);
147
148 ImageViewState imageViewState = null;
149 if (savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_STATE)) {
150 imageViewState = (ImageViewState)savedInstanceState.getSerializable(BUNDLE_STATE);
151 }
152 SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)rootView.findViewById(id.imageView);
153 imageView.setImageAsset("map.png", imageViewState);
154
155 return rootView;
156 }
157
158 @Override
159 public void onSaveInstanceState(Bundle outState) {
160 View rootView = getView();
161 if (rootView != null) {
162 SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)rootView.findViewById(id.imageView);
163 ImageViewState state = imageView.getState();
164 if (state != null) {
165 outState.putSerializable(BUNDLE_STATE, imageView.getState());
166 }
167 }
168 }
169
170## Extending functionality
171
David Morrissey365ccab2014-07-31 00:08:41 +0100172Take 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 +0100173
174## About
175
176Copyright 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.
177
178This 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.