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

Android Ex 09

Android animation code

Uploaded by

TRANAND TR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Android Ex 09

Android animation code

Uploaded by

TRANAND TR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Ex. 9 - Create animation effects on a mobile app.

Step 1: Create a new android application ‘MyAnimation’.

Step 2: Write the code like as shown below in the main.xml


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout 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/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/flowers1" />

<ImageView
android:id="@+id/img2"
android:layout_width="match_parent"
android:layout_height="218dp"
app:srcCompat="@drawable/bird" />

<ImageView
android:id="@+id/img3"
android:layout_width="match_parent"
android:layout_height="218dp"
android:layout_below="@+id/imageButton2"
android:layout_marginTop="263dp"
app:srcCompat="@drawable/butterfly" />
</RelativeLayout>
Step 3: Create a new folder ‘anim’ of type ‘anim’ inside the ‘/res’ directory.

Step 4: Create new animation resource file fade.xml inside the created ‘/anim’
directory.

fade.xml

<?xml version="1.0" encoding="utf-8"?>


<set
xmlns:android=http://schemas.android.com/apk/res/android
android:interpolator="@android:anim/accelerate_interpolator" >
<alpha
android:fromAlpha="0" android:toAlpha="1" android:duration="2000" >
</alpha>
<alpha
android:startOffset="2000" android:fromAlpha="1" android:toAlpha="0"
android:duration="2000" >
</alpha>
</set>

Step 5: Create new animation resource file blink.xml inside the created ‘/anim’
directory.

blink.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0" android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="600"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>

Step 6: Create new animation resource file slide.xml inside the created ‘/anim’
directory.
Slide.xml

<?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>

Step 7: Write the code like as shown below in the MainActivity.java

import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
ImageView image1, image2, image3;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1 = (ImageView) findViewById(R.id.img1);
image2 = (ImageView) findViewById(R.id.img2);
image3 = (ImageView) findViewById(R.id.img3);
Animation anim1 =
AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade);

image2.startAnimation(anim1);

Animation anim2 =
AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide);

image1.startAnimation(anim2);

Animation anim3 =
AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blink);

image3.startAnimation(anim3);

}
}

You might also like