Hello ko5tik,
Ok, then here is my code which is animates my ball.
package padd.testing;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
//import android.view.View.OnClickListener;
import android.graphics.*;
import android.content.*;
//import android.widget.Button;
public class Testing extends Activity
{
public static final int DIRECTION_RIGHT = 0, DIRECTION_LEFT = 1,
DIRECTION_UP = 2, DIRECTION_DOWN= 3;
private Panel main;
private Bitmap scratch;
private Canvas offscreen;
public boolean start = true;
private volatile boolean running = true;
private int direction = DIRECTION_RIGHT;
private int box = 10;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setOffscreenBitmap();
//Button danger = (Button)findViewById(R.id.Button01);
main = new Panel(this);
setContentView(main,new ViewGroup.LayoutParams(320,480));
/*danger.setOnClickListener( new OnClickListener(){
public void onClick(View v){
if(v.isPressed()==true){
(new Thread(new AnimationLoop())).start();
}
}
});*/
(new Thread(new AnimationLoop())).start();
}
private void setOffscreenBitmap()
{
scratch = Bitmap.createBitmap(30,30,Bitmap.Config.ARGB_8888);
offscreen = new Canvas();
offscreen.setBitmap(scratch);
offscreen.drawColor(Color.GREEN);
}
private synchronized void updatePhysics()
{
if(box < 10)
{
direction = DIRECTION_RIGHT;
}
else if(box > 250)
{
direction = DIRECTION_LEFT;
}
if(direction == DIRECTION_RIGHT)
{
box = box + 10;
}
else
{
box = box - 10;
}
}
private synchronized void doDraw(Canvas canvas, Paint paint)
{
if(start)
{
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(scratch,10,10,paint);
start = false;
}
else
{
canvas.save();
canvas.clipRect(box,8,box+32,40);
canvas.drawColor(Color.RED);
canvas.drawBitmap(scratch,box,10,paint);
canvas.restore();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
if(running)
{
running = false;
}
else
{
running = true;
}
}
else if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
direction = DIRECTION_LEFT;
}
else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
direction = DIRECTION_RIGHT;
}
else if (keyCode == KeyEvent.KEYCODE_DPAD_UP){
direction = DIRECTION_UP;
}
else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
{
finish();
}
return true;
}
class Panel extends View
{
Paint paint;
public Panel(Context context)
{
super(context);
paint = new Paint();
}
@Override
protected void onDraw(Canvas canvas)
{
doDraw(canvas,paint);
}
}
class AnimationLoop implements Runnable
{
public void run()
{
while(true)
{
while(running)
{
try
{
Thread.sleep(100);
}
catch(InterruptedException ex) {}
updatePhysics();
main.postInvalidate();
}
}
}
}
}
On Sun, Sep 5, 2010 at 5:37 PM, ko5tik <[email protected]> wrote:
>
>
> On Sep 3, 3:34 pm, Dhrumil Shah <[email protected]> wrote:
> > Hey Avigadl,
> >
> > If I change the values of the each direction like DIRECTION_TOP = 0,
> > DIRECTION_DOWN = 1, DIRECTION_RIGHT = 2, DIRECTION_LEFT = 3; it is
> working?
> >
> > I changed that but unfortunetly its not working.
> > Any other Solution?
>
> Then show us a code which is animates your ball
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-developers%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en