Now i am developing an android app, and there are lots of animations and image resources, the ui design and the logic are also very complex. I tried to use the Tween animation and android layout to implement, but the code is very complex, and i also can't implement some parts of animations. Anyone know how to handle such complex animation at the android platform? Dose android have some tool like as flash tool to design the animation?
In android three type of anmiations.
1)TranslateAnimation
An animation that controls the position of an object
2)ScaleAnimation
An animation that controls the scale of an object. You can specify the point to use for the center of scaling.
3)AlphaAnimation
An animation that controls the alpha level of an object. Useful for fading things in and out. This animation ends up changing the alpha property of a Transformation
Check this for some example.
You can use droidQuery for complex animations. For example, to animate all your ImageViews to double their current size, do:
$.with(this).selectByType(ImageView.class)
.animate("{width:200%, height:200%}",
new AnimationOptions().easing($.Easing.BOUNCE).duration(400));
You can highly customize your animation with chained methods on the AnimationOptions Object - from the duration and interpolator to callbacks for progress, errors, success, and completion, etc.
Related
It looks like both Animations and Animators allow me to animate properties (position, opacity, scale, rotation, etc) on objects, and I'm having a hard time differentiating between the use case for both. When should I use an animator versus an animation and vice versa?
Animations are older versions of Animators. Animators where introduced in 3.0 to help overcome some short-coming that Animations have.
Animations only change the visual representation of an object. This is fine if you're just changing opacity, but it causes issues when you translate, rotate, or scale objects. In the old days before Animators, if you translated the object, you had to perform a re-layout with the new coordinates. It could be rather difficult depending on where the object moved.
Animators on the other hand change the physical properties of the objects. This means that if you move a View to a new location, the touch coordinates will be mapped at the new location without any other intervention.
Personally, I don't use Animations much anymore unless I'm developing at API's 2.3 or less. Thankfully that's becoming less of an issue. There are also some old classes that still use Animations API especially when it comes to using xml resources such as the android.support.v4.app.FragmentTransaction class (the normal FragmentTransaction supports Animators instead).
As a side note, the project NineOldAndroids was developed to mimic functionality of Animators but using Animations so you can make apps that work all the way to 1.6.
An Animation object animate the view's image. If you use this for example, to move a button around the screen you will not be able to click on it at the new visible position because it was not truly moved, but only his bitmap representation was translated. You will also not be able to change the proportions of it since you are making modifications to a bitmap. If you use xml files, place them at anim folder.
An Animator object animate the view's property (like the margin or width). If you use this to move a button around the screen you will be able to capture clicks on it in the new visible positions. If you use xml files, place them at animator folder.
If you only need cosmetic effects, like fade in or small appearance translation, using a Animation will be more efficient because it does not call layout() or measure() methods. If you do need to capture actions like click events, use a Animator.
It looks like both Animations and Animators allow me to animate properties (position, opacity, scale, rotation, etc) on objects, and I'm having a hard time differentiating between the use case for both. When should I use an animator versus an animation and vice versa?
Animations are older versions of Animators. Animators where introduced in 3.0 to help overcome some short-coming that Animations have.
Animations only change the visual representation of an object. This is fine if you're just changing opacity, but it causes issues when you translate, rotate, or scale objects. In the old days before Animators, if you translated the object, you had to perform a re-layout with the new coordinates. It could be rather difficult depending on where the object moved.
Animators on the other hand change the physical properties of the objects. This means that if you move a View to a new location, the touch coordinates will be mapped at the new location without any other intervention.
Personally, I don't use Animations much anymore unless I'm developing at API's 2.3 or less. Thankfully that's becoming less of an issue. There are also some old classes that still use Animations API especially when it comes to using xml resources such as the android.support.v4.app.FragmentTransaction class (the normal FragmentTransaction supports Animators instead).
As a side note, the project NineOldAndroids was developed to mimic functionality of Animators but using Animations so you can make apps that work all the way to 1.6.
An Animation object animate the view's image. If you use this for example, to move a button around the screen you will not be able to click on it at the new visible position because it was not truly moved, but only his bitmap representation was translated. You will also not be able to change the proportions of it since you are making modifications to a bitmap. If you use xml files, place them at anim folder.
An Animator object animate the view's property (like the margin or width). If you use this to move a button around the screen you will be able to capture clicks on it in the new visible positions. If you use xml files, place them at animator folder.
If you only need cosmetic effects, like fade in or small appearance translation, using a Animation will be more efficient because it does not call layout() or measure() methods. If you do need to capture actions like click events, use a Animator.
I'm doing some research in utilizing Android's resource directories appropriately and the following isn't clear to me:
What is the difference between the android animator resource directory and the android anim resource directory?
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
Moreover I guess the question I'm asking is what is the difference between property animations and tween animations?
I honestly think Google have done a very good job explaining the differences in their Property Animations API guide (see below).
TL;DR the main differences are:
Tween animations are succinct and allow for the manipulation of no more than the location (translation), size (scale), angle (rotation) and translucency (alpha) of views. The property animations framework is more generic and flexible: It generalizes the former case by allowing for real-time updating of any property (e.g. "foobar") of animations' target-object -- provided it has a setFoobar() method. setScaleX(), setAlpha(), etc. are just a specific case when it comes to views.
Accordingly, implementing tween animations is often easier and the code is more lightweight.
Property animations can be used over target objects of any type, not just views: the only thing that matters is the definition of setFoobar() methods as explained earlier (reflection based method look-up).
Tween animations merely perform adjustments over views' configurations, while property animations effectively modify the object. A common flaw of the former approach is that when using animations for moving views around, the associated clickable area doesn't get updated throughout the animation and gets out-of-sync with the view's effective location on the screen.
To quote from the guide:
How Property Animation Differs from View Animation
The view animation system provides the capability to only animate View objects, so if you wanted to animate non-View objects, you have to implement your own code to do so. The view animation system is also constrained in the fact that it only exposes a few aspects of a View object to animate, such as the scaling and rotation of a View but not the background color, for instance.
Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.
With the property animation system, these constraints are completely removed, and you can animate any property of any object (Views and non-Views) and the object itself is actually modified. The property animation system is also more robust in the way it carries out animation. At a high level, you assign animators to the properties that you want to animate, such as color, position, or size and can define aspects of the animation such as interpolation and synchronization of multiple animators.
The view animation system, however, takes less time to setup and requires less code to write. If view animation accomplishes everything that you need to do, or if your existing code already works the way you want, there is no need to use the property animation system. It also might make sense to use both animation systems for different situations if the use case arises.
Basically Tween animations are standard animation,
for eg: Scale, Rotate, Translate, etc. (These can be applied to any type of views)
Where are Property Animations as the name suggest are used to change the Property of any View.
For eg: Changing Alpha of ImageView, in lollipop changing translationZ value of fab button, etc.
Hope that clarifies.
Check Android View Tween Animation
A tween animation can perform a series of simple transformations (position, size, rotation, and transparency) on the contents of a View object. So, if you have a TextView object, you can move, rotate, grow, or shrink the text. If it has a background image, the background image will be transformed along with the text. The animation package provides all the classes used in a tween animation.
check this url for Property Animation
The property animation system is a robust framework that allows you to animate almost anything. You can define an animation to change any object property over time, regardless of whether it draws to the screen or not. A property animation changes a property's (a field in an object) value over a specified length of time. To animate something, you specify the object property that you want to animate, such as an object's position on the screen, how long you want to animate it for, and what values you want to animate between.
Try this shorter answer:
res/anim -
Any view Tween Animation (scale, rotate, translate).
res/animator -
Certain views Property Animation (ImageView - change alpha, FAB - set Z-order).
So I see there are three animation mechanisms in Android:
1) android.view.animation.Animation
2) android.animation.Animator
3) View.animate()
What are differences between those three? How should I decide which to use? What are benefits of using each of them?
You can read more here. In short, there are three types of animations...
View Animations
Simple tween animations.
Can only modify position, size, rotation, and transparency.
Limited to View objects.
Property Animations
Can animate any property of a View (not limited to size, rotation, position, and transparency).
Not limited to just View objects.
Drawable Animations
Animate a set of drawables in sequence.
Similar to sprites.
What you decide to use, is totally up to you and dependent on what you're trying to achieve. With this information in mind, use your best judgement.
EDIT
To clarify on your specific examples...
Animation is a base class for other Animation types like AlphaAnimation. It provides you the necessary methods to create your own Animation if you wish. This class has existed since API 1.
Animator is part of a newer set of animation tools. ObjectAnimator is an example implementation of Animator. I can't say much about it since I did not write it, but Animator and Animation seem to be very similar in that they both provide a way to create animations.
ViewPropertyAnimator is also part of the new animation tools providing an even easier way to animate View objects. It's also optimized to handle multiple animations.
For simple animations, View Animations are just fine. For more complex animations, Property Animations would be the way to go.
My app implements its own sprites by calling the following in my view's onDraw() method:
canvas.drawBitmap(sprite.getBitmap(), sprite.getX(), sprite.getY(), null);
The app is a physics simulation, and so far this has worked out great. But now I'd like to enhance the animation by morphing between images for the sprites when certain events occur.
For example- when a collision happens, I would like to play an animation of an explosion. My idea was to replace the usual sprite bitmap with that of an explosion PNG, and use Android "tween animation" to make the explosion grow larger.
But the Android tween animation example assumes that you have an ImageView defined somewhere statically in your XML configuration.
Is there a way to animate a bitmap as drawn in onDraw() using tween animation? Or do I need to convert my sprites to use some sort of ImageView? If the latter, can you point me to an example of proper sprite animation in Android?
Thanks
I built a generic Tween Engine in java that you can use to animate anything, including your sprites. It's optimized for Android and games because it does not allocate anything at runtime, to avoid any garbage collection. Moreover, Tweens are pooled, so really: no garbage collection at all!
You can see a complete demo here as an android application, or here as a WebGL html page (requires Chrome)!
All you have to do is implement the TweenAccessor interface to add Tween support to all your sprites. You don't even have to change your Sprite class, just create a SpriteTweenAccessor class that implements TweenAccessor<Sprite>, and register it to the engine at initialization. Just have a look at the GetStarted wiki page ;)
http://code.google.com/p/java-universal-tween-engine/
I'm also building a visual timeline editor that can be embedded in any application. It will feature a timeline similar to the Flash authoring tool and Expression Blend (a Silverlight dev tool).
The whole engine is heavily documented (all public methods and classes have detailed javadoc), and the syntax is quite similar to Greensock's TweenMax/TweenLite engine that is used in the Flash world. Note that it supports every Robert Penner easing equation.
// Arguments are (1) the target, (2) the type of interpolation,
// and (3) the duration in seconds. Additional methods specify
// the target values, and the easing function.
Tween.to(mySprite, Type.POSITION_XY, 1.0f).target(50, 50).ease(Elastic.INOUT);
// Possibilities are:
Tween.to(...); // interpolates from the current values to the targets
Tween.from(...); // interpolates from the given values to the current ones
Tween.set(...); // apply the target values without animation (useful with a delay)
Tween.call(...); // calls a method (useful with a delay)
// Current options are:
yourTween.delay(0.5f);
yourTween.repeat(2, 0.5f);
yourTween.repeatYoyo(2, 0.5f);
yourTween.pause();
yourTween.resume();
yourTween.setCallback(callback);
yourTween.setCallbackTriggers(flags);
yourTween.setUserData(obj);
// You can of course chain everything:
Tween.to(...).delay(1.0f).repeat(2, 0.5f).start();
// Moreover, slow-motion, fast-motion and reverse play is easy,
// you just need to change the speed of the update:
yourTween.update(delta * speed);
Of course, no tween engine would be complete without providing a way to build powerful sequences :)
Timeline.createSequence()
// First, set all objects to their initial positions
.push(Tween.set(...))
.push(Tween.set(...))
.push(Tween.set(...))
// Wait 1s
.pushPause(1.0f)
// Move the objects around, one after the other
.push(Tween.to(...))
.push(Tween.to(...))
.push(Tween.to(...))
// Then, move the objects around at the same time
.beginParallel()
.push(Tween.to(...))
.push(Tween.to(...))
.push(Tween.to(...))
.end()
// And repeat the whole sequence 2 times
// with a 0.5s pause between each iteration
.repeatYoyo(2, 0.5f)
// Let's go!
.start();
I hope you're convinced :) There are a lot of people already using the engine in their games or for android UI animation.
You can do the tween animation without the ImageView coming from an xml file, but it does need to actually be a View in your view hierarchy.
The drawing you're doing in your Canvas is opaque to the view hierarchy. I think you have two options:
Overlay an ImageView on top of your custom view and animate that using tween animations.
Use Canvas draw routines to animate your explosion.
I'd say that using Canvas routines, along with their maxtrix transformations, makes sense given that you probably already have an animation thread in your app.
You could draw and animate your explosion in Adobe Flash, export the sprite images using swfSheet or similar and then use Frame animation
If you are creating a game, you can use Vectors(Mathematical) to update the position of your images and then draw then with getX and getY. You should iterate and update this vector in some direction through some speed.
Those Vectors are not native (Game APIs has).
You need have a loop to iterate and update the position, then redraw.
For games, it's not a good idea to have components such ImageViews to make animations. They need the system to calc all the layout again and again over time.