Android basics About Android Linux based operating system Open source Designed for handheld devices Developed by Android Inc. Google (2005) Open Handset Alliance (2007) Very successful system (75% sales in 2012) Top mobile operating systems Android basics Can run Java code on Dalvik VM Multi-user system (each application is a different user) Unique user ID Has its own virtual machine Has its own Linux process Principle of least privilege Applications can share data Application components Building blocks of an application 4 types Activity Single screen with UI Service Backgroud process without UI Content provider Manages a shared set of application data Broadcast receiver Responds to system-wide broadcast announcements Applications All components of an application runs in the same process The system tries to keep a process as long as possible Applications are stored in a stack The systems duty to destroy the applications The system uses importance hierarchy Foreground processes Visible process Service process Background process Empty process Activity lifecycle Android development Andriod SDK (http://developer.android.com/sdk) Java API libraries for different platforms SDK manager AVD Manager (emulator) Developer IDE Eclipse with ADT (Juno has it by default) Netbeans with NBAndroid plugin Android device USB Driver Enable USB debugging Useful link: http://developer.android.com IDE basics NetBeans https://kenai.com/projects/nbandroid/pages/Install https://kenai.com/projects/nbandroid/pages/Intro Eclipse For ADT bundle everything is set up http://developer.android.com/sdk/installing/bundle.html For Juno install is not required, for other existing Eclipse installs: http://developer.android.com/sdk/installing/addingpackages.html Add platforms and packages http://developer.android.com/sdk/installing/addingpackages.html Set up virtual devices Presets Set up virtual devices API levels API levels New Project (NetBeans) New Project New Project AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="gamedev.android.UEngine.PlatformGame" android:versionCode="1" Our application version android:versionName="1.0"> <application android:label="@string/app_name" > Activity to start <activity android:name="MainActivity" android:label="@string/app_name"> Icon label of our <intent-filter> Activity <action android:name="android.intent.action.MAIN"We /> can change this to e.g.: /> <category android:name="android.intent.category.LAUNCHER" “MyPlatformGame” </intent-filter> </activity> </application> </manifest> Start a virtual device Build and run the project MainActivity.java: setContentView(R.layout.main); Main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, MainActivity" /> </LinearLayout> OpenGL ES – MainActivity.java import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; public class MainActivity extends Activity { private GLSurfaceView mGLView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGLView = new MainSurfaceView(this); setContentView(mGLView); } } OpenGL ES – MainSurfaceView.java package gamedev.android.UEngine.PlatformGame; import android.content.Context; import android.opengl.GLSurfaceView; public class MainSurfaceView extends GLSurfaceView { private MainRenderer mRenderer; public MainSurfaceView(Context context) { super(context); this.mRenderer = new MainRenderer(getContext()); setRenderer(this.mRenderer); } } OpenGL ES – MainRenderer.java package gamedev.android.UEngine.PlatformGame; import android.content.Context; import android.opengl.GLSurfaceView; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; public class MainRenderer implements GLSurfaceView.Renderer{ private Context context; public MainRenderer(Context context) { this.context = context; } public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glClearColor(0.5F, 0.5F, 1.0F, 1.0F); } public void onDrawFrame(GL10 gl) { gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT ); } public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); } } Build and run Draw something - MainRenderer New imports import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; New data member protected FloatBuffer VB = null; public float rotAngle = 0; Draw something - MainRenderer public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glClearColor(0.5F, 0.5F, 1.0F, 1.0F); float[] VCoords = { -1.0F, -1.0F, 0.0F, 1.0F, -1.0F, 0.0F, 1.0F, 1.0F, 0.0F, -1.0F, -1.0F, 0.0F, 1.0F, 1.0F, 0.0F, -1.0F, 1.0F, 0.0F }; ByteBuffer vbb = ByteBuffer.allocateDirect(VCoords.length * 4); vbb.order(ByteOrder.nativeOrder()); this.VB = vbb.asFloatBuffer(); this.VB.put(VCoords); this.VB.position(0); } Draw something - MainRenderer public void onDrawFrame(GL10 gl) { gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT ); gl.glMatrixMode(gl.GL_MODELVIEW); gl.glLoadIdentity(); gl.glTranslatef(0,0,-6); gl.glColor4f(1, 0, 0, 1); gl.glRotatef(rotAngle, 0, 1, 0); gl.glEnableClientState(gl.GL_VERTEX_ARRAY); gl.glVertexPointer(3, gl.GL_FLOAT, 0, this.VB); gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6); gl.glDisableClientState(gl.GL_VERTEX_ARRAY); rotAngle += 2.5f; } Draw something - MainRenderer public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); float ratio = (float) width / height; gl.glMatrixMode(gl.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustumf(-ratio, ratio, -1, 1, 1.0f, 10); } Build and run Input handling - MainSurfaceView protected float lastX = -1; public boolean onTouchEvent(MotionEvent e) { float x = e.getX(); switch (e.getAction()) { case MotionEvent.ACTION_MOVE: float dx = lastX == -1 ? 0 : x - lastX; mRenderer.rotAngle += dx * 1.0f; break; } lastX = x; return true; } Also remove rotAngle += 2.5f; from MainRenderer.onDrawFrame Build and run Quad can be rotated with the mouse in emulator Orientation – Full screen Fix orientation to landscape (typical in games) Activity::onCreate(): setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); Set to full screen (typical in games) Activity::onCreate(): requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); Rotate emulator to landscape mode: Ctrl+F12 Compile and run