Android custom --02 view animation

2. View the animation

2.1 view animation label

2.1.1 Overview

  • Android view the animation of five types consisting of:

    • Alpha : gradient transparency animation.
    • Scale : Tapered telescopic animation.
    • Translate : position of the moving picture conversion animation.
    • Rotate : rotate screen transition animation effects.
    • the SET : custom animation set.
  • For example:

    • scale label definition XML animation file:

      <?xml version="1.0" encoding="utf-8"?>
      <scale xmlns:android="http://schemas.android.com/apk/res/android"
             android:fromXScale="0.0"
             android:toXScale="1.4"
             android:fromYScale="0.0"
             android:toYScale="1.4"
             android:duration="700"/>
      
    • Storage location: res/anim; access:R.anim.XXX

    • Can also be stored in: res/drawable; access:R.drawable.XXX

2.1.2 scale label

  • fomXScale : animation starting control scaling relative to itself, a floating point value in the X-axis direction.
  • toXScale : At the end of the animation, the scaling control relative to itself, a floating point value in the X-axis direction.
  • fomYScale : animation starting control scaling relative to itself, a floating point value in the Y-axis direction.
  • toYScale : At the end of the animation, the scaling control relative to itself, a floating point value in the Y-axis direction.
  • pivotX: Scale X coordinate of the starting point, may be values , percentages , percentages p three styles.
  • pivotY : Y coordinate of the start point scale.

2.1.3 Animation inherited property

  • DURATION : to set the duration of time to complete the animation, in milliseconds.
  • fillAfter : If set to true, then at the end of the animation controls, will hold at the end of the animation.
  • fillBefore : If set to true, then at the end of the animation controls, to restore to the initial state.
  • fillEnable : fillBefore with the same effect.
  • repeatCount : specified number of times for repeating the animation, when the value of Infinite, represent an infinite loop.
  • REPEATMODE : duplicate for setting the type, with the reverse and restart two values. To be used with repeatCount.
  • interpolator : used to set the interpolator, in fact, specify animation effects, such as bouncing effects.

2.1.4 alpha tag

  • fomAlpha : transparency, at the start of the animation in the range from 0.0 to 1 .0,0.0 represents transparent and 1.0 is completely opaque.
  • toAlpha : transparency, at the end of the animation in the range from 0.0 to 1.0, where 0.0 represents a completely transparent and 1.0 is completely opaque.

2.1.5 rotate tag

  • fromDegrees : angular position of the animation starts rotating clockwise value representative of the degree, negative values indicate degrees counterclockwise.
  • toDegrees : rotation to angular position at the end of the animation, the representative value of the degrees clockwise, negative values indicate degrees counterclockwise.
  • pivotx : X coordinate of the center point of rotation, rotation center is the default controls coordinate origin. It can be a value , percentage , percentage, p, three styles.
  • pivotY : rotation center point Y coordinate.

2.1.6 translate labels

  • fromXDelta : X coordinate of the starting point, it may be a numerical value , a percentage , the percentage p three styles.
  • fromYDelta : Y coordinate of the starting point.
  • toXDelta : End point X coordinate axis.
  • toYDelta : Y coordinate of the end point.

2.1.7 set label

  • tag set is a container class label is used to define animation set.
  • Note : Setting repeateCount attribute set tag is invalid, it must have a direct effect on a separate set of each movie.

2.2 view the animation code implementation

2.2.1 Overview

  • And the corresponding tag class as follows:
label class
scale ScaleAnimation
alpha AlphaAnimation
rotate RotateAnimation
translate TranslateAnimation
set AnimationSet
  • Animation tag correspondence between class attributes and methods:
Tag attributes method
duration setDuration(long)
fiIIAfter setFillAfter(boolean)
fillBefore setFillBefore(boolean)
fillEnabled setFillEnabled(boolean)
repeatCount setRepeatCount(int)
repeatMode setRepeatMode(int)
interpolator setlnterpolator(lnterpolator)

2.2.2 ScaleAnimation

ScaleAnimation(Context context , AttributeSet attrs)
ScaleAnimation(float fromX , float toX, float fromY , float toY)
ScaleAnimation(float fromX , float toX , float fromY , float toY , float pivotX,
float pivotY)
ScaleAnimation(float fromX , float toX, float fromY , float toY, intpivotXType,
float pivotXValue , int pivotYType , float pivotYValue)
  • pivotXType:

    • Animation.ABSOLUTE
    • Animation.RELATIVE_TO_SELF
    • Animation.RELATIVE_TO_PARENT

2.2.3 AlphaAnimation

AlphaAnimation(Context context , AttributeSet attrs)
AlphaAnimation(float fromAlpha , float toAlpha)

2.2.4 RotateAnimation

RotateAnimation(Context context , AttributeSet attrs)
RotateAnimation(float fromDegrees , float toDegrees)
RotateAnimation(float fromDegrees , float toDegrees , float pivotX , float pivotY)
RotateAnimation(float fromDegrees , float toDegrees , int pivotXType , float pivotXValue , int pivotYType , float pivotYValue)

2.2.5 TranslateAnimation

TranslateAnimation(Context context , AttributeSet attrs)
TranslateAnimation(float fromXDelta , float toXDelta , float fromYDelta , float toYDelta)
TranslateAnimation(int fromXType , float fromXValue , int toXType , float toXValue , int fromYType , float fromYValue , int toYType , float toYValue)

2.2.6 AnimationSet

AnimationSet(Context context , AttributeSet attrs)
AnimationSet(boolean shareInterpolator)
  • There are two parameter values ​​shareInterpolator: true and false.

    • true: used to define a class AnimationSet interpolator in which the following common to all animation interpolator.
    • false: The following represents their respective interpolator animation definition.
  • Increasing function of animation:

public void addAnimation(Animation a)

2.2.7 Animation

  • Some functions:

    void cancel()	//取消动画。
    void reset()	//将控件重置到动画开始前状态。
    void setAnimationListener(Animation.AnimationListener listener)//设置动画监昕
        
    //Animation.AnimationListener中的回调函数:
    abstract void onAnimationEnd(Animation animation)
    abstract void onAnimationRepeat(Animation animation)
    abstract void onAnimationStart(Animation animation)
    

2.3 Introduction difference

  • Issues related to the rate of change of the animation is determined by the Interpolator class (interpolator) it is.

  • Interpolator is an interface, you can change the rate by implementing this interface to customize the animation.

  • The system offers several classes have achieved interpolator:

    Interpolator class Resource ID
    AccelerateDecelerateinterpolator @android:anim/accelerate_decelerate_interpolator
    AccelrateInterpolator @android:anim/accelerate_interpolator
    AnticipateInterpolator @android:anim/anticipate_interpolator
    AnticipateOvershootInterpolator @android:anim/anticipate_overshoot_interpolator
    BounceInterpolator @android:anim/bounce_interpolator
    Cychleinterpoltor @android:anim/cycle_interpolator
    DecelerateInterpolator @android:anim/decelerate_interpolator
    LinearInterpolator @android:anim/linear_interpolator
    OvershootInterpolator @android:anim/overshoot_interpolator
  • It cited the difference in the XML file:

    android:interpolator="@android:anim/accelerate_interpolator"
    
  • By setlnterpolator () function to set the interpolator.

2.3.1 Description difference

  • AccelerateDeceleratelnterpolator: acceleration and deceleration interpolator.

  • Acceleratelnterpolator: acceleration interpolator.

  • Deceleratelnterpolator: deceleration interpolator.

  • Linearlnterpolator: linear interpolation filter, also known as uniform accelerator.

  • Bouncelnterpolator: bounce interpolator.

  • Anticipatelnterpolator: initial offset interpolator.

    /**
     * 张力值:默认值为2,值越大,初始的偏移量越大,而且速度越快。
     */
    
    //XML属性
    android:tension
    //函数
    public Anticipateinterpolator(float tension)
    
  • Overshootlnterpolator: End offset interpolator.

    /**
     * 张力值:默认值为2,值越大,结束的偏移量越大,而且速度越快。
     */
    
    //XML属性
    android:tension
    //函数
    public Overshootinterpolator(float tension)
    
  • AnticipateOvershootlnterpolator: Anticipatelnterpolator and Overshootlnterpolator fit, i.e., the offset distance ahead of the beginning of the animation, a distance rearwardly offset when the movie ends.

    /**
     * tension张力值:默认值为2,值越大,起始和结束时的偏移量越大,而且速度越快。
     * extraTension额外张力值:默认值为1.5。
     * T = tension * extraTension
     */
    //XML属性
    android:tension
    android:extraTension
    //函数
    public AnticipateOvershootlnterpolator(float tension)
    public AnticipateOvershootlnterpolator(float tension, float extraTension)
    
  • Cyclelnterpolator: is a cyclic interpolator indicating a specific number of animation loop, the rate of change along the sinusoid.

    public Cycleinterpolator(float cycles)
    
  • skill:

    //延迟各个动画的开始时间,使各个动画可以相互间隔。
    Animation.setStartOffset(int time);
    

2.4 frame by frame animation

  • That frame animation (Frame Animation), then one is a playback picture.

2.4.1 XML achieve

  • ** 1 defines the XML animation file: ** defined res/drawableunder

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
                    android:oneshot=["true" | "false"]>
        <item
              android:drawable="@[package:]drawable/drawable_resource_name"
              android:duration="integer"
    />
    
    • Element is required, and must as the root element, may comprise one or more elements.
    • oneshot: true: the animation will only execute; false: has been circulating.
    • Element represents an animation, android: drawable specified picture frame animation of this resource corresponding, android: duration represents the duration of the frame animation, is an integer unit ms.
  • 2. Set ImageView

    • By android: src achieve, you can also android: background achieve.
  • 3.AnimationDrawable start animation

    ImageView image = (ImageView)findViewById(R.id.frame_image);
    AnimationDrawable anim = (AnimationDrawable)image.getDrawable();
    anim.start();
    
    • By android: src animate resources when removed way image.getDrawable ().
    • By android: background animate resources, the way out is image.getBackground ().
  • AnimationDrawable categories:

    • Frame animation need to be supported AnimationDrawable class, which is an indirect subclass of Drawable.
    • void start (): start playing frame by frame animation.
    • void stop (): Stop playing frame by frame animation.
    • int getDuration (int index): to give duration of the frame to the specified index.
    • Drawable getFrame (int index): to give Drawable target frame corresponding to the specified index.
    • int getNumberOfFrames (): get all the current frame number AnimationDrawable.
    • boolean isRunning (): determine whether AnimationDrawable currently being played.
    • void setOneShot (boolean oneShot): Set AnimationDrawable is performed once or loop.
    • boolean isOneShot (): determining a current state AnimationDrawable performed.
    • void addFrame (Drawable frame, int duration): Add 1 to AnimationDrawable, and set the duration.

2.4.2 code implementation

  • For example:

    ImageView image = (ImageView)findViewById(R.id.frame_image);
    final AnimationDrawable anim = new AnimationDrawable();
    for(int i=1;i<=14;i++){
        int id = getResources().getIdentifier("list_icon_gif_playing"+i,"drawable",getPackageName());
    Drawable drawable  = getResources().getDrawable(id);
    anim.addFrame(drawable,60);
    anim.setOneShot(false);
    image.setBackgroundDrawable(anim);
    anim.start();
    
  • Function Description:

    int getidentifier(String name,String defType,String defPackage)
    
    • String name: To find the name of the resource ID of the resource.
    • String defType: file type resource is located. [Eg.drawable / string / array]
    • String defPackage: application package name.

Reference material

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

Guess you like

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