RSS

Android animation

08 Jul

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();
		}
	}

	

}

Advertisements
 
Leave a comment

Posted by on July 8, 2012 in Android, Code, Source code

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: