Analysis of animation animation

Source: Internet
Author: User

Animation in the view of the package, we through the principle of animation can also know, animation left the view there is no effect, why say so?

The animation principle of animation

Let's look at a simple animation animation, Alphaanimation:

 Public  class alphaanimation extends Animation {    Private floatMfromalpha;Private floatMtoalpha;/** * Constructor used when an alphaanimation are loaded from a resource. * * @param Context Application context to use * @param attrs Attribute set from which to read VA Lues * *     Public alphaanimation(context context, AttributeSet attrs) {Super(context, attrs);        TypedArray a = Context.obtainstyledattributes (Attrs, com.android.internal.r.styleable.alphaanimation); Mfromalpha = A.getfloat (Com.android.internal.r.styleable.alphaanimation_fromalpha,1.0f); Mtoalpha = A.getfloat (Com.android.internal.r.styleable.alphaanimation_toalpha,1.0f);    A.recycle (); }/** * Constructor to use when building a alphaanimation from code * * @param Fromalpha starting     Alpha value for the animation, where 1.0 means * Fully opaque, 0.0 means fully transparent.     * @param toalpha ending alpha value for the animation. */     Public alphaanimation(floatFromalpha,floatToalpha) {mfromalpha = Fromalpha;    Mtoalpha = Toalpha; }/** * Changes the Alpha property of the supplied {@link transformation} * /    @Override    protected void applytransformation(floatInterpolatedtime, transformation t) {Final floatAlpha = Mfromalpha;    T.setalpha (Alpha + ((mtoalpha-alpha) * interpolatedtime)); }@Override     Public Boolean Willchangetransformationmatrix() {return false; }@Override     Public Boolean Willchangebounds() {return false; }/** * @hide * *    @Override     Public Boolean Hasalpha() {return true; }}

The code is very simple and its core code is

@OverrideprotectedvoidapplyTransformation(float interpolatedTime, Transformation t) {    finalfloat alpha = mFromAlpha;    t.setAlpha(alpha + ((mToAlpha - alpha) * interpolatedTime));}

By constantly giving the variable alpha value through Interpolatedtime, we can see an animated view that changes transparency! So the question comes, who is calling the Applytransformation method? Follow-up code see

publicbooleangetTransformation(long currentTime, Transformation outTransformation){}

In the applytransformation made a call, do not think, Gettransformation is definitely in the view of the Startanimation trigger!
Isn't it?

startAnimation(Animation animation) {        animation.setStartTime(Animation.START_ON_FIRST_FRAME);        setAnimation(animation);        invalidateParentCaches();        invalidate(true);    }

The view is redrawn here, so the view's Boolean draw (Canvas canvas, viewgroup parent, long Drawingtime) method will be executed, why draw instead of OnDraw, we look at a note

    /**     isbytoeach child view draw itself.     isandisnottoor     tofromelse other than ViewGroup.drawChild().     */

In the Draw () method, there are

final Animation a = getAnimation();ifnull) {    more = drawAnimation(parent, drawingTime, a, scalingRequired);    concatMatrix = a.willChangeTransformationMatrix();    if (concatMatrix) {        mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;    }    parent.getChildTransformation();}

So animation to Gettransformation to execute, this is the animation implementation principle.

But it doesn't seem to mean much to us, indeed, because we're not animation's creator, we just need to know how to use it and warm up the current face!

Use of animation

Animation is very simple to use, Android has been defined for us four kinds of animation animation effects alphaanimation, Rotateanimation, Scaleanimation, Translateanimation
Take Alphaanimation as a quick example of how to use it:

AlphaAnimation animation = new AlphaAnimation(1,0);animation.setDuration(2000;animation.setRepeat(2;view.startAnimation(animation);

OK, a view of the animation is implemented, the other three animation effects are similar, very simple! Instead of repeating it here, let's focus on how to define a animation effect yourself, or illustrate it in one case.

Achieve a button to enlarge the animation effect, here to enlarge can not appear stretching, if can stretch that is meaningless, because the system has been defined for us a, scaleanimation can be achieved;

The first step, inherit the animation,
The second step, overriding the Applytransformation method
The third step, no!

So the code is as follows:

/** * Created by Moon.zhong on 2015/4/23. * * Public  class scaleanimation extends Animation {    PrivateView Mtarget;Private intMoriginwidth;Private intMtargetwidth;PrivateViewgroup.layoutparams Mparams; Public scaleanimation(intMtargetwidth, View target) { This. Mtarget = target; This. Moriginwidth = Mtarget.getmeasuredwidth (); This. mtargetwidth = Mtargetwidth;if(Moriginwidth = =0) {Mtarget.getviewtreeobserver (). Addonpredrawlistener (NewViewtreeobserver.onpredrawlistener () {@Override                 Public Boolean Onpredraw() {mtarget.getviewtreeobserver (). Removeonpredrawlistener ( This); Moriginwidth = Mtarget.getmeasuredwidth ();return false;        }            });    } mparams = Target.getlayoutparams (); }@Override    protected void applytransformation(floatInterpolatedtime, transformation t) {/*interpolatedtime * Change range * 0~1 * */Mparams.width = (int) (Moriginwidth + (mtargetwidth-moriginwidth) *interpolatedtime);    Mtarget.setlayoutparams (Mparams); }}

Using code:

    publicvoidstartAnimation(View view){        finalfloat density =  getResources().getDisplayMetrics().density;        int width = (int) (300 * density);        new ScaleAnimation(width,mTargetView) ;        animation.setDuration(2000);        mTargetView.startAnimation(animation);    }


Another stretch of the contrast:

Summarize:

Overall is very simple, here I just defined a simple animation, the same definition of a more complex animation is the same operation, just applytransformation inside the code to write a little more.
The main points of this blog's knowledge are:
1, the use of animation scene, for the view;
2, the use of system animation

AlphaAnimation animation = new AlphaAnimation(1,0);animation.setDuration(2000;animation.setRepeat(2;View.startAnimation(animation);

This form, of course, also reads the form of XML, which is not mentioned here
3. Custom Animation
Overriding the Applytransformation method

Demo Source
http://download.csdn.net/detail/jxxfzgy/8634819

Analysis of animation animation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.