Sunday, November 27

ANDROID GRAPHICS APPLICATION ~ BASIC CODES


 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.
Get..!! Set..!! GO..!!

Well,first create a new Android project in your Eclipse platform.
Let us start with MySpalsh.java

  1. public class MySplash extends Activity {
  2. private Thread myThread;
  3. 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>

  1. public void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.splash);
  4. 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> .

  1. myThread = new Thread() {
  2.          @Override
  3.           public void run() {
  4.               try {            
  5.                synchronized(this){
  6.                 wait(SplashTime);
  7.                }
  8.                 } 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>

  1.  finally {
  2.   finish();
  3.   Intent i = new Intent();
  4.                  i.setClass(SplashProcess, page2.class);
  5.            startActivity(i);
  6.               stop();
  7.              }
  8.          }
  9.         };
After this we define finally, specifying , after the working of Threads(i.e, when welcome image vanishes) what should do. finish() indicate<2> scope of Threads ends.Next activity/page (here page2.class)is started<5>. stop() is used to kill all the Threads created <6>

 myThread.start();

After Thraed definition we should call start() function to start Thread excecution.

  1. public boolean onTouchEvent(MotionEvent event) {
  2.     if (event.getAction() == MotionEvent.ACTION_DOWN) {
  3.      synchronized(myThread){
  4.       myThread.notifyAll();
  5.      }
  6.    }
  7.     return true;}
If we have to close welcome screen (without waiting till it ends) add this code. It detects whteher DOWN key is pressed<2>, if so, do notifyAll() function <4>.

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>

3 comments :

Hari said... Best Blogger Tips [Reply to comment] Best Blogger Templates

thanks bhai..... very informative :)

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Hari

Nice to hear that..!!
Thank U..
visit again :)

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Does anyone have a reference or know how to make a phone call when users click/touch a button (say "Call")?
Im a newbie to android

Post a Comment