Advertisement
Guest User

Untitled

a guest
Aug 8th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. package com.example.tomecki.alarmclock;
  2.  
  3.  
  4. import android.app.Activity;
  5. import android.app.Dialog;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.os.CountDownTimer;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.view.animation.AlphaAnimation;
  13. import android.widget.Button;
  14. import android.widget.EditText;
  15. import android.widget.NumberPicker;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18.  
  19. import java.text.DateFormat;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Date;
  22.  
  23. public class NewTimer extends Activity implements OnClickListener {
  24.  
  25. private Button buttonStartTime, buttonStopTime, one, two, three;
  26. private TextView textViewShowTime;
  27. private CountDownTimer countDownTimer;
  28. private long totalTimeCountInMilliseconds;
  29. private long timeBlinkInMilliseconds;
  30. private long timeBlinkInMillisecondstwo;
  31. private boolean blink;
  32. NumberPicker np1;
  33. NumberPicker np2;
  34. NumberPicker np3;
  35.  
  36. @Override
  37. public void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.activity_new_timer);
  40.  
  41. buttonStartTime = (Button) findViewById(R.id.btnStartTime);
  42. buttonStopTime = (Button) findViewById(R.id.btnStopTime);
  43. textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
  44. //------------------------------
  45. one = (Button) findViewById(R.id.button_alarm2);
  46. two = (Button) findViewById(R.id.button_timer2);
  47. three = (Button) findViewById(R.id.button_stopwatch2);
  48.  
  49. buttonStartTime.setOnClickListener(this);
  50. buttonStopTime.setOnClickListener(this);
  51. //-----------------------
  52. one.setOnClickListener(this);
  53. two.setOnClickListener(this);
  54. three.setOnClickListener(this);
  55.  
  56. np1 = (NumberPicker) findViewById(R.id.numberTime3);
  57. np1.setMaxValue(59);
  58. np1.setMinValue(0);
  59. np1.setWrapSelectorWheel(true);
  60.  
  61. np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
  62.  
  63. @Override
  64. public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
  65. }
  66. });
  67. np2 = (NumberPicker) findViewById(R.id.numberTime2);
  68. np2.setMaxValue(59);
  69. np2.setMinValue(0);
  70. np2.setWrapSelectorWheel(true);
  71.  
  72. np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
  73.  
  74. @Override
  75. public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
  76. }
  77. });
  78. np3 = (NumberPicker) findViewById(R.id.numberTime1);
  79. np3.setMaxValue(100);
  80. np3.setMinValue(0);
  81. np3.setWrapSelectorWheel(true);
  82.  
  83. np3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
  84.  
  85. @Override
  86. public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
  87. }
  88. });
  89.  
  90.  
  91. }
  92.  
  93. //---------------------------------Menu + options---------------------------------------
  94. public void onClick(View v) {
  95.  
  96. switch (v.getId()) {
  97.  
  98. case R.id.button_alarm2:
  99. startActivity(new Intent(this, MainActivity.class));
  100. break;
  101.  
  102. case R.id.button_timer2:
  103.  
  104. startActivity(new Intent(this, NewTimer.class));
  105.  
  106. break;
  107.  
  108. case R.id.button_stopwatch2:
  109. startActivity(new Intent(this, NewStopwatch.class));
  110. break;
  111.  
  112. case R.id.btnStartTime:
  113. textViewShowTime.setTextAppearance(getApplicationContext(),
  114. R.style.normalText);
  115. setTimer();
  116. Toast.makeText(NewTimer.this, "Odliczanie zaczęte...",
  117. Toast.LENGTH_LONG).show();
  118. buttonStopTime.setVisibility(View.VISIBLE);
  119. buttonStartTime.setVisibility(View.GONE);
  120. startTimer();
  121. break;
  122. case R.id.btnStopTime:
  123. countDownTimer.cancel();
  124. buttonStartTime.setVisibility(View.VISIBLE);
  125. buttonStopTime.setVisibility(View.GONE);
  126. Toast.makeText(NewTimer.this, "Odliczanie przerwane",
  127. Toast.LENGTH_LONG).show();
  128. break;
  129.  
  130. default:
  131. break;
  132. }
  133.  
  134. }
  135. //---------------------------------End Menu + options-----------------------------
  136.  
  137. int time = 0;
  138.  
  139. private void setTimer() {
  140.  
  141.  
  142. time = (np1.getValue())+(np2.getValue() * 60)+(np3.getValue()*3600);
  143.  
  144. np1.setVisibility(View.GONE);
  145. np2.setVisibility(View.GONE);
  146. np3.setVisibility(View.GONE);
  147. textViewShowTime.setVisibility(View.VISIBLE);
  148.  
  149. totalTimeCountInMilliseconds = time * 1000;
  150.  
  151. timeBlinkInMilliseconds = 30 * 1000;
  152.  
  153. }
  154.  
  155. private void startTimer() {
  156. countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
  157.  
  158.  
  159. @Override
  160. public void onTick(long leftTimeInMilliseconds) {
  161.  
  162. if (time <= timeBlinkInMilliseconds) {
  163. timeBlinkInMillisecondstwo = time / 2;
  164.  
  165. if (leftTimeInMilliseconds <= timeBlinkInMillisecondstwo) {
  166. textViewShowTime.setTextAppearance(getApplicationContext(),
  167. R.style.blinkText);
  168.  
  169.  
  170. AlphaAnimation animation1 = new AlphaAnimation(0.02f, 0.01f);
  171. animation1.setDuration(1000);
  172. animation1.setStartOffset(5000);
  173. animation1.setFillAfter(true);
  174. textViewShowTime.startAnimation(animation1);
  175. }
  176. } else {
  177.  
  178.  
  179. if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {
  180. textViewShowTime.setTextAppearance(getApplicationContext(),
  181. R.style.blinkText);
  182.  
  183.  
  184. AlphaAnimation animation1 = new AlphaAnimation(0.02f, 0.01f);
  185. animation1.setDuration(1000);
  186. animation1.setStartOffset(5000);
  187. animation1.setFillAfter(true);
  188. textViewShowTime.startAnimation(animation1);
  189. }
  190. }
  191.  
  192. Date date = new Date(leftTimeInMilliseconds);
  193. DateFormat formatter = new SimpleDateFormat("HH:mm:ss"); //tutaj możesz podać różne warianty
  194. String dateFormatted = formatter.format(date);
  195. textViewShowTime.setText(String.format(dateFormatted));
  196. textViewShowTime.setTextSize(10);
  197.  
  198. }
  199.  
  200. @Override
  201. public void onFinish() {
  202. textViewShowTime.setText("Czas się skończył!");
  203. textViewShowTime.setVisibility(View.VISIBLE);
  204. buttonStartTime.setVisibility(View.VISIBLE);
  205. buttonStopTime.setVisibility(View.GONE);
  206. }
  207.  
  208. }.start();
  209.  
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement