blob: 3d4415ef53969d98f3507be9993dadb266de48d1 [file] [log] [blame] [view]
David Morrisseya0bf8022014-05-30 14:08:31 +01001Subsampling Zoom Image View
2===========================
davemorrisseyb064b0b2013-08-26 09:45:26 -07003
David Morrissey0e895c22013-08-26 20:07:35 +01004A custom ImageView for Android with pinch to zoom and subsampled tiles to support large images. While zooming in, the
David Morrisseya0bf8022014-05-30 14:08:31 +01005low resolution, full size base layer is overlaid with smaller tiles at least as high resolution as the screen, and
David Morrissey260aec22013-08-29 23:31:20 +01006tiles are loaded and discarded during panning to avoid holding too much bitmap data in memory.
David Morrissey0e895c22013-08-26 20:07:35 +01007
David Morrissey260aec22013-08-29 23:31:20 +01008Ideal for use in image gallery apps where the size of the images may be large enough to require subsampling, and where
9pinch to zoom is required to view the high resolution detail.
10
David Morrisseya0bf8022014-05-30 14:08:31 +010011*This view doesn't extend `ImageView` and isn't intended as a general replacement for it. It's specialized for the display of much larger images than `ImageView` can display, so it's perfect for image galleries and displaying images from the camera.*
David Morrissey0e895c22013-08-26 20:07:35 +010012
David Morrisseya0bf8022014-05-30 14:08:31 +010013#### Image display
14* Display huge images, larger than can be loaded into memory
15* Show high resolution detail on zooming in
16* Tested up to 20,000x13,000px, though larger images are slower
17* Display images from assets or the file system
18* Automatically rotate images from the file system (e.g. the camera or gallery) according to EXIF
19* Manually rotate images in 90° increments
20* Swap images at runtime
David Morrissey0e895c22013-08-26 20:07:35 +010021
David Morrisseya0bf8022014-05-30 14:08:31 +010022#### Gesture detection
23* One finger pan
24* Two finger pinch to zoom
25* Pan while zooming
26* Seamless switch between pan and zoom
27* Fling momentum after panning
David Morrissey02ceb3d2014-05-30 20:48:51 +010028* Options to disable pan and/or zoom gestures
David Morrisseya0bf8022014-05-30 14:08:31 +010029
30#### Overridable event detection
31* Supports `OnClickListener` and `OnLongClickListener`
32* Supports interception of events using `GestureDetector` and `OnTouchListener`
33* Extend to add your own gestures
34
35#### Easy integration
36* Use within a `ViewPager` to create a photo gallery
37* Easily restore scale, center and orientation after screen rotation
38* Can be extended to add overlay graphics that move and scale with the image
39* Handles view resizing and `wrap_content` layout
40
41#### Coming soon...
42* Gradle library structure
43* Better support for very tall or wide images
44* Double tap to zoom
45* Demonstration app
46
47#### Limitations
David Morrissey0e895c22013-08-26 20:07:35 +010048* Requires SDK 10 (Gingerbread).
David Morrisseya0bf8022014-05-30 14:08:31 +010049* No support for decoding an image from resources - the image file needs to be in assets or external storage.
David Morrissey260aec22013-08-29 23:31:20 +010050* This view does not extend ImageView so attributes including android:tint, android:scaleType and android:src are not supported.
David Morrisseya0bf8022014-05-30 14:08:31 +010051* 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 :-)
52
53## Basic setup
54
55Checkout 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.
56
57Add the view to your layout XML as shown below. Normally you should set width and height to `match_parent`.
58
59 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
60 android:layout_width="match_parent"
61 android:layout_height="match_parent" >
62
63 <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
64 android:id="@+id/imageView"
David Morrisseye11ee3e2014-05-30 14:09:41 +010065 android:layout_width="match_parent"
66 android:layout_height="match_parent"/>
David Morrisseya0bf8022014-05-30 14:08:31 +010067
68 </RelativeLayout>
69
70Now, in your fragment or activity, set the image asset name or file path.
71
72 SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)findViewById(id.imageView);
73 imageView.setImageAsset("map.png");
74 // ... or ...
75 imageView.setImageFile("/sdcard/DCIM/DSCM00123.JPG");
76
77That's it! Keep reading for some more options.
78
79## Define asset name in XML
80
81For a zero code approach to showing an image from your assets, you need to define the custom namespace in your layout.
82
83 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
84 xmlns:ssiv="http://schemas.android.com/apk/res-auto"
85 android:layout_width="match_parent"
86 android:layout_height="match_parent" >
87
88 <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
89 ssiv:assetName="map.png"
David Morrisseye11ee3e2014-05-30 14:09:41 +010090 android:layout_width="match_parent"
91 android:layout_height="match_parent"/>
David Morrisseya0bf8022014-05-30 14:08:31 +010092
93 </RelativeLayout>
94
95**This method doesn't support restoring state after a screen orientation change.**
96
97## Handle screen orientation changes
98
99If 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.
100
101 private static final String BUNDLE_STATE = "ImageViewState";
102
103 @Override
104 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
105 View rootView = inflater.inflate(R.layout.my_fragment, container, false);
106
107 ImageViewState imageViewState = null;
108 if (savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_STATE)) {
109 imageViewState = (ImageViewState)savedInstanceState.getSerializable(BUNDLE_STATE);
110 }
111 SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)rootView.findViewById(id.imageView);
112 imageView.setImageAsset("map.png", imageViewState);
113
114 return rootView;
115 }
116
117 @Override
118 public void onSaveInstanceState(Bundle outState) {
119 View rootView = getView();
120 if (rootView != null) {
121 SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)rootView.findViewById(id.imageView);
122 ImageViewState state = imageView.getState();
123 if (state != null) {
124 outState.putSerializable(BUNDLE_STATE, imageView.getState());
125 }
126 }
127 }
128
129## Extending functionality
130
131Take a look at the sample app for a simple implementation showing the view in a `ViewPager` with an `OnClickListener` and an `OnLongClickListener`. I'll add more examples to this soon.