Skip to content
Related Articles

Related Articles

PhotoView in Android
  • Difficulty Level : Medium
  • Last Updated : 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: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.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
My Personal Notes arrow_drop_up
Recommended Articles
Page :