Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
Implement Zoom In or Zoom Out in Android
Next article icon

Implement Zoom In or Zoom Out in Android

Last Updated : 16 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Zoom In and Zoom Out animations are used to enlarge and reduce the size of a view in Android applications respectively. These types of animations are often used by developers to provide a dynamic nature to the applications. Users also feel the changes happening in the application by watching these kinds of animations. 

zoom-inout

XML Attributes of Scale Tag

The characteristics of Zoom In and Zoom Out animations are defined in the XML files by using scale tag. 

XML attributeDescription
android:durationUsed to define the duration of the animation in millisecond
android:fromXScaleUsed to set initial size of the view in X-axis
android:fromYScaleUsed to set initial size of the view in Y-axis
android:pivotXTo define the X coordinate of the point about which the object is being zoom in/out
android:pivotYTo define the Y coordinate of the point about which the object is being zoom in/out
android:toXScaleUsed to set final size of the view in X-axis
android:toYScaleUsed to set final size of the view in Y-axis

How to Add Zoom In/Out Animation in Android

The following example demonstrates the steps involved in implementing Zoom In and Zoom Out animation to an image file. An image file will be added in the activity using ImageView.

Note: Following steps are performed on Android Studio version 4.0 
 

Step 1: Create new project 

  1. Click on File, then New => New Project.
  2. Select language as Kotlin.
  3. Select the minimum SDK as per your need.

Step 2: Modify activity_main.xml file
Below is the code for activity_main.xml file to add a TextView, ImageView and two Buttons in an activity.

Filename: activity_main.xml

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"     android:background="#168BC34A"     tools:context=".MainActivity" >      <TextView         android:id="@+id/textView"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:fontFamily="@font/roboto"         android:text="@string/heading"         android:textAlignment="center"         android:textColor="@android:color/holo_green_light"         android:textSize="36sp"         android:textStyle="bold"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.060000002" />      <ImageView         android:id="@+id/imageView"         android:layout_width="179dp"         android:layout_height="172dp"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@+id/textView"         app:layout_constraintVertical_bias="0.31"         app:srcCompat="@drawable/logo" />      <Button         android:id="@+id/zoomInButton"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="#AB4CAF50"         android:fontFamily="@font/roboto"         android:padding="5dp"         android:text="@string/zoomInButtonText"         android:textSize="18sp"         android:textStyle="bold"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toStartOf="@+id/zoomOutButton"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@+id/imageView"         app:layout_constraintVertical_bias="0.76" />      <Button         android:id="@+id/zoomOutButton"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="#AB4CAF50"         android:fontFamily="@font/roboto"         android:padding="5dp"         android:text="@string/zoomOutButtonText"         android:textSize="18sp"         android:textStyle="bold"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintHorizontal_bias="0.77"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@+id/imageView"         app:layout_constraintVertical_bias="0.76" /> </androidx.constraintlayout.widget.ConstraintLayout> 

Step 3: Define XML file for Zoom In and Zoom Out animation of the image
Create a new directory in the res folder of the application through right-click on res => New => Android Resource Directory. Select Resource Type as anim and Directory name should also be anim. In this directory create 2 Animation Resource File namely zoom_in and zoom_out. These 2 files are the XML file which holds the details of the animation. Below is the code for both the file.

Filename: zoom_in.xml

XML
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"     android:fillAfter = "true">      <scale         xmlns:android = "http://schemas.android.com/apk/res/android"         android:duration = "1000"         android:fromXScale = "1"         android:fromYScale = "1"         android:pivotX = "50%"         android:pivotY = "50%"         android:toXScale = "2"         android:toYScale = "2"/>  </set> 

Filename: zoom_out.xm

XML
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"     android:fillAfter = "true">      <scale         xmlns:android="http://schemas.android.com/apk/res/android"         android:duration = "2500"         android:fromXScale = "1"         android:fromYScale = "1"         android:pivotX = "50%"         android:pivotY = "50%"         android:toXScale = ".2"         android:toYScale = ".2" />  </set> 

The android:fillAfter attribute under set tag is used to fix the final size of the image file until any other animation happens. 
 

Step 4: Modify MainActivity.kt file
Below is the code for MainActivity.kt file to load and start the animation on the ImageView widget according to the button clicked by the user.

Filename: MainActivity.kt

Java
package com.example.zomminout   import android.os.Bundle import android.view.animation.AnimationUtils import android.widget.Button import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity     class MainActivity : AppCompatActivity() {       override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)           // assigning id of the button         // which zoom in the image         val buttonZoomIn: Button = findViewById(R.id.zoomInButton)           // assigning id of the button         // which zoom out the image         val buttonZoomOut: Button = findViewById(R.id.zoomOutButton)           // assigning id of imageview on         // which zoom in/out will be performed         val image: ImageView = findViewById(R.id.imageView)           // actions to be performed when         // "Zoom In" button is clicked         buttonZoomIn.setOnClickListener() {               // loading the animation of             // zoom_in.xml file into a variable             val animZoomIn = AnimationUtils.loadAnimation(this,                                                 R.anim.zoom_in)             // assigning that animation to             // the image and start animation             image.startAnimation(animZoomIn)         }           // actions to be performed when         // "Zoom Out" button is clicked         buttonZoomOut.setOnClickListener() {               // loading the animation of             // zoom_out.xml file into a variable             val animZoomOut = AnimationUtils.loadAnimation(this,                                                 R.anim.zoom_out)               // assigning that animation to             // the image and start animation             image.startAnimation(animZoomOut)         }     } } 

Step 5: Modify strings.xml file
All the strings which are used in the activity are listed in this file.

Filename: strings.xml

XML
<resources>     <string name="app_name">ZoomInOut</string>     <string name="heading">Zoom In/Out in Android</string>     <string name="zoomOutButtonText">Zoom Out</string>     <string name="zoomInButtonText">Zoom In</string> </resources> 

Output: Run on Emulator


Next Article
Implement Zoom In or Zoom Out in Android

R

RISHU_MISHRA
Improve
Article Tags :
  • Kotlin
  • Android
  • Android-Animation

Similar Reads

    How to Implement Zoom In or Zoom Out in Android?
    ImageView in Android is used to display images of different formats. Images are set according to the size of ImageView irrespective of the size of the image. However, ImageView does not provide a provision to zoom in or zoom out of the image.  So in this article, we will show you how you could creat
    2 min read
    How to Implement TNImage Library in Android?
    Android is an open-source operating system, based on the Linux kernel and used in mobile devices like smartphones, tablets, etc. Further, it was developed for smartwatches and Android TV. Each of them has a specialized interface. Android has been one of the best-selling OS for smartphones. Android O
    3 min read
    How to implement Picture in Picture (PIP) in Android?
    In this article, it is explained how to implement a Picture in Picture (PIP) in an Android application. We have seen in many apps such as Google Maps while using navigation that when we close the app, there is a floating screen that appears at the bottom right of the screen as shown in the image bel
    3 min read
    ZoomControls in Android with Example
    In Android, Zoom Control is a class that has some set of methods that are used to control the zoom functionality. It has two buttons that are used to control the zoom functionality (ie Zoom In and Zoom Out). Zoom Control Class has been deprecated in API Version 29. The functionalities offered by Zoo
    5 min read
    How to Implement Item Click Interface in Android?
    When we click on an item in an application either it gives some information or it redirects the user to any other page. In this article, we will learn that how we can implement Item Click Interface in an android application. What we are going to build in this article?In this article, we will be usin
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences