Thursday, March 10, 2011

USING CANVAS TO ROTATE BALL

package in.ram.JungleMayhem;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;


public class JungleMayhem extends Activity {
//private Bitmap bg;
private DrawOval dw;
int x = 30;
int y = 230;
//private boolean start = true;
private boolean running = true;
private boolean direction = true;
private boolean direction1;
private boolean direction2;
private boolean direction3;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
//Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bg1480x320);
//Bitmap bg= Bitmap.createScaledBitmap(mBitmap, 240, 320, true);
//int pic_width = mBitmap.getWidth();
//int pic_height = mBitmap.getHeight();
//ImageView img = new ImageView(this);
//img.setImageBitmap(bg);
//setContentView(img);
dw = new DrawOval(this);
setContentView(dw,new ViewGroup.LayoutParams(240,400));
(new Thread(new Animationloop())).start();
}

public void updatePhysics() {
if(direction == true) {
if(x<= 130) {
x+=1;
y-=1;
}else if(x==131){
direction = false;
direction1=true;
}
}

if(direction1==true){
if(x<=230){
x+=1;
y+=1;
}else if(x==231){
direction1=false;
direction2=true;
}
}

if(direction2==true){
if(x>=131){
x-=1;
y+=1;
}else if(x==130){
direction2=false;
direction3=true;
}
}

if(direction3==true){
if(x>=30){
x-=1;
y-=1;
}else if(x==29){
direction3=false;
direction=true;
x = 30;
y = 230;
}
}
}

class DrawOval extends View {
//Canvas c;
//private ShapeDrawable shp;
private Bitmap bg = BitmapFactory.decodeResource(getResources(),R.drawable.bg2480x320);
private Bitmap bg1= Bitmap.createScaledBitmap(bg, 240, 320, true);
private Bitmap p1 = BitmapFactory.decodeResource(getResources(),R.drawable.egg);
private Bitmap egg = Bitmap.createScaledBitmap(p1, 10, 10, true);

private Bitmap pl1 =BitmapFactory.decodeResource(getResources(),R.drawable.egg1);
private Bitmap pl2 =BitmapFactory.decodeResource(getResources(),R.drawable.egg2);
private Bitmap pl11 = Bitmap.createScaledBitmap(pl1, 45,45, true);
private Bitmap pl22 = Bitmap.createScaledBitmap(pl2, 45,45, true);


public DrawOval(Context context) {
super(context);
}
//shp = new ShapeDrawable(new OvalShape());
//shp.getPaint().setColor(0xff74AC23);
//shp.setBounds(0, 0, 480, 360);


protected synchronized void onDraw(Canvas canvas) {

canvas.drawBitmap(bg1, new Rect(0,0,240,400), new Rect(0,0,240,400), new Paint());
canvas.drawBitmap(pl11,25,150,new Paint());
canvas.drawBitmap(pl22,165,150,new Paint());
canvas.drawBitmap(egg,200,200,new Paint());
canvas.drawBitmap(egg,x,y,new Paint());
//canvas.drawBitmap(p1, new Rect(0,0,100,100), new Rect(0,0,100,100), new Paint());
//canvas.drawColor(Color.BLUE);
//shp.draw(canvas);
invalidate();
}
}

class Animationloop implements Runnable {

@Override
public void run() {
while(true) {
while(running) {
try {
Thread.sleep(10);
}catch(InterruptedException e) {
System.out.println("-------InterruptedException-----"+e);
}
//dw.postInvalidate();
updatePhysics();
}
}

}

}
}

No comments:

Post a Comment