0% found this document useful (0 votes)
23 views

Animation Android Code

The document describes an Android application that animates an image view. It includes the XML layout file with an image view and buttons to trigger different animations. It also includes XML animation files for rotating, zooming in/out, and fading in/out effects. The Java code initializes the views and buttons, loads the corresponding animation on button clicks, and starts the animation on the image view.

Uploaded by

CM-Hitesh Tolani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Animation Android Code

The document describes an Android application that animates an image view. It includes the XML layout file with an image view and buttons to trigger different animations. It also includes XML animation files for rotating, zooming in/out, and fading in/out effects. The Java code initializes the views and buttons, loads the corresponding animation on button clicks, and starts the animation on the image view.

Uploaded by

CM-Hitesh Tolani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

AndroidManifest.

xml

No changes

Activity_main.xml
<?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"
android:orientation="vertical"
android:gravity="center"
android:background="@color/black"
tools:context=".MainActivity">

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_launcher_foreground"
/>

<Button
android:id="@+id/clock"
android:text = "Clockwise/Anti Clockwise"
android:textAlignment="viewStart"
android:layout_width="270dp"
android:layout_height="wrap_content"
/>

<Button
android:id="@+id/zoom"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:textAlignment="viewStart"
android:text = "Zoom In/Out"
/>

<Button
android:id="@+id/fade"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:textAlignment="viewStart"
android:text = "Fade In/Out"
/>
</LinearLayout>

Right Click res folder -> New -> Android Resource Directory -> set directory name as preferred and
set resource type to anim

for the animation files

Right click anim folder -> new -> android resource files
Clock.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration = "4000">
</rotate>
<rotate
android:startOffset = "2000"
android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:duration = "4000">
</rotate>
</set>

Fade.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="4000"/>
<alpha
android:startOffset ="2000"
android:fromAlpha="1"
android:toAlpha="0"
android:duration = "4000"/>

</set>

Zoom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration ="4000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="2"
android:toYScale="2"
/>
<scale
android:startOffset = "2000"
android:duration ="4000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.1"
android:toYScale="0.1"
/>
</set>
MainActivity.java
package com.example.q1;

import androidx.appcompat.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 clock,zoom,fade;
Animation animation;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.img);
clock = findViewById(R.id.clock);
zoom = findViewById(R.id.zoom);
fade = findViewById(R.id.fade);
clock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animation =
AnimationUtils.loadAnimation(getApplicationContext(),R.anim.clock);
img.startAnimation(animation);
}
});
zoom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animation =
AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoom);
img.startAnimation(animation);
}
});
fade.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animation =
AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade);
img.startAnimation(animation);
}
});

}
}

You might also like