package com.learning.gilo; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.os.Bundle; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnTouchListener; public class GFXSurface extends Activity implements OnTouchListener{ MineSurface ourSurfaceView; float x, y; float sX, sY, fX, fY, dX, dY, scaledX, scaledY, aniX, aniY; protected void onCreate(Bundle b){ super.onCreate(b); ourSurfaceView = new MineSurface(this); ourSurfaceView.setOnTouchListener(this); setContentView(ourSurfaceView); } protected void onPause(){ super.onPause(); ourSurfaceView.pause(); } protected void onResume(){ super.onResume(); ourSurfaceView.resume(); } public boolean onTouch(View view, MotionEvent event) { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } x = event.getX(); y = event.getY(); switch(event.getAction()){ case MotionEvent.ACTION_DOWN: sX = sY = fX = fY = dX = dY = scaledX = scaledY = aniX = aniY = 0; sX = event.getX(); sY = event.getY(); break; case MotionEvent.ACTION_UP: fX = event.getX(); fY = event.getY(); dX =fX - sX; dY =fY - sY; scaledX = dX/30; scaledY = dY/30; x = y = 0; break; } return true; } public class MineSurface extends SurfaceView implements Runnable{ SurfaceHolder ourHolder; Thread ourThread; boolean isRunning = false; Bitmap gball; public MineSurface(Context context){ super(context); ourHolder = getHolder(); gball = BitmapFactory.decodeResource(getResources(), R.drawable.greenball); x = 0; y = 0; sX = sY = fX = fY = dX = dY = scaledX = scaledY = aniX = aniY = 0; } public void run(){ while(isRunning){ if(!ourHolder.getSurface().isValid()) continue; Canvas canvas = ourHolder.lockCanvas(); canvas.drawColor(Color.WHITE); if(x != 0 && y != 0){ canvas.drawBitmap(gball, x - gball.getWidth()/2, y - gball.getHeight()/2, null); } if(fX != 0 && fY != 0){ canvas.drawBitmap(gball, fX - gball.getWidth()/2- aniX, fY - gball.getHeight()/2 - aniY, null); } aniX += scaledX; aniY += scaledY; ourHolder.unlockCanvasAndPost(canvas); } } public void pause(){ isRunning = false; while(true){ try { ourThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } break; } ourThread = null; } public void resume(){ isRunning = true; ourThread = new Thread(this); ourThread.start(); } } }
Category Archives: Source code
Android animation
Source code: The Splash screen
This is the full code for the splash screen. I will keep on adding stuff to it as I explain and also learn. So it isnt the complete thing.
package com.learning.gilo; import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import android.os.Bundle; public class Splash extends Activity{ MediaPlayer song; protected void onCreate(Bundle b){ super.onCreate(b); setContentView(R.layout.splash); song = MediaPlayer.create(Splash.this, R.raw.door); //the song is door.mp3 song.start(); Thread mythread = new Thread(){ public void run(){ try{ sleep(2000); }catch(InterruptedException e){ e.printStackTrace(); }finally{ Intent myIntent = new Intent("com.learning.gilo.MENULIST"); startActivity(myIntent); } } }; mythread.start(); } protected void onPause(){ super.onPause(); song.release(); finish(); } }