Geiger Tilt counter Android App Game
In this video it shows a simple code to create a Geiger Counter kind of game based on the tilt of the phone. It uses Accelerometer sensor to detect the tilt of the phone.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Code:
package com.programmerworld.geigercounter;
// How to create a fun game Android App like Geiger counter playing with Friends?
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private SensorManager sensorManager;
private Sensor accelerometer;
private float tiltAngle;
private MediaPlayer mediaPlayer;
private AnalogMeterView meterView;
private Handler soundHandler = new Handler();
private boolean isPlayingSound = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
meterView = new AnalogMeterView(this);
setContentView(meterView);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (sensorManager != null) {
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
mediaPlayer = MediaPlayer.create(this, R.raw.geiger_click); // Place geiger_click.mp3 in res/raw
if (accelerometer == null) {
Toast.makeText(this, "Accelerometer not available", Toast.LENGTH_LONG).show();
finish();
}
}
@Override
protected void onResume() {
super.onResume();
if (accelerometer != null) {
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
}
}
@Override
protected void onPause() {
super.onPause();
if (accelerometer != null) {
sensorManager.unregisterListener(this);
}
stopSound();
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float x = event.values[0];
float y = event.values[1];
tiltAngle = (float) Math.toDegrees(Math.atan2(x, y));
meterView.updateAngle(tiltAngle);
playSoundBasedOnTilt();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Not needed
}
private void playSoundBasedOnTilt() {
if (!isPlayingSound) {
isPlayingSound = true;
soundHandler.postDelayed(() -> {
if (mediaPlayer != null) {
mediaPlayer.start();
}
isPlayingSound = false;
}, Math.max(50, 300 - (int) Math.abs(tiltAngle) * 10));
}
}
private void stopSound() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
class AnalogMeterView extends androidx.appcompat.widget.AppCompatImageView {
private Paint paint;
private float angle = 0;
public AnalogMeterView(Context context) {
super(context);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(8);
paint.setColor(Color.YELLOW);
}
public void updateAngle(float angle) {
this.angle = angle;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
// Draw meter arc
paint.setColor(Color.YELLOW);
paint.setStyle(Paint.Style.STROKE);
canvas.drawArc(50, 50, width - 50, height - 50, 180, 180, false, paint);
// Draw needle
paint.setColor(Color.RED);
paint.setStrokeWidth(12);
float centerX = width / 2;
float centerY = height / 2 + (height - 100) / 4;
float needleLength = (width - 100) / 2;
double radians = Math.toRadians(angle);
float needleX = (float) (centerX + needleLength * Math.cos(Math.PI + radians));
float needleY = (float) (centerY + needleLength * Math.sin(Math.PI + radians));
canvas.drawLine(centerX, centerY, needleX, needleY, paint);
}
}
}
Screenshots:

