Making a countdown "Pie" Clock using rotating images - android

what i wish to make is a pie type clock that counts time from 1 minute to 0.
My question is how do i add images or remove them using a script to a layout view and make them rotate at the center of the layout view, depending on a countdown.
What would be the best approach for it? where do i start? and what would i need to read about to make it?
Thanks in advance :]

One way of doing this is that you can create all the images you need and use CountDownTimer to relate to each image.
I do not know what you are exactly trying to do but creating an animation (flash) and putting it into your application may work as long as you have the timing done correctly.
For an analog clock, this is what I use:
public void CDT() {
//CHANGE 31000 TO WHATEVER YOU WANT. FOR 60 SEC, I WOULD USE 60000 OR 61000
new CountDownTimer(31000, 1000) {
public void onTick(long millisUntilFinished) {
//THIS MAKES IT LOOK NICER WHEN IT COUNTS DOWN (eg. :09 instead of :9)
if(millisUntilFinished < 10000) {
textTimer.setText(":0" + millisUntilFinished / 1000);
}
else {
textTimer.setText(":" + millisUntilFinished / 1000);
}
}
public void onFinish() {
textTimer.setText(":00");
new AlertDialog.Builder(SplitEmUpBeta2Activity.this)
.setTitle("TEXT").setMessage("TEXT")
.setNeutralButton("TEXT", null);
.show();
}
}
.start();
}
If you really want to create the "Pie" clock, you need to set onTick to replace the image of the clock with the appropriate one (remove the old and add the new). For example:
public void onTick(long millisUntilFinished) {
if(millisUntilFinished == 60000) {
//REPLACE IMAGE (REMOVE OLD AND REPLACE WITH NEW)
}
// AND SO ON UNTIL YOU FINISH ALL THE IMAGES
}

Related

CountDownTimer skipping one second when displaying on TextView

I want to display a simple CountDownTimer in a TextView, but it seems to go from 30 down to 28 right away, as if there was a lag in between. I'm not sure how to go about on fixing this small bug. Here is my code:
This is in the click listener of the Button:
new CountDownTimer(30000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
coolDownTimer.setText("Cool Down for: " + String.valueOf(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
animatedPic.setClickable(true);
// reregister the proximity sensor
sm.registerListener(sensorListener, proxSensor, SensorManager.SENSOR_DELAY_NORMAL);
coolDownTimer.setText("GO!");
}
}.start();
There are 2 issues:
The first issue is that the countdown timer doesn't execute until time has elapsed for the first tick, in this case, after 1000ms.
The second is that the elapsed time is only approximate. millisUntilFinished is not guaranteed to come in increments of the interval (you can see that if you don't divide by 1000, the first tick is slightly under 29000).
Another thing to keep in mind is that you're not guaranteed to receive a tick call. That is to say, if the device did not have enough time to complete a tick, it may skip it (generally only noticeable for faster intervals).
To solve issue 1, you can simply run the onTick code (or refactor it into its own method) and run it as you start the countdown timer.
For issue 2, you can simply round the number.
For example:
new CountDownTimer(30000, 1000)
{
#Override
public void onTick(long millisUntilFinished)
{
performTick(millisUntilFinished);
}
#Override
public void onFinish()
{
animatedPic.setClickable(true);
// reregister the proximity sensor
sm.registerListener(sensorListener,proxSensor,SensorManager.SENSOR_DELAY_NORMAL);
coolDownTimer.setText("GO!");
}
}.start();
performTick(30000);
void performTick(long millisUntilFinished) {
coolDownTimer.setText("Cool Down for: " + String.valueOf(Math.round(millisUntilFinished * 0.001f)));
}
If you want to make sure the appropriate value is updated with minimal latency, you may want to consider reducing the interval.
It is skipping because CountDownTimer is not a precise timer. Try to print just the millisUntilFinished without dividing it you'll see some excess number that it wont be exactly 29000 when it hits its first tick.
you can refer on this thread for the solution.

Multiple CountDownTimers with a uiversal Cancel command

Im back to ask another question :)
In an app I am making, I need a selector to choose one of 4 different countdown timers, for example round1, round2, etc.
but i have 2 problems with the same source. Using the default countdowntimer implentation which doesnt involve naming it round1 but running as a straight countdowntimer, I can cancel it in onPause using
coutdowntimer.cancel();
but after naming it round1 I can no longer pause it and the round1.cancel(); comes up as undefined.
I have spent most of yesterday and throught to 3am trying to find a solution and i thought i had it by making my timers final but it still didnt work.
after thinking about it more i relaised once ive solved this problem, I'm going to have to implement an if statement in my onpause which i would rather not do in an effort to keep my code clean.
So is there a way to implement a universal cancel command to cancel the root countdowntimer function regardless of the actual name its running under.
for example
public void canceltimers(){
//if round1,2,3,4 is runnng, cancel the main countdowntimer thread altogether.
countdowntimer.Cancel();
}
any help is appreciated and here is my current timer code.
CountDownTimer round1 = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
textview4.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
endsequence();
}
};
CountDownTimer round2 = new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
textview4.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
endsequence();
}
};
// round1.start();
ive not implemented my if statements for the level select yet but will be similar to this
if (level == 1){
round1.start();}
if (level == 2){
round2.start();}
So far i have tried setting it as final, with no joy.
I have tried
round1.cancel();
with no joy. as well every implementation of countdowntimer i can think of or find.
Now as a request, im not as amazing as making sense of code as most of you guys so if you could try and give an example of the implementation it would be appreciated, Thanks for looking and thanks for your time & help
phil
make a single timer. Add multiple timertasks to this timer. Define the COUNTDOWN_TIME.
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask() {
#Override
public void run() {
//execute code when the COUNTDOWN_TIME has ran out.
}
}, COUNTDOWN_TIME);
}
//cancels all sceduled/running TimerTasks
mTimer.cancel();

how can i set the time in digital clock

I want to build an android application, this application allow the users to answer some questions.
For every question, User have to answer it in 360 seconds, so I want to make a digital clock starts from 360secons and then down to 0 seconds
I read that I can't set time on digital clock , is it so?
or I have to use Chronometer?
this is my digital clock
DigitalClock timer;
public void init(){
timer=(DigitalClock)findViewById(R.id.dcTimer);
}
any help appreciated.
Use CountDownTimer instead.
That link also shows you how to update a TextView every 1 second.
Just change new CountDownTimer(30000, 1000) so the first parameter is 360000 instead of 30000 and that will give you a good start to your code.
EDIT : You need to import android.os.CountDownTimer and the code example I referred to is below. Good luck.
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

Showing Timer in android

My objective is to display a simple timer clock on my screen where it starts from 20 and goes on decreasing and when it becomes 0 I want to start an action.
How to achieve this ?
Thanks in advance
I don't know how you want to display it but you can do this also just by taking a TextView and keep changing its Text every second.
static int i = 20;
new CountdownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
i = i-1;
mTextField.setText(String.valuOf(i));
}
public void onFinish() {
//do your stuff here
}
}.start();

how to display Count down clock?

In my application one option is there which is used to select the application trigger time that is implemented in RadioButtons like : 10 mins, 20Mins, 30mins and 60mins
after selecting one of them the application starts at that time.. up to this working fine.
But, now I want to display a count down clock after selecting one option from above from that time onwords one clock, I want to display
That will useful to user how much time remaining to trigger the application
if any body knows about this please try to help me
Thanks for reading
Something like this for short counts:
public void timerCountDown(final int secondCount){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
if (secondCount > 0){
//format time and display
counterBox.setText(Integer.toString((secondCount)));
timerCountDown(secondCount - 1);
}else{
//do after time is up
removeCounterViews();
}
}
}, 1000);
}
But this one is even better and it has two events which update the main UI thread: on tick and on finish, where tick's lenght is defined in the second parameter:
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

Categories

Resources