What is difference b/w invalidate() and postInvalidate()? - android

I am using PostInvalidate(40, Rect); means After 40 miliseconds it should again call the OnDraw() but it seems it is taking more than 40 miliseconds in calling onDraw().
Shall I invalidate() this on timer or handler. It is necessary for me to call it within 40 miliseconds
Any suggestions

From the documentation:
Cause an invalidate of the specified area to happen on a subsequent
cycle through the event loop. Waits for the specified amount of time.
Which means that the system will wait 40 milliseconds, then in a subsequent cycle through the event loop, it will do the invalidation. If you have a slow event cycle, you will get a slow response to the invalidation.
That means that the time required for the invalidation to take effect is limited by how long it takes to cycle through your event loop. It will never happen in 40 milliseconds. Whatever average time it takes for an Invalidate() to work, it will take that amount of time + 40 milliseconds more (at least).

Related

The method loadContainerPreferNonDefault takes too long during initialization

As docs https://developers.google.com/tag-platform/tag-manager/android/v4#init describing, the method loadContainerPreferNonDefault is non-blocking, while it still takes more than 500ms in Pixel 3.
Is the duration normal? Or the container file(11KB) is too large to load quickly.

Understanding Interpolation

I have been reading up on game loops and am having a hard time understanding the concept of interpolation. From what I seen so far, a high level game loop design should look something like the sample below.
ASSUME WE WANT OUR LOOP TO TAKE 50 TICKS
while(true){
beginTime = System.currentTimeMillis();
update();
render();
cycleTime = System.currentTimeMillis() - beginTime;
//if processing is quicker than we need, let the thread take a nap
if(cycleTime < 50)
Thread.sleep(cycleTime);
)
//if processing time is taking too long, update until we are caught up
if(cycleTime > 50){
update();
//handle max update loops here...
}
}
Lets assume that update() and render() both take only 1 tick to complete, leaving us with 49 ticks to sleep. While this is great for our target tick rate, it still results in a 'twitchy' animation due to so much sleep time. To adjust for this, instead of sleeping, I would assume that some kind of rendering should be going on within the first if condition. Most code samples I have found simply pass an interpolated value into the render method like this...
while(true){
beginTime = System.currentTimeMillis();
update();
render(interpolationValue);
cycleTime = System.currentTimeMillis() - beginTime;
//if processing is quicker than we need, let the thread take a nap
if(cycleTime < 50)
Thread.sleep(cycleTime);
)
//if processing time is taking too long, update until we are caught up
if(cycleTime > 50){
update();
//handle max update loops here...
}
interpolationValue = calculateSomeRenderValue();
}
I just don't see how this can work due to the 49 tick sleep time? If anyone knows of an article or sample I can check out please let me know as I am not really sure what the best approach to interpolation is...
I know its a bit late, but hopefully this article will help
http://gameprogrammingpatterns.com/game-loop.html
It explains game time scheduling very well. I think the main reason you are a bit confused is because of passing the render function the current elapsed time. Oh course this depending on which system you are using but conventionally the render doesn't modify the Scene in any way, it only draws it, therefore it doesn't need to know how much time has passed.
However the update call modifies the objects in the scene, and in order to keep them in time (e.g. playing animations, Lerps ect...) then the update function needs to know how much time has passed either globally, or since the last update.
Anyway no point me going to fair into it.... that article is very useful.
Hope this helps

Syncing Time in Android

I am trying to sync time between two android devices. The precision has to be upto 5 ms. The gps and network time werent this precise so i thought about sharing time between the devices over the local network and syncing the time using PTP(Precision time protocol).
Now as i can't change the time on non-rooted devices so i thought about saving the time difference shared by device and kept on showing the time to user in a textview.
Now the textview needed to be updated every one ms so the user can see the time in ms too.
I am updating the textview in a thread which is updated every ms.
class CountDownRunner implements Runnable {
// #Override
public void run() {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
while (!Thread.currentThread().isInterrupted()) {
try {
setCurrentTime();
Thread.sleep(1); // Pause of 1/100 Second
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
}
}
}
}
This is where the problem comes that after syncing the time , the time diff is less then 5 ms but after some time of syncing the time starts to drift a part and after 10-15 mins the time diff is more then 1 sec.
So any ideas on how to rectify this issue?
seems like making thread.sleep(1000) works more like thread.sleep(1003 or 1004) and these changes keep on adding up to make a bigger drift
As I noted, Android is not a RTOS.
Moreover, you should not care whether sleep() returns in 1003 or 1004 milliseconds. You seem to be combining two things:
Determining how much time has elapsed since you synchronized the time
Displaying the current synchronized time
These have nothing to do with each other.
Use your sleep() loop -- or, better yet, postDelayed() for greater efficiency -- only to get control roughly every second to know that you need to update the TextView.
To determine what value to show in the TextView, do not add some assumed amount of time based on the sleep(). Instead:
When you get the synchronized time value from the other device, also save the current value of SystemClock.elapsedRealtime()
To determine how much time in milliseconds has elapsed since the synchronization, subtract the saved value of elapsedRealtime() from the current value of calling elapsedRealtime()

Android Chronometer - One second "ticks" late

The app is a sports timer for cycling, skiing etc, where racers start at regular intervals. e.g. 1 minute.
In my implementation of OnChronometerTickListener I notice that the calls occur at intervals significantly longer than 1000 mS. I use the elapsed time (between the Tick and the Chronometer's base) to count down the last 5 seconds for each interval. Due to the late callback, I can get ticks at, say,
55,500
56,600
57,750
58,870
59,980
61,110
I can therefor skip a whole second when I use m_Elapsed % 1000.
I have even seen the text in the Chronometer get behind and have to skip a second.
I have no problem with accuracy when I do calculations based on the Chronometer's base time and current system time.
Do I have to write my own Chronometer using finer callbacks?
Or is there some other fix?

Do after specific time, Android Runnable

Ive got an app with a class that implements Runnable. Where a thread is started and the run() methid overridden. This runs my graphics.
1.st question : how often is the run() called upon? i havent set a time for this so it must be a default value?
2.nd question : i want stuff to be done after a certain amount of time (2min,5min,10min) etc. What would be the best way to go about doing this, i was thinking about using an int as an counter and once it hits a specific value does what i want.
1.st question : how often is the run() called upon? i havent set a time for this so it must be a default value?
The run() method in your Thread is called when you call it eg. yourThread.start();
2.nd question : i want stuff to be done after a certain amount of time (2min,5min,10min) etc. What would be the best way to go about doing this, i was thinking about using an int as an counter and once it hits a specific value does what i want.
There are to options. Either you could call Thread.sleep() method (NB: Never do this in your UI thread).
Or you can do it the way you described above. So in your run() method you would have a while() loop and check on every iteration if the difference of the lastUpdate and the current time in milli seconds is bigger than the wanted period eg. 2 min, 5 min or 10 min.
I hope this helps.
Regarding question 2 - use ScheduledExecutor
1.st question : how often is the run() called upon?
You can find out for yourself, put this at the start of your Runnable:
Log.v("Running Runnable", System.currentTimeMillis() + "");
2.nd question : i want stuff to be done after a certain amount of time (2min,5min,10min) etc.
Extend a HandlerThread (it initializes the Looper for you!), add a Handler as a class variable, and use the Handler's postDelayed() or postAtTime() methods.
The exact amount of time in between calls to run() depends on the processor. The time between each call is the sort of thing that's really visible by the nanosecond. If you're trying to create a timer, I'd recommend using System.currentTimeMillis(), calling it in the run() method, and once the difference is greater than or equal to 1000 milliseconds, the actual timer decrements by one. This will keep track of seconds, and you can use it as a base for minutes and generating other events at specific times.

Categories

Resources