PhotoView in Android - GeeksforGeeks

18 Feb, 2021
In this article, PhotoView is added in android. PhotoView aims to help produce an easily usable implementation of a zooming Android ImageView using multi-touch and double-tap. Besides that, it has many more features like it notifies the application when the user taps on the photo or when the displayed matrix gets changed. It provides smooth scrolling even when a scrolling parent container like ViewPager is used. It can be used in the gallery app. Approach: 
Step 1: Add the support Library in your root build.gradle file (not your module build.gradle file). This library jitpack is a novel package repository. It is made for JVM so that any library which is present in github and bigbucket can be directly used in the application.  
XML
             allprojects {            repositories {                   maven { url 'https://jitpack.io' }                }          }           
Step 2: Add the support library in build.gradle file and add the dependency in the dependencies section. It allows the developer to use the PhotoView directly in XML files.  
XML
             dependencies {                implementation 'com.github.chrisbanes:PhotoView:2.0.0'          }          
Step 3: Add the following code in the activity_main.xml file. In this file, PhotoView is added to the layout.
activity_main.xml
   <?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">      <com.github.chrisbanes.photoview.PhotoView        android:id="@+id/photo_view"        android:layout_width="match_parent"        android:layout_height="match_parent"/>  </androidx.constraintlayout.widget.ConstraintLayout>    
Step 4: Add the following code in the MainActivity.java file. In this file, we add image to our PhotoView.  setImageResource() method is used to add the image in PhotoView.
MainActivity.java
            package org.geeksforgeeks.photoView            import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import com.github.chrisbanes.photoview.PhotoView;  public class MainActivity extends AppCompatActivity {      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          PhotoView photoView = (PhotoView)                  findViewById(R.id.photo_view);        photoView.setImageResource(R.drawable.gfg);      }}
Output:
 Refer to the official documentation for more information.
android
Android-View
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
Flexbox-Layout in Android
Retrofit with Kotlin Coroutine in Android
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Stream In Java