0% found this document useful (0 votes)
79 views13 pages

Lab Sheet 5: Animation and Fabs: Java Code

The document describes two Android app designs using animations: 1. A floating action button demo that displays additional buttons on click and hides them when clicked again, with toast messages for each button click. 2. An app that scales one image larger and one smaller alternately using zoom animations, with the animations triggering each other in a loop.

Uploaded by

kavya lingutla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views13 pages

Lab Sheet 5: Animation and Fabs: Java Code

The document describes two Android app designs using animations: 1. A floating action button demo that displays additional buttons on click and hides them when clicked again, with toast messages for each button click. 2. An app that scales one image larger and one smaller alternately using zoom animations, with the animations triggering each other in a loop.

Uploaded by

kavya lingutla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Lab Sheet 5 : Animation and FABs

1. Design a Floating action button demo. Initially only the pink button should display
when the the pink button is click the rest of the three buttons should be displayed. When
each button is clicked then taost message should be displayed. When the pink button is
pressed again the three buttons should hide.

JAVA CODE:

pACkAge com.exAmple.floAtingACtionbutton;
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.ToAst;
import
com.google.Android.mAteriAl.floAtingACtionbutton.FloAtingActionButton;
public clAss MAinActivity extends AppCompAtActivity {
FloAtingActionButton fAb1,fAb2,fAb3,fAb4;
@Override
protected void onCreAte(Bundle sAVedInstAnceStAte) {
super.onCreAte(sAVedInstAnceStAte);
setContentView(R.lAyout.ACtivity_mAin);
finAl booleAn[] flAg = {fAlse};
fAb1 = findViewById(R.idf.
Ab1); fAb2 =
findViewById(R.idf. Ab2); fAb3
= findViewById(R.idf. Ab3);
fAb4 = findViewById(R.idf.
Ab4);
finAl AnimAtion in =

AnimAtionUtils.loAdAnimAtion(getApplicAtionContext(),
R.Anim.show); finAl AnimAtion out =

AnimAtionUtils.loAdAnimAtion(getApplicAtionContext(),R.Anim.hide);
fAb1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (flAg[0]) {
ToAst.mAkeText(getApplicAtionContext()
, "fbA1",
ToAst.LENGTtf_StfORT).show();
fAb2.stArtAnimAtion(in);
fAb3.stArtAnimAtion(in);
fAb4.stArtAnimAtion(in);
}
else
{
fAb2.stArtAnimAtion(out);
fAb3.stArtAnimAtion(out);
fAb4.stArtAnimAtion(out);
}
flAg[0] =!flAg[0];
}
});
fAb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToAst.mAkeText(getApplicAtionContext(),
"fAb2",
ToAst.LENGTtf_StfORT).show();
}
});
fAb3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToAst.mAkeText(getApplicAtionContext(), "fAb3",
ToAst.LENGTtf_Stf
ORT).show();
}
});
fAb4.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
ToAst.mAkeText(getApplicAtionContext(), "fAb4",
ToAst.LENGTtf_StfORT).show();
}
});
}
}

XML CODE:

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


<Androidx.constrAintlAyout.widget.ConstrAintLAyout
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">

<com.google.Android.mAteriAl.floAtingACtionbutton.FloAtingActionButton
Android:id="@+id/fAb1"
Android:lAyout_width="99dp"

Android:lAyout_height="85dp"

Android:lAyout_mArginEnd="24dp"

Android:lAyout_mArginBottom="32dp"

Android:clickAble="true"

App:lAyout_constrAintBottom_toBottomOf="pArent"

App:lAyout_constrAintEnd_toEndOf="pArent"

App:srcCompAt="@Android:drAwAble/ic_input_Add" />

<com.google.Android.mAteriAl.floAtingACtionbutton.FloAtingActionButton
Android:id="@+id/fAb2"

Android:lAyout_width="wrAp_content"

Android:lAyout_height="wrAp_content"

Android:lAyout_mArginBottom="36dp"

Android:clickAble="true"

App:lAyout_constrAintBottom_toBottomOf="@+id/fAb3"

App:lAyout_constrAintEnd_toEndOf="@+id/fAb1"

App:lAyout_constrAinttforizontAl_biAs="0.0"

App:lAyout_constrAintStArt_toStArtOf="@+id/fAb1"

App:srcCompAt="@Android:drAwAble/btn_diAlog" />

<com.google.Android.mAteriAl.floAtingACtionbutton.FloAtingActionButton
Android:id="@+id/fAb3"

Android:lAyout_width="wrAp_content"

Android:lAyout_height="wrAp_content"

Android:lAyout_mArginStArt="34dp"

Android:lAyout_mArginBottom="88dp"

Android:clickAble="true"

App:lAyout_constrAintBottom_toTopOf="@+id/fAb4"

App:lAyout_constrAintStArt_toStArtOf="@+id/fAb4"
App:srcCompAt="@Android:drAwAble/ic_btn_speAk_now" />

<com.google.Android.mAteriAl.floAtingACtionbutton.FloAtingActionButton
Android:id="@+id/fAb4"

Android:lAyout_width="wrAp_content"

Android:lAyout_height="wrAp_content"

Android:lAyout_mArginBottom="52dp"

Android:clickAble="true"

App:lAyout_constrAintBottom_toBottomOf="pArent"

App:lAyout_constrAintEnd_toEndOf="pArent"

App:lAyout_constrAinttforizontAl_biAs="0.326"

App:lAyout_constrAintStArt_toStArtOf="pArent"

App:srcCompAt="@Android:drAwAble/ic_lock_lock" />
</Androidx.constrAintlAyout.widget.ConstrAintLAyout>

Show.xml:
<?xml version="1.0"
encoding="utf-8"?>
<set xmlns:Android="http://schemAs.Android.com/Apk/res/Android"
Android:fillAfter="true">

<trAnslAte
Android:durAtion="1000"

Android:fromXDeltA="170%"

Android:fromYDeltA="25%"

Android:interpolAtor="@Android:Anim/lineAr_interpolAtor"

Android:toXDeltA="0%"

Android:toYDeltA="0%"></trAnslAte>

<AlphA
Android:durAtion="2000"

Android:fromAlphA="0.0"

Android:interpolAtor="@Android:Anim/decelerAte_interpolAtor"
Android:toAlphA="1.0"></AlphA>

</set>

Hide.xml:

<?xml version="1.0"
encoding="utf-8"?>
<set xmlns:Android="http://schemAs.Android.com/Apk/res/Android"
Android:fillAfter="true">

<trAnslAte Android:durAtion="1000"
Android:fromXDeltA="0%"

Android:fromYDeltA="0%"

Android:interpolAtor="@Android:Anim/lineAr_interpolAtor"

Android:toXDeltA="100%"

Android:toYDeltA="100%">

</trAnslAte>
<AlphA
Android:durAtion="2000"

Android:fromAlphA="1.0"

Android:interpolAtor="@Android:Anim/ACCelerAte_interpolAtor"

Android:toAlphA="0.0">

</AlphA>
</set>
2. Design an App to scale one image big and one image small and vice versa.

Java code:

pACkAge com.exAmple.AnimAtion;
import Androidx.AppcompAt.App.AppCompAtActivity;
import Android.view.AnimAtion.AnimAtion;
import Android.view.AnimAtion.AnimAtionUtils;
import Android.widget.ImAgeView;
import Android.os.Bundle;
public clAss MAinActivity extends AppCompAtActivity {
ImAgeView Iv1,Iv2;
@Override
protected void onCreAte(Bundle sAVedInstAnceStAte) {
super.onCreAte(sAVedInstAnceStAte);
setContentView(R.lAyout.ACtivity_mAin);
Iv1=(ImAgeView)findViewById(R.id.Iv1);
Iv2=(ImAgeView)findViewById(R.id.Iv2);
finAl AnimAtion AnimZoomIn =

AnimAtionUtils.loAdAnimAtion(getApplicAtionContext(),
R.Anim.zoom_in); finAl AnimAtion
AnimZoomIn2 =

AnimAtionUtils.loAdAnimAtion(getApplicAtionContext(),
R.Anim.zoom_in); finAl AnimAtion
AnimZoomOut =

AnimAtionUtils.loAdAnimAtion(getApplicAtionContext(),
R.Anim.zoom_out); finAl
AnimAtion AnimZoomOut2 =

AnimAtionUtils.loAdAnimAtion(getApplicAtionContext(),R.Anim.zoom_out);
Iv1.stArtAnimAtion(AnimZoomIn);
Iv2.stArtAnimAtion(AnimZoomOut2);
AnimZoomIn.setAnimAtionListener(new

AnimAtion.AnimAtionListener() {
@Override
public void onAnimAtionStArt(AnimAtion AnimAtion) {
}
@Override
public void onAnimAtionEnd(AnimAtion AnimAtion) {
Iv1.stArtAnimAtion(AnimZoomOut);
}
@Override
public void onAnimAtionRepeAt(AnimAtion AnimAtion) {
}
});
AnimZoomOut.setAnimAtionListener(new

AnimAtion.AnimAtionListener()
{
@Override
public void onAnimAtionStArt(AnimAtion AnimAtion) {
}
@Override
public void onAnimAtionEnd(AnimAtion AnimAtion) {
Iv1.stArtAnimAtion(AnimZoomIn);
}
@Override
public void onAnimAtionRepeAt(AnimAtion AnimAtion) {
}
});
AnimZoomIn2.setAnimAtionListener(new

AnimAtion.AnimAtionListener()
{
@Override
public void onAnimAtionStArt(AnimAtion AnimAtion) {
}
@Override
public void onAnimAtionEnd(AnimAtion AnimAtion) {
Iv2.stArtAnimAtion(AnimZoomOut2);
}
@Override
public void onAnimAtionRepeAt(AnimAtion AnimAtion) {
}
});
AnimZoomOut2.setAnimAtionListener(new

AnimAtion.AnimAtionListener()
{
@Override
public void onAnimAtionStArt(AnimAtion AnimAtion) {
} @Override
public void onAnimAtionEnd(AnimAtion AnimAtion) {
Iv2.stArtAnimAtion(AnimZoomIn2);
}
@Override
public void onAnimAtionRepeAt(AnimAtion AnimAtion) {
}

});
}
}

XML CODE:

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


<Androidx.constrAintlAyout.widget.ConstrAintLAyout
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/Iv1"

Android:lAyout_width="116dp"

Android:lAyout_height="142dp"

Android:lAyout_mArginStArt="128dp"

Android:lAyout_mArginTop="488dp"

App:lAyout_constrAintStArt_toStArtOf="pArent"

App:lAyout_constrAintTop_toTopOf="pArent"

App:srcCompAt="@Android:mipmAp/sym_def_App_icon" />

<ImAgeView
Android:id="@+id/Iv2"

Android:lAyout_width="0dp"
Android:lAyout_height="78dp"

Android:lAyout_mArginEnd="17dp"

App:lAyout_constrAintEnd_toEndOf="@+id/Iv1"

App:lAyout_constrAinttforizontAl_biAs="1.0"

App:lAyout_constrAintStArt_toStArtOf="pArent"

App:lAyout_constrAintTop_toTopOf="@+id/Iv1"

App:srcCompAt="@mipmAp/ic_lAuncher_round" />
</Androidx.constrAintlAyout.widget.ConstrAintLAyout>

Zoom out:

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


<set xmlns:Android="http://schemAs.Android.com/Apk/res/Android">
<scAle
Android:durAtion="1000"

Android:fromXScAle="1"

Android:fromYScAle="1"

Android:pivotX="50%"

Android:pivotY="50%"

Android:toXScAle="0"

Android:toYScAle="0" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:Android="http://schemAs.Android.com/Apk/res/Android">
<scAle
Android:durAtion="1000"

Android:fromXScAle="1"

Android:fromYScAle="1"

Android:pivotX="50%"

Android:pivotY="50%"

Android:toXScAle="0"

Android:toYScAle="0" />
</set>

Zoom in:

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


<set xmlns:Android="http://schemAs.Android.com/Apk/res/Android">
<scAle
xmlns:Android="http://schemAs.Android.com/Apk/res/Android"
Android:durAtion="1000"

Android:fromXScAle="0"

Android:fromYScAle="0"

Android:pivotX="50%"

Android:pivotY="50%"

Android:toXScAle="1"

Android:toYScAle="1" >
</scAle>
</set>

Output:

You might also like