You are on page 1of 2

Animation Zoom in and out

Xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher_foreground"
android:scaleType="fitCenter"
android:clickable="true"
android:onClick="zoomIn" />

</LinearLayout>

Mainactivity.java
package com.example.animapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

private ImageView imageView;


private Animation scaleUpAnimation;
private Animation scaleDownAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
scaleUpAnimation = AnimationUtils.loadAnimation(this,
R.anim.scale_up);
scaleDownAnimation = AnimationUtils.loadAnimation(this,
R.anim.scale_down);
}

public void zoomIn(android.view.View view) {


imageView.startAnimation(scaleUpAnimation);
}

public void zoomOut(android.view.View view) {


imageView.startAnimation(scaleDownAnimation);
}
}
Xml resource file

Scale_up.xml
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.5"
android:toYScale="1.5"
android:duration="500"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false" />

Scale_down.xml
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.5"
android:fromYScale="1.5"
android:toXScale="1.0"
android:toYScale="1.0"
android:duration="500"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false" />

You might also like