In my app, animated image should repeat every time with different time interval.
like i want any of between 1000 to 5000.
I applied below code but this not work properly. Image move very fast.
So, plz help me out of this problem.
Random gen = new Random();
anim.setDuration(new Random().nextInt(5000-1000+1));
I think you just need to set interpolator.
anim.setInterpolator(new AccelerateDecelerateInterpolator());
So its speed will be proportioned
First of all - you even didn't check values returned by nextInt(5000-1000+1).If you check this you will discover that values are from 0 to 4001.
If you need values from 1000 to 5000 use:
anim.setDuration(1000 + gen.nextInt(4000));
Related
Is it possible to clear the values from a STEP_COUNTER listener? I thought that you might be able to do it by unregistering the listener, but apparently the values are preserved. I'm registering a listener on the step_counter sensor, checking for changes, but then I want to flush or clear these values after a certain period of time. Any ideas?
Apparently not but I did mine with a bit of math. I store the value of the count when the step_counter is initiated and then do some math.
mFinalCount = mCurrentCount;
mUpdatedCount = mCurrentCount - mFinalCount*2;
mNewCount = count - mUpdatedCount;
// count is the Float.valueOf(event.values[0]
Basically lets say the step counter is 300, the mUpdatedCount becomes -300 so now count(300) - mUpdatedCount(-300) = 0
I'm really new to android by the way but this worked for me.
Ok so delta-time is 1/fps right? Say the fps was 50, then delta time would equal 1/50= 0.02. My question is that frame rate varies(one second it might be 50, another it might be 52). So say for one second the fps is 50, that means that delta time will be equal to 0.02, but the NEXT second the fps will be 52, but we don't know that yet. So our animations are being done with delta time of 1/50 but the fps is actually 52. Until the next second is finished we won't know that the fps has changed. This may not seem like a big deal if the changes are small, but if they become very big then we have a problem. So the thing is we are always doing calculations based on the previous second's fps. I want to know how to solve this. Thanks!
You're right that 1/fps = delta-time. However, fps isn't known at the present, and as you pointed out going about it this way would cause a problem! In practice, the formula is re-arranged such that 1/dt = fps.
So, we determine delta-time by determining how much time has passed since the last update( deltaTime = (CurrentTime - LastTime) ). If we were to have a variable that we add delta time to every every update (say, deltaCounter += deltaTime), and another variable which is a counter we add one to each update (Counter++), we would see that when deltaCounter is becomes equal to 1, the Counter variable is our fps for that second.
Further Reading on Delta Time and its Implementation
this is an extension to this question: Android - Handling a grid
Basically, I am wondering how to make a grid of 4x4 buttons randomly change their features(text, color, etc.) I don't need help setting the actual change in text or color, I just need a way to go about something like this. I don't know if I should write an array and choose from there, or use the GridView. Just a little start to something is all I need, I'm not asking for lots of code. Thanks in advance, I really appreciate it.
If your 4 ids are sequential as is
btnId1 = 8006;
btnId2 = 8007;
btnId3 = 8008;
btnId4 = 8009;
Random rn = new Random();
int chooseId = btnId1 + rn.nextInt(btnId4 - btnId1 + 1);
You can generate a random number like so:
Random r=new Random();
r.nextInt(4);
this will give you a random number in the range [0,4).
Based on this random number, you change the properties of the appropriate tile/square which you can enumerate yourself from 0 to 3 since you have 4 tiles in total.
From both your questions it seems that you lack some basic programming knowledge of algorithms/approaches to problems. You should try to fill that gap, which will make you progress MUCH MUCH faster in your programming endeavors!
I need to modify/access my mat on Android, but it is really really slow (it took about 2 minutes to run on a 3500*100 mat).
I need to set some value to 0, but not all, and I am using this line to modify it.
this.getMyMat().put(i, j, 0);
Any idea to get it a bit faster ? My code in C++ takes at least 50 times less time to run, doing this way :
((myMat.data + myMat.step*row))[j] = 0
You can use rowRange() or colRange() to extract the submatrix you wish to be zeroed out, and call setTo() to actually fill in the values. This will be faster than iterating pixel-by-pixel.
Mat rows = this.getMyMat().rowRange(0,3);
rows.setTo(new Scalar(0));
I'm developing an android application using GPS. I'd like to implement a feature that displays the users average speed over the 1/5/15 minute. Something like the CPU load on unix. I can calculate average easily by cumulating the distance traveled second by second and divide it by the elapsed time, but I can't think of a smart way of calculating the moving average.
Obviously I can get id done by putting the distance between the last and the current position in an array every second while deleting the oldest value.
I'm looking for a neat way of doing this.
Heres one way to go about it that is pretty straight forward:
If you are sampling position every second keep 901 samples in a queue, thats 15 mins worth (and 1 extra).
Position 0 is the most recent measurement, effectively your current position.
For an average speed over the last X minutes:
s = X * 60;
point1 = postion_queue[0]; // this is your current position
point2 = postion_queue[s]; // this is your position s seconds ago
d = distance_between_points(point1, point2);
speed = d / s;
speed is now distance units per second, convert to mph, or kph or whatever units you need. Different values of X can be used for any average between 1 and 15 minutes.
You will need to store all the values for the whole time span, as you already suggested. The reason is that you somehow need to "forget" the contributions of the old values to the moving average. You can't do that exactly if you don't know what these values where (i.e. if you do not store them).
In your case, 1 value each second for 15 minutes amounts to 15 * 60 = 900 data points, that should be OK.
Note that you do not need to perform a sum over the whole array each time you update: You can calculate the new moving average from the number of data points, the new value and the value you are "forgetting" at that moment:
new_average = (n * old_average - x_forget + x_new) / n
Here, n is the number of data points (900 in your case), x_forget is the value you are "forgetting" and x_new is the latest value. You then drop x_forget from the front of your array and store x_new at the end. Instead of an array you might want to use a queue implemented via a linked list.