Demo
구성
- activity
- layout
MainActivity.java
package com.example.recyclerview;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private SeekBar seekBar;
private TextView textView;
private Button btnStart;
private CountDownTimer countDownTimer;
private Boolean counterIsActive = false;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = findViewById(R.id.sb_timer);
textView = findViewById(R.id.tv_timer);
btnStart = findViewById(R.id.btn_start);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.media_sample_01);
seekBar.setMax(300);
seekBar.setProgress(30);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
update(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private void update(int progress) {
int minutes = progress / 60;
int seconds = progress % 60;
String secondsFinal = "";
if(seconds <= 9) {
secondsFinal = "0" + seconds;
} else {
secondsFinal = "" + seconds;
}
seekBar.setProgress(progress);
textView.setText("" + minutes + ":" + secondsFinal);
}
public void startTimer(View view) {
if(counterIsActive == false) {
counterIsActive = true;
seekBar.setEnabled(false);
btnStart.setText("STOP");
countDownTimer = new CountDownTimer(seekBar.getProgress() * 1000, 10) {
@Override
public void onTick(long millisUntilFinished) {
update((int) millisUntilFinished / 1000);
}
@Override
public void onFinish() {
reset();
if(mediaPlayer != null)
mediaPlayer.start();
}
}.start();
} else {
reset();
}
}
private void reset() {
textView.setText("0:30");
seekBar.setProgress(30);
countDownTimer.cancel();
btnStart.setText("START");
seekBar.setEnabled(true);
counterIsActive = false;
}
@Override
protected void onPause() {
super.onPause();
if(counterIsActive) {
countDownTimer.cancel();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(counterIsActive) {
countDownTimer.cancel();
}
}
}
activity_main.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sb_timer"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0:30"
android:id="@+id/tv_timer"
android:textSize="60sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start"
android:id="@+id/btn_start"
android:onClick="startTimer"/>
</LinearLayout>
'System Programming > Android' 카테고리의 다른 글
[Android/Java] video view 가로 모드 (0) | 2021.06.14 |
---|---|
[Android/Java] 날씨 api (0) | 2021.06.14 |
[Android/Java] File stream 외부 Storage에 글 읽고 쓰기 (0) | 2021.06.14 |
[Android/Java] Text To Speech (0) | 2021.06.14 |
[Android/Java] 음성인식 해서 글로 변환 (0) | 2021.06.14 |