Feb 27, 2012

Detect Android device rotation, using Accelerometer sensor

Detect Android device rotation, using Accelerometer sensor
package com.AndroidRotation;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidRotationActivity extends Activity implements SensorEventListener{

private SensorManager manager;
private Sensor sensor;

TextView azimuth, pitch, roll;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

manager = (SensorManager)getSystemService(SENSOR_SERVICE);
sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

azimuth = (TextView)findViewById(R.id.azimuth);
pitch = (TextView)findViewById(R.id.pitch);
roll = (TextView)findViewById(R.id.roll);
}

@Override
protected void onPause() {
super.onPause();
manager.unregisterListener(this);
}

@Override
protected void onResume() {
super.onResume();
manager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent arg0) {

azimuth.setText("Azimuth: " + String.valueOf(arg0.values[0]));
pitch.setText("Pitch: " + String.valueOf(arg0.values[1]));
roll.setText("Roll: " + String.valueOf(arg0.values[2]));

}
}



Next:
- Get detail info of Accelerometer

Related:
- Detect Orientation using Accelerometer and Magnetic Field sensors

Feb 15, 2012

Implement onTouchEvent() to handle user touch on SurfaceView

Modify from last article Sprite auto-run, implement onTouchEvent() to handle user touch on SurfaceView. When user touch on the screen(SurfaceView), it will call setX(x) and setY(y) of mySprite object to update its position.

package com.MyGame;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class MyForeground extends MyGameSurfaceView {

Sprite mySprite;

public MyForeground(Context context) {
super(context);
init();
}

public MyForeground(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public MyForeground(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init(){
mySprite = new Sprite(
BitmapFactory.decodeResource(getResources(), R.drawable.icon_me),
100, 100);
}

@Override
protected void onDraw(Canvas canvas) {
//Clear Canvas with transparent background
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);

mySprite.draw(canvas);
}

@Override
public void updateStates() {
// TODO Auto-generated method stub
mySprite.update();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();

int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
mySprite.setX(x);
mySprite.setY(y);
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_OUTSIDE:
break;
default:
}

return true;

}

}



Next:
- React device movement/orientation using accelerometer

Feb 12, 2012

Sprite auto-run

In last article Introduce Sprite without movement. In this article, we are going to make it self move.
Sprite auto-run
Modify Sprite.java to make it self random move inside the canvas.
package com.MyGame;
import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Sprite {
private Bitmap bitmap;
private int x;
private int y;
float bitmap_halfWidth, bitmap_halfHeight;

Random random = new Random();
private int dirX;
private int dirY;
private int moveX;
private int moveY;

final int MAX = 99;

public Sprite(Bitmap bm, int tx, int ty){
bitmap = bm;
x = tx;
y = ty;
bitmap_halfWidth = bitmap.getWidth()/2;
bitmap_halfHeight = bitmap.getHeight()/2;

if(random.nextBoolean()){
dirX = 1;
}else{
dirX = -1;
}

if(random.nextBoolean()){
dirY = 1;
}else{
dirY = -1;
}

moveX = random.nextInt(MAX) + 1;
moveY = random.nextInt(MAX) + 1;

}

public void setX(int tx){
x = tx;
}

public void setY(int ty){
y = ty;
}

public int getX(){
return x;
}

public int getY(){
return y;
}

public void draw(Canvas canvas){

if(x < 0){
x = 0;
dirX *= -1;
moveX = random.nextInt(MAX) + 1;
}else if(x >= canvas.getWidth()){
x = canvas.getWidth();
dirX *= -1;
moveX = random.nextInt(MAX) + 1;
}

if(y < 0){
y = 0;
dirY *= -1;
moveY = random.nextInt(MAX) + 1;
}else if(y >= canvas.getHeight()){
y = canvas.getHeight();
dirY *= -1;
moveY = random.nextInt(MAX) + 1;
}


canvas.drawBitmap(bitmap, x-bitmap_halfWidth, y-bitmap_halfHeight, null);
}

public void update(){
x += (dirX * moveX);
y += (dirY * moveY);
}

}


Modify MyForeground.java to override updateStates(), call mySprite.update() indirectly. Also note the method onDraw(); because SurfaceView will not remove the old Sprite, so we have to clear the canvas before draw the new Sprite. TO clear canvas with TRANSPARENT background, we can call the method canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR).
package com.MyGame;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.util.AttributeSet;

public class MyForeground extends MyGameSurfaceView {

Sprite mySprite;

public MyForeground(Context context) {
super(context);
init();
}

public MyForeground(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public MyForeground(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init(){
mySprite = new Sprite(
BitmapFactory.decodeResource(getResources(), R.drawable.icon_me),
100, 100);
}

@Override
protected void onDraw(Canvas canvas) {
//Clear Canvas with transparent background
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);

mySprite.draw(canvas);
}

@Override
public void updateStates() {
// TODO Auto-generated method stub
mySprite.update();
}

}


Next:
- Implement onTouchEvent() to handle user touch on SurfaceView

Feb 9, 2012

Introduce Sprite

Sprite
Base on last post Create transparent foreground SurfaceView, the object on foreground was draw as a bitmap. We are going to implement a Sprite class here. Sprite object keep it's own status such as location(x, y) and bitmap, and also draw itself on canvas.

Add class Sprite.java
package com.MyGame;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Sprite {
private Bitmap bitmap;
private int x;
private int y;
float bitmap_halfWidth, bitmap_halfHeight;

public Sprite(Bitmap bm, int tx, int ty){
bitmap = bm;
x = tx;
y = ty;
bitmap_halfWidth = bitmap.getWidth()/2;
bitmap_halfHeight = bitmap.getHeight()/2;
}

public void setX(int tx){
x = tx;
}

public void setY(int ty){
y = ty;
}

public int getX(){
return x;
}

public int getY(){
return y;
}

public void draw(Canvas canvas){
canvas.drawBitmap(bitmap, x-bitmap_halfWidth, y-bitmap_halfHeight, null);
}

}


Modify MyForeground.java
package com.MyGame;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;

public class MyForeground extends MyGameSurfaceView {

Sprite mySprite;

public MyForeground(Context context) {
super(context);
init();
}

public MyForeground(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public MyForeground(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init(){
mySprite = new Sprite(
BitmapFactory.decodeResource(getResources(), R.drawable.icon_me),
100, 100);
}

@Override
protected void onDraw(Canvas canvas) {
mySprite.draw(canvas);
}

}


Next:
- Sprite auto-run

Infolinks In Text Ads