Tomorrow Is Another Day!
Related articles
- I. Work flow of View
- 2. Event distribution mechanism of View
- 3. View common API
- 4. Animation effect of View
Summary of this article:
- Basic usage means matters
- The use of composite animation
preface
Android animation is divided into View animation and property animation, and View animation includes tween animation and frame by frame animation. Since View animation is rarely used and relatively easy to use, I won’t talk about it here.
1. Basic use mode
1.1 Method 1:ViewPropertyAnimator
The sample code
View.animate ().translationx (utils.dptopixel (200)).translationy (100).rotation(180).alpha(0.5f).setStartDelay(1000) .start();Copy the code
This method is suitable for the system definition attributes such as translationX, translationY etc. To use the property animation quickly, if you need some way to take two custom types.
1.2 Method 2:ValueAnimator and ObjectAnimator
Principle:
- ValueAnimator. OfInt ()/float/object/PropertyValuesHolder (digital definition animation interval)
- Interpolator (Returns the current animation completion progress fraction over time)
- Estimator (returns a specific value)
- The callback onAnimationUpdate
- ObjectAnimator. OfInt ()/float/object/ofPropertyValuesHolder (object definition animation and digital range)
- Interpolator (returns the current animation completion progress fraction)
- Estimator (returns a specific value)
- Concatenated “set”+ incoming PropertyName reflects the callback to the set method.
- Matters needing attention:
- The type passed in by the construct is the same as the type returned by the estimator.
- When the incoming type is a special type, ofObject is used and a custom TypeEvaluator is required.
- ObjectAnimator needs to provide get(optional)/set(mandatory) methods.
The sample code
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 400);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int curValue = (int) animation.getAnimatedValue();
Log.d(TAG, "onAnimationUpdate: "+ curValue); }}); valueAnimator.setDuration(2000); valueAnimator.setEvaluator(new IntEvaluator()); valueAnimator.setInterpolator(new LinearInterpolator()); valueAnimator.start(); ObjectAnimator objectAnimator = ObjectAnimator.ofInt("Control object"."Attribute name",0,400);
objectAnimator.setDuration(2000);
objectAnimator.setEvaluator(new IntEvaluator());
objectAnimator.setInterpolator(new LinearInterpolator());
objectAnimator.start();
Copy the code
Second, the use of composite animation
2.1 PropertyValuesHolder with Keyframe
When the same control multiple properties, can achieve multiple animation playback, can also take the following way.
- PropertyValuesHolder. OfInt ()/float/object/Keyframe: equivalent to encapsulate save attribute names and values of parameters.
- Keyframe.ofint ()/float/object: Implement the rate (value) of each frame, at least two frames must be set.
2.2 AnimatorSet and AnimatorSet. Builder
-
PlayTogether and playSequentially. Only responsible for activating control animation on a regular basis.
-
Play. With. After. Before.
-
When a function set by an AnimatorSet conflicts with that set by a single animation, the AnimatorSet Settings prevail.
- The only exception is setStartDelay.
AnimatorSet activation delay = animatorSet. startDelay(extension of AnimatorSet activation time)+ the first animation. startDelay
Three. Layout animation related
- LayoutAnimation and gridLayoutAnimation: introduced in API 1
- AnimateLayoutChanges (using system default animations) and LayoutTransaction(custom animations) : introduced in API 11
Because my technology is limited, if there is a mistake, trouble everyone to put forward to me, I am very grateful, we learn progress together.
Reference links:
- hencoder.com/ui-1-3/
- hencoder.com/ui-1-4/
- Blog.csdn.net/harvic88092…
- Blog.csdn.net/harvic88092…