sumber asli sourcode dengan perintah : svn checkout http://android-ac.googlecode.com/svn/trunk/ android-ac-read-only

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="25dp" 
    android:gravity="center" 
    android:text="@string/app_name" 
    android:textSize="35sp"
/>
    
<TextView android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" 
android:id="@+id/textDirection" 
android:textSize="150sp" 
android:text="NW">
</TextView>

</LinearLayout>

buat 2 file java seperti berikut :

Compass.java

package com.wilis.compass;

import android.app.Activity;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

// implement SensorListener
public class Compass extends Activity implements SensorListener {
  SensorManager sensorManager;
  static final int sensor = SensorManager.SENSOR_ORIENTATION;
  Rose rose;

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

    // Set full screen view
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    rose = new Rose(this);

    setContentView(rose);

    // get sensor manager
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  }

  // register to listen to sensors
  @Override
  public void onResume() {
    super.onResume();
    sensorManager.registerListener(this, sensor);
  }

  // unregister
  @Override
  public void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
  }

  // Ignore for now
  public void onAccuracyChanged(int sensor, int accuracy) {
  }

  // Listen to sensor and provide output
  public void onSensorChanged(int sensor, float[] values) {
    if (sensor != Compass.sensor)
      return;
    int orientation = (int) values[0];
    rose.setDirection(orientation);
  }
}

Rose.java

package com.wilis.commpass;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.widget.ImageView;

public class Rose extends ImageView {
  Paint paint;
  int direction = 0;

  public Rose(Context context) {
    super(context);

    paint = new Paint();
    paint.setColor(Color.WHITE);
    paint.setStrokeWidth(2);
    paint.setStyle(Style.STROKE);

    this.setImageResource(R.drawable.compassrose);
  }

  @Override
  public void onDraw(Canvas canvas) {
    int height = this.getHeight();
    int width = this.getWidth();

    canvas.rotate(direction, width / 2, height / 2);
    super.onDraw(canvas);
  }

  public void setDirection(int direction) {
    this.direction = direction;
    this.invalidate();
  }

}

apabila dijalankan di device langsung agan menghasilkan seperti urutan gambar berikut :

buktikan sensornya berjalan dengan menggeser posisi anda atau device androidnya sehingga compassnya bergerak seperti contoh berikut :

Download source code projectnya Commpas.zip

mudah2 an berguna

uxc.wilis
🙂