Introduction

This article explains how to do ZoomIn and ZoomOut in Android.
In this you will use a ImaegView on which you perform ZoomIn and ZoomOut on a button click. To perform animation you will use the Animation class object to set the animation on the Imageview. In this you will create a folder name anim inside the res folder and create two XML files inside the anim folder to write how much an image should be ZoomOut and ZoomIn.
Step 1
Create a new project like this:
4.jpg
Step 2
Create an XML file and write the following code.
In this, you will use an ImageView and two Buttons inside the LinearLayout of which the orientation is set to vertical.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity"
  10. android:background="#E0A51B">
  11. <LinearLayout
  12. android:layout_height="wrap_content"
  13. android:layout_width="fill_parent"
  14. android:orientation="vertical">
  15. <ImageView
  16. android:id="@+id/imageVIEW"
  17. android:layout_width="fill_parent"
  18. android:layout_height="150dp"
  19. android:background="@drawable/man_of_steel"
  20. android:layout_centerHorizontal="true"
  21. >
  22. </ImageView>
  23. <Button
  24. android:id="@+id/btnZoom_In"
  25. android:layout_height="wrap_content"
  26. android:layout_width="150dp"
  27. android:text="StartZoomIn"
  28. android:layout_centerHorizontal="true"
  29. android:layout_marginLeft="70dp"
  30. android:layout_marginTop="40dp"
  31. />
  32. <Button
  33. android:id="@+id/btn_Zoom_out"
  34. android:layout_height="wrap_content"
  35. android:layout_width="150dp"
  36. android:text="StartZoomOut"
  37. android:layout_centerHorizontal="true"
  38. android:layout_marginLeft="70dp"
  39. android:layout_marginTop="40dp"
  40. />
  41. </LinearLayout>
  42. </RelativeLayout>
Step 3
Now you will create a folder named anim inside the res folder. And inside the res folder, you will create two XML files, zoom_in, and zoom_out.
In the zoom_in.xml file you will write this:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:fillAfter="true" >
  4. <scale
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:duration="2000"
  7. android:fromXScale="1"
  8. android:fromYScale="1"
  9. android:pivotX="50%"
  10. android:pivotY="50%"
  11. android:toXScale="3"
  12. android:toYScale="3" >
  13. </scale>
  14. </set>
In zoom_out XML file you will write this
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:fillAfter="true" >
  4. <scale
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:duration="1000"
  7. android:fromXScale="1.0"
  8. android:fromYScale="1.0"
  9. android:pivotX="50%"
  10. android:pivotY="50%"
  11. android:toXScale="0.5"
  12. android:toYScale="0.5" >
  13. </scale>
  14. </set>
Step 4
Create a Java class file and write the following code.
In this class file first, you will create the id of the image view, button, and an Animation reference variable. The Animation reference variable will hold the object that will be returned by the loadAnimation method() method. After getting the object of the animation call setAnimationListener () will set the listener to the animation. Use a button and set it on setonClicklIstener to perform the animation on button click. Inside the OnClick method set the imageview on startAniamtion() method to start the animation on the button click.
  1. package com.zoominactivity;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.animation.Animation;
  6. import android.view.animation.AnimationUtils;
  7. import android.widget.Button;
  8. import android.widget.ImageView;
  9. public class MainActivity extends Activity implements Animation.AnimationListener {
  10. ImageView imageview;
  11. Button button1,button2;
  12. Animation animation,animation2;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. imageview=(ImageView)findViewById(R.id.imageVIEW);
  18. button1=(Button)findViewById(R.id.btnZoom_In);
  19. button2=(Button)findViewById(R.id.btn_Zoom_out);
  20. animation= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoom_in);
  21. animation2= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoom_out);
  22. animation.setAnimationListener(this);
  23. button1.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View view) {
  26. imageview.startAnimation(animation);
  27. }
  28. });
  29. button2.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View view) {
  32. imageview.startAnimation(animation2);
  33. }
  34. });
  35. }
  36. @Override
  37. public void onAnimationStart(Animation animation) {
  38. }
  39. @Override
  40. public void onAnimationEnd(Animation animation) {
  41. }
  42. @Override
  43. public void onAnimationRepeat(Animation animation) {
  44. }
  45. }
Step 5
When you run your project:
1.jpg
Step 6
After clicking on the zoomin button:
2.jpg
Step 7
After clicking on the zoomout button:
3.jpg
So in this article, you have learned how to perform zoom in and zoom out operations on Imaheview on a button click.