This post include:
- Complete code for a simple graphics application in Android.
- Concept of process in Android.
- Use of Thread in Android programming.
Welcome friend,
Here I'm posting the basic code for a simple Graphics application in Android ~ a "WELCOME-SCREEN"..!!
For any application, welcome screen is a must .(remember any game starts with an attractive welcome note,isnt it?).So chose to start graphics coding with craeting a welcome screen.
summary of application::
- When we open this application, a welcome screen will splash out and stay for a while.
- We can also close this screen by pressing DOWN key.
- Followed by this welcome screen we can place starting activity of our apllication.
Well,first create a new Android project in your Eclipse platform.
Let us start with MySpalsh.java
- public class MySplash extends Activity {
- private Thread myThread;
- protected int SplashTime = 8000;
At first, Within the class we defined a thread <2> to display the welcome screen for a default time predefined by <3>
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.splash);
- final MySplash SplashProcess = this;
Within the onCreate function, (which will be active on starting) we set the screen as splash.xml<3>. we should link the welcome image to this xml file. we also created one process-instance <4> .
- myThread = new Thread() {
- @Override
- public void run() {
- try {
- synchronized(this){
- wait(SplashTime);
- }
- } catch(InterruptedException e) {}
Followed by this we define a thread to display the welcome image.<1> @override provision should also given<2>. we know that, in a Thread the code-fragment within run() will be executed<3>.Here<5> the keyword synchronised is used to get synchronisation among threads(i.e., at a time only one thread will be active).Each Thread will "wait" for the default wait-time<6>
- finally {
- finish();
- Intent i = new Intent();
- i.setClass(SplashProcess, page2.class);
- startActivity(i);
- stop();
- }
- }
- };
myThread.start();
After Thraed definition we should call start() function to start Thread excecution.
- public boolean onTouchEvent(MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- synchronized(myThread){
- myThread.notifyAll();
- }
- }
- return true;}
The complete code is as follows:
MySplash.java
package com.VipinNarayan.splash;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.MotionEvent;
public class MySplash extends Activity {
protected int SplashTime = 8000;
private Thread myThread;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final MySplash SplashProcess = this;
myThread = new Thread() {
@Override
public void run() {
try {
synchronized(this){
wait(SplashTime);
}
} catch(InterruptedException e) {}
finally {
finish();
Intent i = new Intent();
i.setClass(SplashProcess, page2.class);
startActivity(i);
stop();
}
}
};
myThread.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
synchronized(myThread){
myThread.notifyAll();
}
}
return true;
}
}
page2.java
package com.VipinNarayan.splash;
import android.app.Activity;
import android.os.Bundle;
public class page2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:src="@drawable/android" android:layout_height="match_parent" android:layout_width="wrap_content" android:id="@+id/ImageView01"></ImageView>
</LinearLayout>