commit | 02ceb3d02408536a50aca2244c6e24765d0f52f1 | [log] [tgz] |
---|---|---|
author | David Morrissey <[email protected]> | Fri May 30 20:48:51 2014 +0100 |
committer | David Morrissey <[email protected]> | Fri May 30 20:48:51 2014 +0100 |
tree | e105c41fa8b5671244ec7bc196775bc684e60353 | |
parent | e11ee3ea122ba703ea5f3391bd4305aa186f7831 [diff] |
Options to disable pan and zoom gestures
A custom ImageView for Android with pinch to zoom and subsampled tiles to support large images. While zooming in, the low resolution, full size base layer is overlaid with smaller tiles at least as high resolution as the screen, and tiles are loaded and discarded during panning to avoid holding too much bitmap data in memory.
Ideal for use in image gallery apps where the size of the images may be large enough to require subsampling, and where pinch to zoom is required to view the high resolution detail.
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.
OnClickListener
and OnLongClickListener
GestureDetector
and OnTouchListener
ViewPager
to create a photo gallerywrap_content
layoutCheckout 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.
Add the view to your layout XML as shown below. Normally you should set width and height to match_parent
.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
Now, in your fragment or activity, set the image asset name or file path.
SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)findViewById(id.imageView); imageView.setImageAsset("map.png"); // ... or ... imageView.setImageFile("/sdcard/DCIM/DSCM00123.JPG");
That's it! Keep reading for some more options.
For a zero code approach to showing an image from your assets, you need to define the custom namespace in your layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ssiv="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView ssiv:assetName="map.png" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
This method doesn't support restoring state after a screen orientation change.
If 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.
private static final String BUNDLE_STATE = "ImageViewState"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.my_fragment, container, false); ImageViewState imageViewState = null; if (savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_STATE)) { imageViewState = (ImageViewState)savedInstanceState.getSerializable(BUNDLE_STATE); } SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)rootView.findViewById(id.imageView); imageView.setImageAsset("map.png", imageViewState); return rootView; } @Override public void onSaveInstanceState(Bundle outState) { View rootView = getView(); if (rootView != null) { SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)rootView.findViewById(id.imageView); ImageViewState state = imageView.getState(); if (state != null) { outState.putSerializable(BUNDLE_STATE, imageView.getState()); } } }
Take 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.