Professional Documents
Culture Documents
Animations in Andriod
Animations in Andriod
Example + Quiz 3
Animation
• Animation is the process of adding a motion effect to any view,
image, or text. With the help of an animation, you can add motion or
can change the shape of a specific view. Animation in Android is
generally used to give your UI a rich look and feel
Types of animations
• The animations are basically of three types as follows:
1. Property Animation
2. View Animation
3. Drawable Animation
1. Property Animation
• Property Animation is one of the robust frameworks which allows
animating almost everything. This is one of the powerful and flexible
animations which was introduced in Android 3.0. Property animation
can be used to add any animation in the CheckBox, RadioButtons, and
widgets other than any view.
2. View Animation
• View Animation can be used to add animation to a specific view to
perform tweened animation (Tweened animation, also known as
"tweening," is a technique used in animation to create smooth
transitions between keyframes) on views. Tweened animation
calculates animation information such as size, rotation, start point,
and endpoint. These animations are slower and less flexible. An
example of View animation can be used if we want to expand a
specific layout in that place we can use View Animation. The example
of View Animation can be seen in Expandable RecyclerView.
3. Drawable Animation
• Drawable Animation is used if you want to animate one image over
another. The simple way to understand is to animate drawable is to
load the series of drawable one after another to create an animation.
A simple example of drawable animation can be seen in many apps
Splash screen on apps logo animation.
Important Methods of Animation
• Strings.xml can be found from the app > res > values > strings.xml. Below
is the snippet for the strings.xml file.
String.xml file code
<resources>
<string name="app_name">app</string>
<string name="blink">BLINK</string>
<string name="clockwise">ROTATE</string>
<string name="fade">FADE</string>
<string name="move">MOVE</string>
<string name="slide">SLIDE</string>
<string name="zoom">ZOOM</string>
<string name="stop_animation">STOP ANIMATION</string>
</resources>
Optional (if throw error remove it)
• Step 3: Add google repository in the build.gradle file of the application project if
by default it is not there
buildscript {
repositories {
google()
mavenCentral()
}
• google(): This is a shorthand method to add Google's Maven repository. It hosts
Android libraries and tools provided by Google.
• mavenCentral(): This adds the Maven Central repository, a widely used repository
for Java and Android libraries.
Optional (if throw error remove it)
• Step 3: Add google repository in the build.gradle file of the application
project if by default it is not there
All Jetpack components are available in the Google Maven repository, include
them in the build.gradle file
allprojects {
repositories {
google()
mavenCentral()
}
}
Jetpack (often stylized as Jetpack or Android Jetpack) is a suite of
libraries, tools, and architectural guidance provided by Google to help
Android developers build robust, high-quality apps more efficiently. It is
designed to simplify complex development task
• Step 4: Working with the activity_main.xml file
• Create ImageView in the activity_main.xml along with buttons that
will add animation to the view. Navigate to the app > res > layout >
activity_main.xml. Below is the code for the activity_main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".MainActivity">
<ImageView
android:id="@+id/imageview"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:contentDescription="@string/app_name"
android:src="@drawable/bunny" />
<LinearLayout
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imageview"
android:layout_marginTop="30dp"
android:orientation="horizontal"
android:weightSum="3">
</LinearLayout>
<LinearLayout
android:id="@+id/linear2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linear1"
android:layout_marginTop="30dp"
android:orientation="horizontal"
android:weightSum="3">
</LinearLayout>
<!--To stop the animation of the image-->
<Button
android:id="@+id/BTNstop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linear2"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:layout_marginRight="30dp"
android:text="@string/stop_animation" />
</RelativeLayout>
• Step 5: Create 6 different types of animation for ImageView
• To create new animations we have to create a new directory for
storing all our animations. Navigate to the app > res > Right-Click on
res >> New >> Directory >> Name your directory as “anim”. Inside
this directory, we will create our animations. For creating a new anim
right click on the anim directory >> Animation Resource file and give
the name to your file. Below is the code snippet for 6 different
animations.
1) Blink Animation
• alpha: This property defines the transparency level of a view. It ranges from
0.0 (fully transparent) to 1.0 (fully opaque).
• fromAlpha: The starting alpha value of the animation.
• toAlpha: The ending alpha value of the animation.
• In the above code snippet, the alpha tag specifies an animation that
transitions a view's alpha from 0.0 (fully transparent) to 1.0 (fully opaque).
Delta in Animations:
• Delta generally refers to a change in a specific property over time
during an animation. For example, in translation animations, delta
values define how much a view should move from its original
position.
<alpha
android:duration="1000"
android:fromAlpha="1"
android:startOffset="2000"
android:toAlpha="0" />
</set>
Interpolator
• An interpolator in Android defines the rate of change of an animation, influencing the
pacing of the animation. It determines how the animation progresses over time, providing
different effects such as acceleration, deceleration, or repeating patterns. Common
interpolators include:
<translate
android:fromXDelta="0%p"
android:toXDelta="75%p"
android:duration="700" />
</set>
Rotate animation
• A rotate animation in Android involves changing the orientation of an object smoothly over
time. It creates a visual effect where the object appears to rotate around a specified pivot
point. This animation can be used to add dynamism and visual interest to elements within
your Android application.
• Initial Orientation: The object is initially positioned with a certain orientation on the screen.
• Rotation: The object smoothly rotates around a specified pivot point. The rotation can be
clockwise or counterclockwise and can be defined by specifying the starting and ending
angles.
• Final Orientation: Once the animation completes, the object remains in its final rotated
position.
4) Rotate Animation
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="6000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
<rotate
android:duration="6000"
android:fromDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="5000"
android:toDegrees="0" />
</set>
EXPLAINATION
• <rotate>: This defines a rotate animation. The duration attribute
specifies the duration of the animation in milliseconds.
• The fromDegrees attribute specifies the starting rotation angle in
degrees. The pivotX and pivotY attributes specify the pivot point (the
point around which the rotation occurs), where 50% means the
center of the view.
• The toDegrees attribute specifies the final rotation angle in degrees.
• The first <rotate> element rotates the object from 0 degrees to 360
degrees over a duration of 6000 milliseconds (6 seconds). It rotates
around its center (pivotX="50%" and pivotY="50%").
• The second <rotate> element then reverses the rotation. It starts from 360
degrees (the final angle of the previous animation) and rotates back to 0
degrees over a duration of 6000 milliseconds (6 seconds), but with a start
delay of 5000 milliseconds (5 seconds) due to the startOffset attribute.
This means it will start its rotation 5 seconds after the first rotation
completes.
Slide Animation
• A slide animation in Android involves moving an object smoothly from one position to another,
typically horizontally or vertically. It creates a visual effect where the object appears to slide
across the screen. Slide animations are commonly used for transitions between different views
or to reveal hidden content.
• Initial Position: The object is initially located at a specific position on the screen.
• Transition: The object smoothly moves from its initial position to a new position.
• Final Position: Once the animation completes, the object remains at its final position.
• Slide animations can move objects either horizontally (left to right or right to left) or vertically
(upwards or downwards). You can control the duration, acceleration, and deceleration of the
animation to achieve different effects.
5) Slide Animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/linear_interpolator"
android:toXScale="1.0"
android:toYScale="0.0" />
</set>
6) Zoom Animation
• A zoom animation in Android involves changing the scale of an object smoothly over time,
making it appear to grow larger or smaller. This animation creates a visual effect where the
object appears to zoom in or out. Zoom animations can be used to focus attention on specific
elements, provide feedback, or enhance user interactions.
• Initial Scale: The object is initially displayed at a certain scale (size) on the screen.
• Zooming: The object smoothly changes its scale, either increasing (zooming in) or decreasing
(zooming out) over a specified duration.
• Final Scale: Once the animation completes, the object remains at its final scale.
6) Zoom Animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
android:interpolator="@android:anim/linear_interpolator"
android:duration = "1000"
android:fromXScale = "1"
android:fromYScale = "1"
android:pivotX = "50%"
android:pivotY = "50%"
android:toXScale = "2"
android:toYScale = "2"/>
</set>
• A value of 2 means the view will be scaled to twice its original size.
• A value of 1 means no scaling, indicating that the view initially
appears at its original size.
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;
ImageView imageView;
Button blinkBTN, rotateBTN, fadeBTN, moveBTN, slideBTN, zoomBTN, stopBTN;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageview);
blinkBTN = findViewById(R.id.BTNblink);
rotateBTN = findViewById(R.id.BTNrotate);
fadeBTN = findViewById(R.id.BTNfade);
moveBTN = findViewById(R.id.BTNmove);
slideBTN = findViewById(R.id.BTNslide);
zoomBTN = findViewById(R.id.BTNzoom);
stopBTN = findViewById(R.id.BTNstop);
• imageView = findViewById(R.id.imageview);: This line initializes an
ImageView variable named imageView by finding the view with the
specified id (R.id.imageview) from the activity's layout. This is typically
used to get a reference to an ImageView defined in the layout XML
file.
• Below code snippets sets up a click listener for a button (assuming
blinkBTN is a reference to a button in your Android application's
layout XML file). When the button is clicked, it triggers an animation
on an image view (imageView).
blinkBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// To add blink animation
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blink);
imageView.startAnimation(animation);
}
});
Explaination
• blinkBTN.setOnClickListener(...): This sets up a click listener for the button
named blinkBTN. When the button is clicked, the code inside the onClick
method will execute.
• public void onClick(View v) { ... }: This is the onClick method, which gets
called when the button is clicked. Inside this method, you define what should
happen when the button is clicked.
Explaination
• Animation animation =
AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.blink);: Here, an animation is loaded from an XML resource
file named blink.xml. This file presumably contains the definition for
the animation effect you want to apply.