Zoom in animation on Button Click in Android Studio



Step 1: Create a anim folder in res. Also create a file in anim folder named zoomin. If you don't know how to creat anim folder and files in it. Just visit here to learn.

Create animation Folder and file



2. zoomin.xml


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


3.activity_main.xml
----- put an Image in your xml file with ImageView. Here I add ss2 from my drawable file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hp.symonapps.MainActivity"
    android:orientation="vertical"
    android:gravity="center">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv"
        android:src="@drawable/ss2"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Zoom In"
        android:id="@+id/b_zoom"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="45dp" />


</LinearLayout>






3. MainActivity.java


package com.example.hp.symonapps;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {

    Button b_zoom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b_zoom = (Button) findViewById(R.id.b_zoom); //cast the b_zoom button
        b_zoom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                ImageView iv=(ImageView)findViewById(R.id.iv); //cast the iv imageView button
                Animation anim=AnimationUtils.loadAnimation(MainActivity.this, R.anim.zoomin);
                iv.setAnimation(anim);


            }
        });





    } //onCreate end


}

Comments