티스토리 뷰
안드로이드 점프 카운트하기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_time"
android:textSize="22sp"
android:text="time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv_jumpcount"
android:textSize="22sp"
android:text="jump_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn_start"
android:textSize="22sp"
android:text="START"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
package com.jw.jump;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
SensorManager sm;
Sensor sensor_accelerometer;
int jump_up = 0;
int jump_dwon = 0;
float x,y,z;
long startTime;
long currentTime;
static int jumpCount =0;
int elapsedTime;
final int jumpThreshold = 4; // 점프 임계값
double gravity = 9.81;
double acceleration = 0;
TextView tv_jumpcount, tv_time;
Button btn_start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 화면을 가로로 설정하기
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_main);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
sensor_accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
startTime = System.currentTimeMillis();
tv_jumpcount = findViewById(R.id.tv_jumpcount);
tv_time = findViewById(R.id.tv_time);
btn_start = findViewById(R.id.btn_start);
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentTime = System.currentTimeMillis();
elapsedTime = (int) currentTime - (int)startTime;
tv_time.setText("남은시간 " + (60 - elapsedTime / 1000) + " 초");
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
finish();
return false;
}
return false;
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener(this,sensor_accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
sm.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
x = event.values[0];
y = event.values[1];
z = event.values[2];
acceleration = Math.sqrt(x * x + z * z);
if(acceleration > gravity + jumpThreshold){
jump_up = 1;
}
if(jump_up == 1 && acceleration < gravity - jumpThreshold) {
jump_dwon = 1;
}
if(jump_dwon == 1){
jump_up = 0;
jump_dwon = 0;
jumpCount++;
tv_jumpcount.setText("점프 카운트: " + jumpCount);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
'프로그래밍 > 안드로이드 앱프로그래밍' 카테고리의 다른 글
안드로이드 카운트다운 앱 만들기 (0) | 2021.11.23 |
---|---|
안드로이드 만보기 소스코드 (0) | 2021.10.06 |
안드로이드 조도센서 화면 밝게, 어둡게 앱 만들기 (0) | 2021.10.01 |
내 휴대폰에서 센서 알아보기 (0) | 2021.09.30 |
국가코드 가져와서 TTS에 알맞게 언어 바꾸기 (0) | 2021.06.28 |
댓글