wiki android par frandroid

Bille qui "roule" avec l'accéléromètre.

De FrAndroid - Android docs.

TUTO EXPRESS:

Bonjour le monde,

J'avais posé il y a quelques minutes des questions pour fabriquer un objet qui bouge avec l'accéléromètre. Comme j'ai trouvé ma réponse, je poste un "Tuto Express" sur comment réaliser cette application. En fait, j'ai mixé 2 tutos de anddev.org, tout le mérite revient à leurs posteurs respectifs.

Le programme pourrait être améliorer pour créer une "bille" qui roule dans votre téléphone.



Il faut donc créer 2 classes java: BounceView et Move dont voici le code:


Move.Java

import android.app.Activity;

import android.content.Context;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.Window;

public class Move extends Activity implements SensorEventListener {


/* Called when the activity is first created. */
// Just a RANDOM ID to recognize a Message later
protected static final int GUIUPDATEIDENTIFIER = 0x101;
SensorManager sensorManager;
Thread myRefreshThread = null;


/* Our 'ball' is located within this View */
BounceView myBounceView = null;


Handler myGUIUpdateHandler = new Handler() {


// @Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Move.GUIUPDATEIDENTIFIER:
/* Repaint the BounceView
* (where the ball is in) */
myBounceView.invalidate();
break;
}
super.handleMessage(msg);
}
};


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Set fullscreen
this.requestWindowFeature(Window.FEATURE_NO_TITLE);


this.myBounceView = new BounceView(this);
this.setContentView(this.myBounceView);
sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);


new Thread(new RefreshRunner()).start();
}


class RefreshRunner implements Runnable {
// @Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
// Send Message to the Handler which will call the invalidate() method of the BOucneView
Message message = new Message();
message.what = Move.GUIUPDATEIDENTIFIER;
Move.this.myGUIUpdateHandler.sendMessage(message);


try {
Thread.sleep(100); // a 10th of a second
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}


protected void onResume()
{
super.onResume();
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
}


protected void onStop()
{
sensorManager.unregisterListener(this);
super.onStop();
}


public void onAccuracyChanged(Sensor sensor, int accuracy) {
}


public void onSensorChanged(SensorEvent event) {
myBounceView.mPitch=event.values[SensorManager.DATA_Z];
myBounceView.mHeading=event.values[SensorManager.DATA_Y];


//updateOrientation(event.values[SensorManager.DATA_X], event.values[SensorManager.DATA_Y], event.values[SensorManager.DATA_Z]);


}


}


BounceView.java

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Point;

import android.graphics.drawable.Drawable;

import android.view.View;


public class BounceView extends View {


/* Our Ball together with the location it will be painted*/
protected Drawable mySprite;
protected Point mySpritePos = new Point(0,0);


/* Working with a Enum is 10000%
* safer than working with int's
* to 'remember' the direction. */
static float mRoll, mPitch, mHeading;
protected enum HorizontalDirection {LEFT, RIGHT}
protected enum VerticalDirection {UP, DOWN}
protected HorizontalDirection myXDirection = HorizontalDirection.RIGHT;
protected VerticalDirection myYDirection = VerticalDirection.UP;


public BounceView(Context context) {
super(context);
// Set the background
this.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.icon));
// Load our "Ball"
this.mySprite = this.getResources().getDrawable(R.drawable.icon);
}


@Override
protected void onDraw(Canvas canvas) {
/* Check if the Ball started to leave
* the screen on left or right side */


this.mySpritePos.x -= mPitch;


if (mySpritePos.x >= this.getWidth() - mySprite.getBounds().width()) {
mySpritePos.x = this.getWidth() - mySprite.getBounds().width();
//this.myXDirection = HorizontalDirection.LEFT;
} else if (mySpritePos.x <= 0) {
mySpritePos.x = 0;
//this.myXDirection = HorizontalDirection.RIGHT;
}


this.mySpritePos.y -= mHeading;


if (mySpritePos.y >= this.getHeight() - mySprite.getBounds().height()) {
mySpritePos.y = this.getHeight() - mySprite.getBounds().height();
} else if (mySpritePos.y <= 0) {
mySpritePos.y = 0;
}


/* Set the location, where the sprite
* will draw itself to the canvas */
this.mySprite.setBounds(this.mySpritePos.x, this.mySpritePos.y,
this.mySpritePos.x + 50, this.mySpritePos.y + 50);


/* Make the sprite draw itself to the canvas */
this.mySprite.draw(canvas);
}

}

Au plaisir!