POLITEKNIK TUANKU SYED SIRAJUDDIN DFP50293 Mobile Application Development
DEMO 9: Animation
LEARNING OUTCOMES:
3.2 Apply animation
1. Add a new layout to existing project. Name as Splash
2. i. Don’t forget to tick at Launcer Activity
ii. Change activity_splash layout to
LinearLayout
iii. Add id to LinearLayout:
android:id="@+id/lyt1"
iv. Put image on layout activity_splash.
Add id for ImageView
android:id="@+id/imageView"
Run apps
3. Edit AndroidManifest
<activity
android:name=".Splash2"
android:exported="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
4. Right click on res folder.
New > Android Resources Directory. Name as anim
**Pn Farihan Elyana Binti Zahari**
POLITEKNIK TUANKU SYED SIRAJUDDIN DFP50293 Mobile Application Development
5. Right click anim folder. Create new >> Animation resource files. Name as alpha
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="3000" />
6.
Right click anim folder. Create new >> Animation resource files. Name as translate
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0%"
android:toXDelta="0%"
android:fromYDelta="200%"
android:toYDelta="0%"
android:duration="2000"
android:zAdjustment="top" />
</set>
7. Edit file Splash.java
**Pn Farihan Elyana Binti Zahari**
POLITEKNIK TUANKU SYED SIRAJUDDIN DFP50293 Mobile Application Development
public class Splash extends AppCompatActivity {
Thread splashTread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
StartAnimation();
}
private void StartAnimation() {
Animation anim = AnimationUtils.loadAnimation(this,
R.anim.alpha);
anim.reset();
LinearLayout l=(LinearLayout) findViewById(R.id.lyt1);
l.clearAnimation();
l.startAnimation(anim);
anim = AnimationUtils.loadAnimation(this, R.anim.translate);
anim.reset();
ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.clearAnimation();
iv.startAnimation(anim);
splashTread = new Thread(){
@Override
public void run() {
try {
int waited = 0;
// Splash screen pause time
while (waited < 3500) {
sleep(100);
waited += 100;
}
Intent intent = new Intent(Splash.this,
MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
Splash.this.finish();
} catch (InterruptedException e){
}
finally {
Splash.this.finish();
}
}
};
splashTread.start();
**Pn Farihan Elyana Binti Zahari**
POLITEKNIK TUANKU SYED SIRAJUDDIN DFP50293 Mobile Application Development
}
}
8.
Run apps
**Pn Farihan Elyana Binti Zahari**