PhotoView
Implementation of ImageView for Android that supports zooming, by various touch gestures.
Top Related Projects
图片浏览缩放控件
A simple and customizable Android full-screen image viewer with shared image transition support, "pinch to zoom" and "swipe to dismiss" gestures
Android library (AAR). Highly configurable, easily extendable deep zoom view for displaying huge images without loss of detail. Perfect for photo galleries, maps, building plans etc.
A simple and customizable Android full-screen image viewer 一个简单且可自定义的Android全屏图像浏览器
Adds touch functionality to Android ImageView.
Quick Overview
PhotoView is an Android library that provides a zoomable ImageView for displaying high-resolution images. It supports multi-touch gestures for zooming and panning, and offers smooth animations for a seamless user experience. The library is designed to be easily integrated into existing Android projects.
Pros
- Easy integration with existing Android projects
- Smooth zooming and panning animations
- Support for multi-touch gestures
- Customizable zoom levels and boundaries
Cons
- Limited to Android platform only
- May have performance issues with extremely large images
- Lacks advanced features like image editing or filters
- Requires additional setup for loading images from network sources
Code Examples
- Basic implementation of PhotoView:
import com.github.chrisbanes.photoview.PhotoView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val photoView: PhotoView = findViewById(R.id.photo_view)
photoView.setImageResource(R.drawable.my_image)
}
}
- Setting maximum and minimum scale:
photoView.maximumScale = 5f
photoView.mediumScale = 3f
photoView.minimumScale = 1f
- Adding a listener for scale changes:
photoView.setOnScaleChangeListener { scaleFactor, focusX, focusY ->
Log.d("PhotoView", "Scale changed to: $scaleFactor")
}
Getting Started
- Add the dependency to your app's
build.gradle
file:
dependencies {
implementation 'com.github.chrisbanes:PhotoView:2.3.0'
}
- Add PhotoView to your layout XML:
<com.github.chrisbanes.photoview.PhotoView
android:id="@+id/photo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
- In your Activity or Fragment, initialize PhotoView and set an image:
import com.github.chrisbanes.photoview.PhotoView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val photoView: PhotoView = findViewById(R.id.photo_view)
photoView.setImageResource(R.drawable.my_image)
}
}
Competitor Comparisons
图片浏览缩放控件
Pros of PhotoView (bm-x)
- More recent updates and active maintenance
- Supports custom gestures and animations
- Includes built-in image loading and caching
Cons of PhotoView (bm-x)
- Less comprehensive documentation
- Fewer stars and forks on GitHub
- May have a steeper learning curve for beginners
Code Comparison
PhotoView (Baseflow):
PhotoView photoView = findViewById(R.id.photo_view);
photoView.setImageResource(R.drawable.image);
photoView.setScaleType(ImageView.ScaleType.CENTER_CROP);
PhotoView (bm-x):
PhotoView photoView = findViewById(R.id.photo_view);
photoView.setImageResource(R.drawable.image);
photoView.enable();
photoView.setScaleType(ImageView.ScaleType.CENTER_CROP);
photoView.setAnimaDuring(300);
Both libraries provide similar basic functionality for displaying and zooming images. However, PhotoView (bm-x) offers additional features like custom animations and gestures. The code structure is similar, with PhotoView (bm-x) requiring an extra enable()
call and providing more customization options like setAnimaDuring()
.
While PhotoView (Baseflow) has a larger community and more extensive documentation, PhotoView (bm-x) offers more recent updates and additional features. The choice between the two depends on specific project requirements and the developer's familiarity with each library.
A simple and customizable Android full-screen image viewer with shared image transition support, "pinch to zoom" and "swipe to dismiss" gestures
Pros of StfalconImageViewer
- More modern UI with smoother transitions and animations
- Built-in support for image downloading and sharing
- Customizable overlay views for additional information or controls
Cons of StfalconImageViewer
- Less mature and potentially less stable than PhotoView
- Fewer configuration options for zooming and panning behavior
- Limited documentation compared to PhotoView
Code Comparison
StfalconImageViewer:
val images = listOf(imageUrl1, imageUrl2, imageUrl3)
StfalconImageViewer.Builder<String>(this, images) { imageView, image ->
Glide.with(this).load(image).into(imageView)
}.show()
PhotoView:
PhotoView photoView = findViewById(R.id.photo_view);
Glide.with(this).load(imageUrl).into(photoView);
photoView.setMaximumScale(5.0f);
photoView.setMediumScale(3.0f);
StfalconImageViewer offers a more concise API for displaying multiple images in a gallery-like viewer, while PhotoView provides more granular control over individual image views. StfalconImageViewer integrates well with image loading libraries like Glide, similar to PhotoView. However, PhotoView allows for more fine-tuned scaling options directly on the view itself.
Android library (AAR). Highly configurable, easily extendable deep zoom view for displaying huge images without loss of detail. Perfect for photo galleries, maps, building plans etc.
Pros of subsampling-scale-image-view
- Efficient handling of large images through subsampling
- Better memory management for very large images
- Supports custom overlay views
Cons of subsampling-scale-image-view
- Less actively maintained compared to PhotoView
- Fewer built-in features and customization options
- Steeper learning curve for implementation
Code Comparison
PhotoView:
val photoView = findViewById<PhotoView>(R.id.photo_view)
photoView.setImageResource(R.drawable.image)
photoView.maximumScale = 10f
photoView.mediumScale = 5f
photoView.minimumScale = 1f
subsampling-scale-image-view:
val imageView = findViewById<SubsamplingScaleImageView>(R.id.imageView)
imageView.setImage(ImageSource.resource(R.drawable.image))
imageView.maxScale = 10f
imageView.minScale = 1f
imageView.setOnImageEventListener(object : SubsamplingScaleImageView.OnImageEventListener {
// Implement event listeners
})
Both libraries offer zooming and panning capabilities for images in Android applications. PhotoView is more user-friendly and feature-rich, while subsampling-scale-image-view excels at handling very large images efficiently. The choice between them depends on specific project requirements and image sizes being used.
A simple and customizable Android full-screen image viewer 一个简单且可自定义的Android全屏图像浏览器
Pros of ImageViewer
- More recent updates and active development
- Supports video playback in addition to images
- Offers a wider range of customization options
Cons of ImageViewer
- Less mature and potentially less stable than PhotoView
- Smaller community and fewer contributors
- Documentation is not as comprehensive
Code Comparison
PhotoView:
photoView.setOnPhotoTapListener { view, x, y ->
// Handle photo tap
}
ImageViewer:
imageViewer.setOnImageChangeListener { position ->
// Handle image change
}
Both libraries provide similar functionality for image viewing and zooming, but they differ in their implementation and additional features. PhotoView focuses primarily on image viewing with zooming capabilities, while ImageViewer offers a broader range of features, including video support.
PhotoView has been around longer and has a larger user base, which may contribute to its stability and reliability. However, ImageViewer's more recent development and active maintenance could mean it's more up-to-date with current Android best practices.
The choice between these libraries depends on specific project requirements. If you need video support and more customization options, ImageViewer might be the better choice. If you prioritize stability and a well-established library, PhotoView could be more suitable.
Adds touch functionality to Android ImageView.
Pros of TouchImageView
- Simpler implementation with fewer dependencies
- Lightweight and easy to integrate into existing projects
- Supports custom touch events and gestures
Cons of TouchImageView
- Less actively maintained compared to PhotoView
- Fewer advanced features and customization options
- Limited documentation and examples
Code Comparison
TouchImageView:
TouchImageView imageView = findViewById(R.id.touch_image_view);
imageView.setImageResource(R.drawable.my_image);
imageView.setMaxZoom(4f);
PhotoView:
PhotoView photoView = findViewById(R.id.photo_view);
photoView.setImageResource(R.drawable.my_image);
photoView.setMaximumScale(5f);
photoView.setMediumScale(3f);
Both libraries provide similar basic functionality for zooming and panning images. TouchImageView offers a more straightforward approach with fewer lines of code, while PhotoView provides additional customization options like medium scale.
TouchImageView is ideal for simple image viewing needs, while PhotoView is better suited for more complex applications requiring advanced features and customization. PhotoView has a larger community and more frequent updates, making it a more robust choice for long-term projects. However, TouchImageView's simplicity and ease of integration make it a good option for smaller projects or those with limited requirements.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
PhotoView
PhotoView aims to help produce an easily usable implementation of a zooming Android ImageView.
[
Dependency
Add this in your root build.gradle
file (not your module build.gradle
file):
allprojects {
repositories {
maven { url "https://www.jitpack.io" }
}
}
buildscript {
repositories {
maven { url "https://www.jitpack.io" }
}
}
Then, add the library to your module build.gradle
dependencies {
implementation 'com.github.chrisbanes:PhotoView:latest.release.here'
}
Features
- Out of the box zooming, using multi-touch and double-tap.
- Scrolling, with smooth scrolling fling.
- Works perfectly when used in a scrolling parent (such as ViewPager).
- Allows the application to be notified when the displayed Matrix has changed. Useful for when you need to update your UI based on the current zoom/scroll position.
- Allows the application to be notified when the user taps on the Photo.
Usage
There is a sample provided which shows how to use the library in a more advanced way, but for completeness, here is all that is required to get PhotoView working:
<com.github.chrisbanes.photoview.PhotoView
android:id="@+id/photo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);
photoView.setImageResource(R.drawable.image);
That's it!
Issues With ViewGroups
There are some ViewGroups (ones that utilize onInterceptTouchEvent) that throw exceptions when a PhotoView is placed within them, most notably ViewPager and DrawerLayout. This is a framework issue that has not been resolved. In order to prevent this exception (which typically occurs when you zoom out), take a look at HackyDrawerLayout and you can see the solution is to simply catch the exception. Any ViewGroup which uses onInterceptTouchEvent will also need to be extended and exceptions caught. Use the HackyDrawerLayout as a template of how to do so. The basic implementation is:
public class HackyProblematicViewGroup extends ProblematicViewGroup {
public HackyProblematicViewGroup(Context context) {
super(context);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
try {
return super.onInterceptTouchEvent(ev);
} catch (IllegalArgumentException e) {
//uncomment if you really want to see these errors
//e.printStackTrace();
return false;
}
}
}
Usage with Fresco
Due to the complex nature of Fresco, this library does not currently support Fresco. See this project as an alternative solution.
Subsampling Support
This library aims to keep the zooming implementation simple. If you are looking for an implementation that supports subsampling, check out this project
License
Copyright 2018 Chris Banes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Top Related Projects
图片浏览缩放控件
A simple and customizable Android full-screen image viewer with shared image transition support, "pinch to zoom" and "swipe to dismiss" gestures
Android library (AAR). Highly configurable, easily extendable deep zoom view for displaying huge images without loss of detail. Perfect for photo galleries, maps, building plans etc.
A simple and customizable Android full-screen image viewer 一个简单且可自定义的Android全屏图像浏览器
Adds touch functionality to Android ImageView.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot