Set frame rate to ViewSwitcher alpha animation - java

I have ViewSwitcher with two ImageView. It has crossfade animation (as first appears, the other disappears). It is Done using AlphaAnimation, and
viewSwitcher.setInAnimation(inAlphaAnimation);
viewSwitcher.setOutAnimation(fadeAlphaAnimation);
Is very smooth and nice. But I wanted to make gif from it and so, I took screenshots from a layout to "catch frames" and created a gif from it. It is all good. But I need the animation itself to be like a gif with a frame rate, so it won't be smooth. Is it possible to heave such effect?
Thank you.

you can improve anim using hardware layers to app
http://blog.danlew.net/2015/10/20/using-hardware-layers-to-improve-animation-performance/
Hope this helps..

I searched around more and found this:
https://developer.android.com/guide/topics/graphics/prop-animation.html
Frame refresh delay: You can specify how often to refresh frames of
your animation. The default is set to refresh every 10 ms, but the
speed in which your application can refresh frames is ultimately
dependent on how busy the system is overall and how fast the system
can service the underlying timer.
I found this method that sets frame rate to all animations:
ValueAnimator.setFrameDelay(long frameDelay);
Hope this will help others)

Related

Java - Different ways to fade and disply images

I'm starting to learn Java and i came across an excersize where i need to fade away one image and display by fading in another image.
My solution to this excersize is to have one imageView and fading out the first image, then switching the image source to the second image and fading the imageView in so it should display the new image. Doing that so it will display all the images i want by fading out then in with a new image.
this is my code for the program:
public void fade(View view){
ImageView simpsonImageView = findViewById(R.id.simpsonsImageView);
simpsonImageView.animate().alpha(0f).setDuration(3000);
simpsonImageView.setImageResource(R.drawable.bart);
simpsonImageView.animate().alpha(1f).setDuration(3000);
simpsonImageView.animate().alpha(0f).setDuration(3000);
simpsonImageView.setImageResource(R.drawable.lisa);
simpsonImageView.animate().alpha(1f).setDuration(3000);
}
Now i have seen in the tutorial i'm learning from that the tutor used different imageView for each image. I wanted to know which solution is correct or at least acceptable amoung these two. Or it dosent really matter and that both solutions are fine.
There is one thing which you can only achieve when using two ImageViews: you can crossfade the two images so that the screen is never entirely empty.
In the context of your exercise however, you only want to exchange images sequentially.
From a performance point of view, one ImageView may be better than two because it will obviously take less memory and CPU time but I doubt that this will have a noticeable impact on modern devices.
So as long as you don't animate lots of pictures simultaneously (think of football teams instead of the Simpsons), both solutions are fine.
Please note that with your code as-is there will be no animation visible at all and the ImageView will appear to only show the second picture. This is because animate() triggers an animation but it does not wait until the animation is finished. So you need to work with an AnimationListener or use Handler.postDelayed() to swap pictures and start the next animation only as soon as the previous one is finished.

Real time logo detection in live camera preview with OpenCV on Android

I am new to computer vision but I am trying to code an android app which does the following:
Get the live camera preview and try to detect one logo in that (i have the logo in my resources). In real-time. Draw a rect around the logo if found. If there is no match, dont draw the rectangle.
I already tried a couple of things including template-matching and feature detection using ORB.
Why that didnt work:
Template-matching:
Issues with scaling and rotation. I tried a multi scale variant of it but a) the performance was really bad and b) the rectangle was of course always shown trying to search for the image. There was no way to actually confirm in the code if the logo was found or not.
ORB feature detection:
Also pretty slow (5-6 fps) but it worked ok-ish. The other problem was that also i never could be sure if the logo was in the picture or not. ORB found random matches even if the logo was not in the picture.
Like I said, I am very new to this. I would appreciate the help on what would be the best way to achieve:
Confirm if a picture A (around 200x200 pixels) is in ROI of camera picture (around 600x600 pixels).
This shouldnt take longer than 50ms per frame. I dont know if thats even possible though. So if a correct way to do this would take a bit longer than that, I would just do the work in a seperate thread and only analyze like every fifth camera frame or so.
Would appreciate any hints or code examples on how to achieve that. Thank you!
With logo detection, I would highly recommend using OpenCV HaarClassifier. It is easy to generate training samples from a collection of images of the logo, or one logo image with many distortions.
If you can use a few rules like the minimum and maximum size of the logo to be detected, and possible regions on the image where it can appear, you can run the detector at a speed better than you mention with ORB.

Android: OpenGL: When to load textures?

I am trying to make an OpenGL game which has say 3 stages: Welcome, Battle and Armor. Each stage has different logic and works with different textures.
When is the best time to load those textures, all in the beginning, or load the ones that are used by each stage when it is activated. If so, should I then delete them when switching to another stage?
If the second method is best, how can I show static splash screen (image) while loading the textures per each stage?
Well, loading texture (especially from the sd card) can take time. The reason to load them before you need them is to avoid having a loading screen between stages. If the player will often switch between stages, and your texture are not so big, you might want to load them at startup (giving the pain of waiting for the game to load only once, even if it would be longer). You have to balance your choice between the memory that is available for you, and how much you want to piss the player by interrupting him in his playing experience.
You could also distract the player by displaying a scoreboard at the end of a stage to show him how good he's just been, instead of a loading bar.
If you can anticipate when the player will soon switch to another stage, you can try to stream the texture in background a little before, but it's a lot more complicated to do (well, especially on a mobile platform), so you'd need to do some research for that.
I think you should load them only if they are needed for that stage. Why waste memory by loading textures on your Video RAM if the player may not make it to the next stage?
You can show a dialog which is custom to your needs and dismiss it when you are ready. I don't have any samples right now.

How do I control the speed of an animated GIF?

I would like to control the speed of an animated GIF in a Java applet. Is there a way to do this? If not, is there a way to access the data of an animated GIF so the applet can draw the animation image by image on its own?
I think that the frame rate is embedded into the GIF. You could somehow extract the images from the GIF, but that's harder than starting with the individual images and animating them in JS, which is harder than recreating the GIF with your preferred frame rate.
If you're going to use the GIF only once and the frame rate isn't going to change, just recreate the GIF. If you need to change the speed based on inputs from your applet, you could use the approach here. It alternates between two gifs, but there's nothing stopping you from loading in PNGs and alternating through an Array of those.
The animated GIF format is consists of data for each frame along with a delay value (how long to show that frame). The delay is separate for each frame, and is stored as two bytes and measures as hundred's of a second.
Netscape (back when it was the web), couldn't show the frames faster than 10 per second. So lots of tools just said screw it, and set delay for all the frames to 0. Lots of old gifs and old tools, have keep these screwed up frame delay times around.
With faster computers and browsers, they worked around this by checking if any of the frames had a delay <= 50ms (20+ fps). IF they did, the delay was increased to 100ms (10fps).
In principle, the best solution would be to just fix the GIF you're using to have accurate frame delays in them. If that isn't viable, use that same old workaround. Break the frames out of the animated GIF and do the animation yourself, defaults to a 100ms delay if the specified delay is <= 50ms. This will give you the same behavior as what you see in most web browsers.
Read about this a while ago. Think most of the details on mentioned on wikipedia (including the animated GIF format and the per frame delays). If it you really want some solid references, I can dig them for you.

Which component to subclass to draw in Java2D (for a 2D game)

I have created my own canvas that has been extended from the JPanel, however I have noticed that w/ the content and so forth, that all of the sudden my FPS took a hit. I am following the swing rules from Filthy Rich Clients, by using paintComponent, creating a clip area, only redrawing what has been changed, and so forth. I have the FPS set to a constant 50 FPS, and I notice that sometimes my FPS will jump down to 31/32 FPS and go back up to 50 and so forth. While running my program it's only using about 25MB of RAM and 0 of my CPU, even when rendering. I also have OpenGL set.
Note: I have NO images, this is strictly using the shapes in Graphics.
Is there a major performance hit by drawing everything on a JPanel? Should I be extending a different component (I keep seeing Canvas component)? How "smart" is it to build a game such as tetris (or any of the other retro games) in JPanel?
It's a possibility that this is a timer issue, as I just added 100 additional painting calls and the FPS still does the 50 32/31 thing.
After much investigation I have found that the issue is not the JPanel at all. As a matter of fact, the issue is with the Timer in java. It's not 100% accurate, which has resulted in the FPS being way off. My solution to fixing this was reading this: koonsolo.com/news/dewitters-gameloop
I realise that you've found an issue with the Timer class, however I have another comment for you, which you may find useful:
You've not mentioned whether you're using the double-buffer technique. If you're not, then you may notice a FPS improvement.
Just in case you're not familiar with the technique, it involves creating a separate panel buffer, redraw your scene on this buffer, then switch this buffer with the one on the screen, etc.

Categories