Draw fast in Android - android

I'm developing an app for Android (API level 11), that reads some data from a device, one chunk a second. I want to plot the data, x-axis being time in seconds and y-axis being the values. One of the requirements for the program is that I must show the results of one hour of observations on one screen without any scrolling, so I must fit 3600 points on the screen. It doesn't matter that the points overlap.
I don't know how to plot the data fast enough.
I'm using androidplot, and things get really slow in approximately 30 minutes, as androidplot redraws the same points over and over again.
I tried to make some changes to it (as the sources can be freely downloaded), but in vain.
Can I somehow cache the previously drawn image and add only new points to it?
Is there something like WinAPI BitBlt() function in Android?

Related

GUI Development (in Android)

I come from a non-GUI and lower level programming environment. I have solid experience in OOP programming and algorithms, but the projects I worked on did not have any impressive user interfaces, hence my post here.
I want to understand how the graphics are updated in a mobile app.
For instance, let's take the basic example of a classic wall clock, with second, minute and hour arms. The Wall Clock app will move the seconds arm each second, likewise the hours arm each 60 mins, and minutes arm every 60 secs, problem I have is that I just cant wrap my head around creating these moving parts in an efficient way.
Am I supposed to save images of all possible combinations of second,minute and hour and just morph the 3 and display the appropriate time each second? Or is it a better idea to have the background image as a non dynamic entity, and have only the "seconds, minutes and hours" arms rotate? If this is the case, when I package my application, I will have to also include 60 + 60 + 12 = 132 images (60 images for seconds, 12 images for hours and 60 images for minutes), is this correct? If it is, this seems awfully inefficient...
Just to kind of give you an idea of what I am having problem understanding, consider another example that requires updating the graphics each time microphone hears varying noise levels: Assume that I want to create an app that measures the sound levels in an environment and it shows the dB measured from the microphone on a volume bar. If there is a song playing near the microphone with varying noise, the sound level indicator will move up and down. I understand how to update the noise values to the command prompt, serial window, or a TextView box, but I don't get how to update this info graphically.
I may be wrong, but think I must have a volume bar image that is just empty(as if the noise is at zero) and in the program, I fill this volume bar according to the dB levels acquired from the microphone, but then how can I fill the volume bar in real time depending on the noise level seen by the microphone?
These questions I have are not just Android specific, I'm sure the same approach to updating a Wall Clock app is also used in Windows application development, so I feel like if I have a good understanding of GUI development irrespective of the environment, I may be able to tackle these silly questions with ease; perhaps you can also direct me to a good book on GUI development in Java or C/C++.
For your clock example: you would not generate images for every position a clock could be in. Your app would be HUGE! You would instead make images for: the face of the clock, the minute hand, the hour hand, and the second hand. You could then take the time and calculate the angle of each hand of the clock from some standard point on the clock (this could be any point you want). You could then rotate each image to that calculated amount. This would make the clock appear to have its hands pointing at the right time.
I don't know very much about audio bars, but it would work in a similar way. Make a formula to calculate where/how large the bar should be. Then draw a bar stretched to what the formula dictates.
GUI and custom drawing can be achieved in many different ways, each have varying efficiency.
How to draw?
For example, if you want to show a cartoon character walking, in most cases you need to store each frame as image and redraw it, probably on every tick or second.
But for wall clock, you don't actually need to
Store so many images for every possibility because you can put single arrow image and rotate it
Store any image at all if you can draw it procedurally by defining its shape
Suppose you have a canvas on easel that you can draw whatever you want on it. You can choose which order you want to draw. For your wall clock example, you can first draw your background (numbers / hours) and start adding minute hand and hour hand on top it. This is called z-order.
Operating system libraries gives you widgets that you can interact with it. Canvas (picture box) is one of them and you can to draw whatever you want that you to build your custom widgets and of course Android has this too.
Android has Views and each view provides an method that you can implement your custom drawing logic:
// Implement this to do your drawing.
// Parameters
// canvas the canvas on which the background will be drawn
protected void onDraw (Canvas canvas){}
A very crude wallclock can be implemented with just using:
canvas.drawLine(float startX, float startY, float stopX, float stopY, Paint paint)
Beyond this, we can basically apply OOP concepts in GUI programming too. For instance, you can define every thing you want to draw as an object that implements a common drawing method. This is widely used on 3D game programming as usualy 3D rendering engine manages every object and asks them to draw themselves if "camera" sees them.
When to draw?
Normally this is up to operating system. It draws elements whenever it is required. However, for game like applications and for custom animations you might need to manage this by yourself.
On most operation systems, you can use seperate threads, a timer, an infinite loop or a notification event to force redraw your screen. You can achieve this redrawing with postInvalidate method on Android.
But not every environment is easy. For example, your application might need to redraw itself 30 times every second and depending the operation system and environment, you might need to acquire locks to display your UI updates correctly and might need to make your thread sleep to achieve desired framerate without keeping CPU busy. (This is used in SDL library.)
Android has an animation class to tackle common tasks.
Thing to remember
Reusing widgets that OS gives you is a good idea. You can implement a dB meter by just using a progressbar.

How to make the stock "DigitalClock" randomly appear on screen in different location every minute

absolute beginner (to android) here,
I've got a very simple android project in eclipse, Basically just a fullscreen activity with a digital clock placed inside a Linearlayout. I just want that clock to appear in a random postion of the screen every time the minute changes or every 30 seconds or whatever.
Now I'm not asking someone here to magic up some code that will make this work, I realize this is going to a lot of time and research (which I am doing!). Even if someone could point me in the right direction as where to look as nowhere seems to have what I need.
A very general question. Lets see.
I believe your clock has co-ordinates (x,y) that represent center. Also there will be some range of values that it can take (depending on screen resolution?). Every 30 seconds generate two random numbers from that range and use it as new center.

Android frequency of onTouchListener?

I'm developing an app that essentially is Paint style. The user touchs
the screen and can draw images. However I would like to measure the
speed of the user's movements. At the moment I take the distance
between the X and Y coordinates of the event.getX/Y and the previous
values and calculate the difference. This is directly proportional to
the speed of the movement provided that the timing intervals of the
onTouchListener are constant. Is this the case for Android? I know for
iPhone the Listener actually changes it's frequency depending on the
input.
Furthermore if it is a constant value, does anyone know what it is? So
I can portray the speed in useful units (mm/sec) rather than an
arbitrary value.
I don't think it's the same for Android (but I have to say I'm not 100% sure). But since this could potentially differ even between manufacturers, could you not look at System.currentTimeMillis() on the two events, to calculate the speed yourself?
Sure this is a decade old question, but I have a specific answer. Following the TutorialPoint's Touch Event Tutorial, and modifying the textView size to fill the screen, I tested the callback frequency by logging the times it was called before release. I first held my finger in one position for exactly 10 seconds which resulted in 19 and 9 onTouchListener()->onTouch() event calls in two trials. Moving my finger as fast as I could back and forth resulted in counts within a few counts off of 600 for a 10 second time period in two attempts.
This equates to an exact maximum of 60 Hz for my S8+.
I logged my coordinates, exported them into Excel and did a scatter plot. Faster movement correlated with points becoming more sparse. In my application I want to have a user draw a wave to play as a tone for experimenting with different wave forms and how they sound. For good sound quality, I need at least 20 KHz, with a target of 40.1 KHz.

Android display refresh rate

I'm developing an Android app which acts like a movie clapperboard/clapboard/slate. Is there any way in which I can set the display's refresh rate?
It is very important because when you edit the movie it's necessary to "land" on specific frames. The point is that if the timer is set to 25 frames per second, I need the display to update exactly 25 times per second, when the timer changes its value.
The problem on physical devices is that, let's say my Samsung Spica GT-I5700 returns a refresh rate of 62.016 which is totally inappropriate for a 25 fps timecode, and when editing you can see Frame1-Frame1-Frame2-Frame2 etc. or intermediaries, when you should see exactly Frame1-Frame2 etc.
The point is that I would need the refresh rate to be in sync with the timecode. If the user sets 25 fps, then the display should refresh exactly 25 times per second.
Any ideas, please? Thank you!
Unfortunately, there is no way to set an arbitrary refresh rate for any given device, whether it be android or not. Video circuitry is limited to the fidelity of its timers, and like all discrete systems, cannot operate with continuous precision. However, this shouldn't be too much of a problem. If you're performing a dirty render (on demand), simply base your frame build around a 25Hz system timer, and the video hardware will render it during the next raster pass - a 60Hz raster pass will almost certainly occur within your 25Hz interval. You'll also have to consider frame-dropping when the system is too busy to honour a 25Hz interval. If you're using fixed rate rendering, simply use the elapsed time between renders in order to determine the appropriate frame for a given frame rate.

My app runs too fast on some Android devices

I just released an app, Avoid The Spikes! on the android market.
Some friends of mine have downloaded the apps on their phones/tablets, and I am told it runs too fast. It runs just fine on my android (HTC Dream). Is there any way to tell how much faster it would run on certain androids, and how to adjust it accordingly? I have searched low and high and have yet to find an answer.
I have seen your game on Android Market and tried it.
You cannot actually determine the minimal and maximal speed you will obtain on the various existing hardware, as:
New hardware appear on the market every day
On a given hardware, other factors may influence the execution speed of your app.
What you should do is making your game independant of the framerate. To do so, avoid making your objects moving of a fixed distance each time you update their position. Prefer making them moving of a fixed distance for a certain amount of time.
To do so, measure the time elapsed since the last update each time you are updating the object's position, and calculate the distance to move to accordingly.
This way, depending of the quality of the hardware, these objects will jump or move nicely, but at least they will do so at the same speed, and this speed will be determined by you. This is a bit the same thing when you are playing a FPS game on a brand new machine or an old one: the speed of the motion is constant, only the framerate is not. On the old machine you have the impression the objects jump, while on the new they move gracefully, but it works in both cases (well, whithin certain limits, of course, for the old machine). And a machine can NEVER be too fast that way.
To give you some pseudo-code, for a paratrooper falling from the top to the bottom of the screen:
this is the loop to avoid:
while (playing) {
update_positions();
display();
}
update_positions() {
paratrooper.y -= 10; // the paratrooper's altitude decreases of 10 at each update
}
On a slow hardware it will take hours to cross the screen. On a fast hardware this will be way too fast, you won't even see the paratrooper falling.
this is the proper way to do:
while (playing) {
update_positions();
display();
}
update_positions() {
time_elapsed = getSystemTimeInMS() - lastUpdateTimeInMS();
paratrooper.y -= 0.01 * time_elapsed; // the paratrooper's altitude decreases of 10 per second
setLastUpdateTimeMS(getSystemTimeInMS()); // This way it includes the calculation time
}
On a slow hardware, crossing the whole screen may take 15 displays while on fast hardware it would take 45 displays, but in every cases your paratrooper is falling at 10dp per second.
Sounds like your app is a game? If so, you probably have a game loop that just runs the game as fast as possible. I've found this post about implementing game loops to be very useful: Game Loops, when I created my game which involved time.

Categories

Resources