Para poder hacer una cuenta atrás, Android cuenta con un objeto especial llamado CountDownTimer que permite indicar en milisegundos la cantidad de tiempo de la que hará la cuenta y el intervalo de tiempo que se quiere descontar en cada ejercución, Al tratarse de una interfaz obliga a sobrrescribir el método onFinish() y el método onTick(), ejecutados cuanto se termina la cuenta y cuando se realiza el intervalo indicado. Para utilizar la barra de progreso se ejecutan los métodos setMax(), setProgress() y getProgress() para modificar o acceder a los valores de la misma
<?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:gravity="center" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00:00" android:textSize="75sp" android:id="@+id/tiempo"/> <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" android:layout_margin="20dp" android:id="@+id/progreso"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp"> <ImageButton android:id="@+id/start" android:layout_width="75dp" android:layout_height="75dp" android:background="@drawable/play"/> <ImageButton android:id="@+id/stop" android:layout_width="75dp" android:layout_height="75dp" android:background="@drawable/stop"/> <ImageButton android:id="@+id/reset" android:layout_width="75dp" android:layout_height="75dp" android:background="@drawable/reset"/> </LinearLayout> </LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener { TextView tiempo; ImageButton play, stop, restart; ProgressBar progreso; CountDownTimer reloj; long tiempoRes; boolean pausado; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); instancias(); acciones(); } private void acciones() { play.setOnClickListener(this); stop.setOnClickListener(this); restart.setOnClickListener(this); } private void instancias() { tiempo = findViewById(R.id.tiempo); play = findViewById(R.id.start); stop = findViewById(R.id.stop); restart = findViewById(R.id.reset); progreso = findViewById(R.id.progreso); progreso.setMax(9000); progreso.setProgress(0); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start: reloj = crearTimer(90000); reloj.start(); reserProgreso(); break; case R.id.stop: if (reloj != null) { reloj.cancel(); } break; case R.id.reset: if (reloj!=null){ reloj.cancel(); reloj = crearTimer(90000); reloj.start(); reserProgreso(); } break; } } public CountDownTimer crearTimer(long rest) { reloj = new CountDownTimer(rest, 100) { @Override public void onTick(long millisUntilFinished) { tiempoRes = millisUntilFinished; progreso.setProgress(progreso.getProgress() + 10); traducirMilisegundos(millisUntilFinished); } @Override public void onFinish() { } }; return reloj; } public void reserProgreso() { progreso.setMax(9000); progreso.setProgress(0); } public void traducirMilisegundos(long milis) { int minutos = (int) ((milis / 1000) / 60); int segundos = (int) ((milis / 1000) % 60); tiempo.setText(String.format("%02d:%02d", minutos, segundos)); } }
[ezcol_1third][/ezcol_1third] [ezcol_1third]
[/ezcol_1third] [ezcol_1third_end]
[/ezcol_1third_end]