Android custom control --03 property animation

3. Properties animation

The basic use of 3.1 ValueAnimator

3.1.1 Overview

  • View animation: do animation for the specified control.
  • Property animation: by changing a property value of the control to do the animation.

3.1.2 ValueAnimator simple use of

  • Preliminary use ValueAnimator:

    ValueAnimator animator = ValueAnimator.ofInt(0,400);
    animator.setDuration(1000);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
    	public void onAnimationUpdate(ValueAnimator animation){
            int curValue = (Integer)animation.getAnimatedValue();
            Log.d("ValueAnimator","curValue:"+curValue);
        }
    });
    animator.start();
    
  • ValueAnimator responsible only to the specified value range of animation operations.

  • Need to monitor process operation, and then perform operations on the animation controls.

  • Note :

    • Tween, click the area: the original area.
    • Property animation hit area: the new area.
  • Supplementary :

    • By () function layout changes the position of the control.
    • layout () function when changing the control position is permanent , that is by changing the controls left, top, right, bottom coordinates of the four points to change the coordinates of the location.

3.1.3 Common Functions

  • ** oflnt () and ofFloat () ** function:

    public static ValueAnimatorofInt(int...values)
    public static ValueAnimatorofFloat(float...values)
    
    • Parameter types are variable-length parameters.
    • Pass in a list of values ​​represents a change range of animation.
  • SetDuration ValueAnimator (Long DURATION) : When you set the animation length, unit ms.

  • GetAnimatedValue Object () : Gets the value ValueAnimator moving point in the current exercise.

  • Start void () : Start the animation.

  • setRepeatCount void (int value) : the number of repetitions, to ValueAnimation.INFINITE indicates an infinite loop, set to 0 is not circulated.

  • setRepeatMode void (int value) : Set loop mode, value Value: ValueAnimation.RESTART and ValueAnimation.REVERSE .

  • the Cancel void () : cancel movie.

  • Note : the number of repetitions is INFINITE (infinite loop) animation, when the Activity ends, you must call the cancel () function to cancel the animation, or animation will infinite loop, resulting in a View can not be released, further cause the entire Activity can not be released, and ultimately lead to memory leakage.

  • Add and remove listeners:

    • Add method:

      public void addUpdateListener(AnimatorUpdateListener listener)//监听器一
      public void addListener(AnimatorListener listener)//监听器二
      
    • Listeners: monitor real-time changes in the value of the animation process

      public static interface AnimatorUpdateListener {
          void onAnimationUpdate(ValueAnimator animation);
      }
      
    • Listener II: 4 monitor the state of the animation changes

      public static interface AnimatorListener {
          void onAnimationStart(Animator animation);
          void onAnimationEnd(Animator animation);
          void onAnimationCancel(Animator animation);
          void onAnimationRepeat(Animator animation);
      }
      
    • Remove the listener:

      //移除AnimatorUpdate Listener
      void removeUpdateListener(AnimatorUpdateListener listener);
      void removeAllUpdateListeners();
      
      //移除AnimatorListener
      void removeListener(AnimatorListener listener);
      void removeAllListeners();
      
  • Other less common functions:

    public void setStartDelay(long startDelay)//延迟多久开始,单位ms
    public ValueAnimator clone()//完全克隆一个ValueAnimator实例,包括它所有的设置以及所有对监听器代码的处理
    

3.2 custom interpolation with Evaluator

  • View Animation , only allows setlnterpolator () function to set the interpolator.
  • View Animator can not only set up the interpolation, you can also set the Evaluator.

3.2.1 Custom interpolator

  • Linearlnterpolator example:

    public class Linearinterpolator implements Interpolator{
        public Linearinterpolator(){}
        public Linearinterpolator(Context context , AttributeSet attrs) {
            public float getinterpolation(float input) {
                return input;
            }
        }
        ...
    }
    
    public interface Interpolator extends Timeinterpolator {}
    
    public interface Timeinterpolator{
        float getInterpolation(float input);
    }
    
    • INPUT : the Float type, in the range 0 to 1. Represents the current animation progress, when set to 0 indicates the beginning of the animation, the animation represents the end of an animation showing the intermediate position to take a take 0.5.
    • Return value : Indicates the current actual schedule you want to display. The value may be more than one, may be less than zero. Represents more than one already exceeds the target value is less than 0 indicates less than the start position.
  • Use:

    animator.setinterpolator(new MyInterpolator());
    

3.2.2 Evaluator

  • Process to obtain a value corresponding to the current animation AnimatorUpdateListener from the interval of values ​​defined animations:

    • 1. The interval of values ​​defined Animation: ofInt (400)
    • The difference is: Returns the value of the current progress
    • Evaluator: calculating a current value according to the progress value
    • Listener Returns: In the AnimatorUpdateListener
  • Evaluator fact, a converter that can convert to a decimal value of progress corresponding to the position.

  • For the Evaluator, the default Evaluator oflnt () function is IntEvaluator, and the default Evaluator ofFloat () function is FloatEvaluator.

  • Use:

    animator.setEvaluator(new MyEvaluator());
    
  • ArgbEvaluator:

    • Used to implement the transition color value conversion.

      animator.setEvaluator(new ArgbEvaluator());
      
    • Define the color range to be used ** ValueAnimator.oflnt () ** function, and must include the color A, R, G, B values ​​of four.

      ValueAnimator animator = ValueAnimator.ofInt(0xffffff00,0xff0000ff);
      

3.3 ofObject

3.3.1 Overview

  • ValueAnimator have a function ofObject (), can pass any type of variable.

  • Function definition:

    public static ValueAnimator ofObject(TypeEvaluator evaluator, Object...values)
    
    • A parameter: custom Evaluator.
    • Two parameters: a variable length parameter type Object.

3.4 ObjectAnimator

3.4.1 Overview

  • Google developers on the basis of ValueAnimator derive a class ObjectAnimator.

  • ValueAnimator function that can be used may be in normal use in ObjectAnimator.

  • ObjectAnimator function:

    public static ObjectAnimator ofFloat(Object target,String propertyName,float...values)
        
    //example
    ObjectAnimator animator= ObjectAnimator.ofFloat(tv,"rotation",0,180,0);
    animator.setDuration(2000);
    animator.start();
    
    • A parameter: This control is used to specify the animation operation.
    • Two parameters: this attribute is used to specify the animation operation.
    • Three parameters: a variable length parameter that specifies how the property value changes.
  • set functions:

    • The second parameter ofFloat ObjectAnimator not function according to the control to change the XML attributes, but rather by specifying attributes corresponding to the set function to change.

    • View of several groups set on animation:

      //1.透明度:alpha
      public void setAlpha(float alpha)
      //2.旋转度数:rotation、rotationX、rotationY
      public void setRotation(float rotation)
      public void setRotationX(float rotationX)
      public void setRotationY(float rotationY)
      //3.平移:translationX 、translationY
      public void setTranslationX(float translationX)
      public void setTranslationY(float translationY)
      //4.缩放:scaleX 、scaleY
      public void setScaleX(float scaleX)
      public void setScaleY(float scaleY)
      

3.4.2 Custom ObjectAnimator property

  • ObjectAnimator three constructors:

    public static ObjectAnimator ofFloat(Object target,String propertyName,float...values)
    public static ObjectAnimator ofInt(Object target,String propertyName,int...values)
    public static ObjectAnimator ofObject(Object target,String propertyName,TypeEvaluator evaluator,Object...values)
    
  • Note : if and only if the animation is only a transition value , the system will call the corresponding property get function to get the initial value of the animation. When the get function does not exist, the default value of the animation parameter type as an initial value; default values can not be obtained when the type of animation parameters, it will directly collapse stains.

3.4.3 Common Functions

  • Listeners: real-time value of listeners animation changes

    public static interface AnimatorUpdateListener{
        void onAnimationUpdate(ValueAnimator animation);
    }
    
    //添加方法为:
    public void addUpdateListener(AnimatorUpdateListener listener)
    
  • Listener II: 4 states when listening animation changes

    public static interface AnimatorListener{
        void onAnimationStart(Animator animation);
        void onAnimationEnd(Animator animation);
        void onAnimationCancel(Animator animation);
        void onAnimationRepeat(Animator animation);
    }
    
    //添加方法为: 
    public void addListener(AnimatorListener listener)
    
  • Associated with the interpolation function and Evaluator:

    //设置插值器
    public void setinterpolator(Timeinterpolator value)
    
    //设置Evaluator
    public void setEvaluator(TypeEvaluator value)
    

3.5 AnimatorSet

  • For combining animation.

3.5.1 playSequentially () and playTogether () function

  • AnimatorSet of ValueAnimator and ObjectAnimator is suitable, but generally not used a combination of animation ValueAnimator.

  • playSequentially () represents all the animations play one, playTogether () indicates the start with all the animation.

  • playSequentially () function:

    public void playSequentially(Animator...items);
    public void playSequentially(List<Animator> items);
    
  • playTogether () function:

    public void playTogether(Animator...items);
    public void playTogether(Collection<Animator> items);
    
  • playTogether () and playSequentially () function at the beginning of the animation, the animation just activate each control, as to whether each control their own animation delay, whether an infinite loop control itself only with the movie settings, and playTogether ( ) and playSequentially () function has nothing to do, they are only responsible to the activation time animation. playSequentially () function only after the animation under control on a finished animation, will activate a control.

3.5.2 AnimatorSet.Builde

  • AnimatorSet.Builde function:

    public Builder play(Animator anim)//表示要播放哪个动画
    public Builder with(Animator anim)//和前面的动画一起执行
    public Builder before(Animator anim)//先执行这个动画,再执行前面的动画
    public Builder after(Animator anim)//在执行前面的动画后才执行该动画
    public Builder after(long delay)//延迟n毫秒之后执行动画
    

3.5.3 AnimatorSet listeners

  • Add a listener:

    public static interface AnimatorListener{
    //当AnimatorSet开始时调用
    void onAnimationStart(Animator animation);
    //当AnimatorSet结束时调用
    void onAnimationEnd(Animator animation);
    //当AnimatorSet被取消时调用
    void onAnimationCancel(Animator animation);
    //当AnimatorSet重复时调用。由于AnimatorSet没有设置重复的函数,所以这个函数永远不会被调用
    void onAnimationRepeat(Animator animation);
        
    //添加方法
    public void addListener(AnimatorListener listener);
    

3.5.4 Common Functions

//设置单次动画时长
public AnimatorSet setDuration(long duration)
//设置插值器
public void setinterpolator(TimeInterpolator interpolator)
//设置ObjectAnimator动画目标控件
public void setTarget(Object target)
  • In AnimatorSet not set, then set ObjecAnimator prevail in.

  • After setting the AnimatorSet, set in ObjectAnimator will be invalid.

  • setStartDelay(long startDelay)函数

    //设置延时开始动画时长
    public void setStartDelay(long startDelay)
    
    • setStartDelay () function does not delay coverage for a single movie, and only targeted AnimatorSet extended activation time, setStartDelay single set of animation () function still works for a single movie.

3.6 Animator animation XML implementation

3.6.1 animator tag

<animator
          android:duration="int"
          android:valueFrom="float | int | color "
          android:valueTo="float | int | color "
          android:startOffset="int"
          android:repeatCount="int"
          android:repeatMode=["repeat" | "reverse"]
          android:valueType=["intType" | "floatType"]
          android:interpolator=["@android:interpolator/XXX"] />   
  • The method of program files added:

    ValueAnimator valueAnimator = (ValueAnimator)Animatorinflater.loadAnimator(MyActivity.this,R.animator.animator);
    valueAnimator.start();
    

3.6.2 objectAnimator label

<objectAnimator
          android:duration="string"
          android:duration="int"
          android:valueFrom="float | int | color "
          android:valueTo="float | int | color "
          android:startOffset="int"
          android:repeatCount="int"
          android:repeatMode=["repeat" | "reverse"]
          android:valueType=["intType" | "floatType"]
          android:interpolator=["@android:interpolator/XXX"] />   

Reference material

  • "Android custom control development of entry and actual combat."
Published 64 original articles · won praise 65 · views 8800

Guess you like

Origin blog.csdn.net/qq_33334951/article/details/103735462