Custom Control Trilogy Animation chapter (iii)--code Generation Alpha, scale, translate, rotate, set and interpolation animation _5, andriod development

Source: Internet
Author: User
Tags documentation xml attribute

I believe that the power of faith, as long as every day progress, one day, will be different. Go to Jaca later, found that there are really a lot of knowledge to add, no longer like C + +, as long as in-depth study is good, there is no in-depth, but requires a wide range of knowledge. After the Android knowledge to complete, and then look at Android books, and finally in-depth study of Java, two years should be urgent, efforts.


Do not climb the high mountain, do not know the height of the sky, not near the Deep Brook, not knowing the thickness of the Earth

Related articles:

"Android custom Control Trilogy Article index"

I. Overview

The first two, I told you to use XML to define animation and interpolation, but in the code, we often are dynamically generated animation, so, this article will tell you how to generate dynamic animation and interpolation code.

First of all, simple to write the corresponding categories of tags to facilitate understanding:

Scale--scaleanimation alpha--alphaanimation rotate--rotateanimation translate--translateanimation set--animation Set

Second, animation public class

The Official SDK explanation page is: "Animation"

As we mentioned in the first article, the animation class is the base class for all animations (scale, alpha, translate, rotate), and its label and corresponding functions are:

Android:duration setduration (long) animation duration, in milliseconds android:fillafter setfillafter (bool EAN) If set to true, the state at the end of the animation is persisted Android:fillbefore Setfillbefore (Boolean) If set to true, when the control animation ends, revert to the state before the animation was started The Android:fillenabled setfillenabled (Boolean) is the same as the Android:fillbefore effect by restoring the control to its initialization state at the end of the animation Android:repeatcoun T Setrepeatcount (int) Repeat number android:repeatmode setrepeatmode (int) repeating type, with reverse and restart two values, value is restart or REVERSE, you must work with repeatcount to see the effect. Because the meaning here is the type of repetition, that is, the action at playback. Android:interpolator Setinterpolator (interpolator) set interpolation, in fact, is the specified action effect, such as bounce effect

In the first "Animation animation" (i)--alpha, scale, translate, rotate, set of XML properties and usage we have explained the specific features of each label, and here is no longer detailed, the use of the method will be used in the following tabs. Third, scaleanimation

This is the scale tag corresponding class, the Official SDK page is: "Scaleanimation"

In the scale tag, we mentioned its own properties, which are listed below:

Android:fromxscale starting in the X direction relative to its own scaling, floating point value, such as 1.0 on behalf of their own no change, 0.5 for the beginning of the reduction of one time, 2.0 for a magnification; Android:toxscale the end of the X-direction relative to its own scale, floating Point value Android:fromyscale The starting Y-direction relative to its own scaling, floating-point value, android:toyscale the end of the Y-direction relative to its own scaling, floating-point values; Android:pivotx scaling Start x axis coordinates, which can be numeric, percentages, percentages p three styles, such as 50, 50%, 50%p, when numeric, represents the top left corner of the current view, with 50px at the origin, as the starting zoom point, or 50% for the upper-left corner of the current control with its width of 50% As the starting point; if it is 50%p, it means that the current upper-left corner plus 50% of the width of the parent control is the starting point x-axis coordinates. (concrete meaning, later will illustrate) Android:pivoty scaling start y axis coordinates, the value and the meaning is the same as Android:pivotx. In the code, Scaleanimation has the following constructors:

Scaleanimation (context context, AttributeSet attrs) loads animations from XML files and is not used for scaleanimation (float FromX, float toX, float FromY, float ToY) scaleanimation (float FromX, float toX, float fromY, float toY, float pivotx, float pivoty) scaleanimation (Floa T FromX, float ToX, float fromY, float toY, int pivotxtype, float pivotxvalue, int pivotytype, float pivotyvalue)

The first constructor is to load the animation from the local XML file, which is basically not used, and we mainly look at the following three constructors.

In the label attribute Android:pivotx, there are three kinds of values, numbers, percentages, percent p, which are embodied in the constructor, which is the pivotxtype of the last constructor, which has three values, Animation.absolute, Animation.relative_to_self and Animation.relative_to_parent;

These three constructors are not very difficult, they are no longer detailed, for example:

The XML code for the example scale in the first article is:

<?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="
    android: toyscale= "1.4"
    android:pivotx= "android:pivoty=" "
    android:duration=" "/>"

The corresponding Code construction code is:

Scaleanim = new Scaleanimation (0.0f,1.4f,0.0f,1.4f,animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);
Scaleanim.setduration (700);
When the control is used, the same is used:

Tv.startanimation (Scaleanim);
Four, alphaanimation this is the Alpha label on the class, the official SDK document address is: "Alphaanimation"
The same properties of the Alpha label are:

Android:fromalpha animation begins with transparency, from 0.0--1.0, 0.0 for full transparency, 1.0 for completely opaque android:toalpha animation at the end of transparency, also from 0.0--1.0, 0.0 for full transparency, 1.0 for end All opacity corresponds to the constructor:

Alphaanimation (context context, AttributeSet attrs) Also, loading animations from native XML, basically without alphaanimation (float fromalpha, float toalpha) Here is only the last constructor, it is not very difficult, the following examples illustrate the use.

In the first article, the XML code we constructed is:

<?xml version= "1.0" encoding= "Utf-8"?> <alpha xmlns:android=
"http://schemas.android.com/apk/res/" Android "
    android:fromalpha=" 1.0 "
    android:toalpha=" 0.1 "android:duration="
    android: Fillbefore= "true" >
</alpha>
If you construct the same effect with code, the corresponding code is:

Alphaanim = new Alphaanimation (1.0f,0.1f);
Alphaanim.setduration (3000);
Alphaanim.setfillbefore (TRUE);
Five, Rotateanimation

Rotateanimation class corresponds to rotate label, SDK document address: "Rotateanimation"

The XML attributes that the rotate tag has are:

Android:fromdegrees start rotation angle position, positive value represents the clockwise direction degree, negative code counterclockwise direction degree android:todegrees the angle position which rotates to at the end, the positive value represents the clockwise direction degree, the negative code counterclockwise direction degree an               Droid:pivotx scaling Start x axis coordinates, can be numeric, percentage, percentage p three styles, such as 50, 50%, 50%p, the specific meaning has been described in the scale tag, here is no longer repeat Android:pivoty The scaling start Y coordinates can be values, percentages, percent p three styles, such as 50, 50%, and 50%p corresponding constructors are:

Rotateanimation (context context, AttributeSet attrs) loads animations from local XML documents, likewise, basically without 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)

Rotateanimation is similar to scaleanimation, the key issue is also Pivotxtype and Pivotytype choice, also has three values: Animation.absolute, Animation.relative_to_self and Animation.relative_to_parent;

Write the corresponding Java construction code according to the XML in each article:

XML is:

<?xml version= "1.0" encoding= "Utf-8"?> <rotate xmlns:android=
"http://schemas.android.com/apk/res/" Android "
    android:fromdegrees=" 0 "
    android:todegrees=" -650 "
    android:pivotx=" 50% "
    android:pivoty= "50%"
    android:duration= "3000"
    android:fillafter= "true" >
    
</rotate>
The corresponding Java Construction code is:

Rotateanim = new Rotateanimation (0, -650, animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);
Rotateanim.setduration (3000);
Rotateanim.setfillafter (TRUE);
Six, translateanimation very display translateanimation class corresponding translate label, its SDK official document "Translateanimation"

The properties of the translate label are:

Android:fromxdelta starting point x axis coordinates, can be numeric, percentage, percentage p three styles, such as 50, 50%, 50%p, the specific meaning has been described in the scale tag, here is no longer repeat Android:fromydelta start point y axis from the subscript, can In terms of numbers, percentages, percentages p three styles; Android:toxdelta end point x axis coordinate android:toydelta end point Y coordinate these properties correspond to the following constructors:

Translateanimation (context context, AttributeSet attrs) Also, basic not translateanimation (float fromxdelta, float toxdelta, Float Fromydelta, float toydelta) translateanimation (int fromxtype, float fromxvalue, int toxtype, float toxvalue, int fro MYType, float fromyvalue, int toytype, float toyvalue)

Since the three properties of Fromxdelta, Fromydelta, Toxdelta, and Toydelta all have three states, the ideal state in the constructor is the third constructor, which specifies the type of each value. The second constructor: translateanimation (float fromxdelta, float toxdelta, float fromydelta, float Toydelta) use is an absolute value. Only the last constructor can specify a percentage and a percentage relative to the parent control.

The following example uses the XML code in the first article to construct the same effect in Java code:

XML code:

<?xml version= "1.0" encoding= "Utf-8"?> <translate xmlns:android=
"http://schemas.android.com/apk/res/" Android "
    android:fromxdelta=" 0 " 
    android:toxdelta=" -80 "
    android:fromydelta=" 0 "
    Android:toydelta = " -80"
    android:duration= "android:fillbefore=" "
    true" >
</translate>
The corresponding Java code is:

Translateanim = new Translateanimation (animation.absolute, 0, Animation.absolute, -80, 
		animation.absolute, 0, Animation.absolute, -80);
Translateanim.setduration ();
Translateanim.setfillbefore (TRUE);
Seven: The Animationsetanimationset class corresponds to the set tag, defines the collection of action classes, the corresponding SDK documentation "Animationset"
It has no XML attribute of its own, so let's just say its constructor:

Animationset (context context, AttributeSet attrs) Same, Basic not Animationset (Boolean shareinterpolator) Shareinterpolator evaluates to TRUE or FALSE, when true, refers to the definition of an interpolation (Interpolater) in Animationset, which all of the following animations are common. If set to false, it means that the animation underneath it defines its own interpolation.

Add animation to the function: (more functions, see the SDK documentation)

public void Addanimation (Animation a)

The following XML code in the first article, for example, writes Java code that constructs the same effect:

The XML code is:

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android "
    android:duration=" 3000 "
    android:fillafter=" true ">
    
  <alpha 
    android:fromalpha=" 0.0 "
    android:toalpha=" 1.0 "/>
  
  <scale
    android:fromxscale=
    " 0.0 "android:toxscale=" 1.4 " android:fromyscale= "0.0"
    android:toyscale= "1.4" android:pivotx= "50%"
    android:pivoty= "50%"/>
  
  <rotate
    android:fromdegrees= "0"
    android:todegrees= "720"
    android:pivotx= "50%"
    android:pivoty= "50%"/>
       
</set>
The corresponding Java code is:

Alphaanim = new Alphaanimation (1.0f,0.1f);
Scaleanim = new Scaleanimation (0.0f,1.4f,0.0f,1.4f,animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);
Rotateanim = new Rotateanimation (0, 720, animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);

Setanim=new Animationset (true);
Setanim.addanimation (Alphaanim);
Setanim.addanimation (Scaleanim);
Setanim.addanimation (Rotateanim);

Setanim.setduration (3000);
Setanim.setfillafter (TRUE);
Eight, interpolater interpolation on the effect of interpolation and application, we have opened a special article, look here: "Animation animation details (ii)--interpolator interpolation"

For an SDK on the interpolation, see the interpolators part of the Animation resources;

The interpolation XML attributes and corresponding classes are shown in the following table: Interpolator class Resource ID acceleratedecelerateinterpolator @android: Anim/accelerate_ Decelerate_interpolator accelerateinterpolator @android: Anim/accelerate_interpolator anticipateinterpolator @ Android:anim/anticipate_interpolator anticipateovershootinterpolator @android: anim/anticipate_overshoot_ Interpolator bounceinterpolator @android: Anim/bounce_interpolator cycleinterpolator @android: Anim/cycle_ Interpolator decelerateinterpolator @android: Anim/decelerate_interpolator linearinterpolator @android: Anim/linear_ Interpolator overshootinterpolator @android: Anim/overshoot_interpolator

How to use: (Add bounce for sacleanimation)

Scaleanimation interpolatescaleanim=new scaleanimation (0.0f,1.4f,0.0f,1.4f,animation.relative_to_self,0.5f, animation.relative_to_self,0.5f);
Interpolatescaleanim.setinterpolator (New Bounceinterpolator ());
Interpolatescaleanim.setduration (3000);
Ix. example, the source code below I put all of the above codes together in an example, for everyone to download;

The effect chart is as follows:

SOURCE Download Address: http://download.csdn.net/detail/harvic880925/8047669

Please respect the original copyright, reproduced please indicate the source: http://blog.csdn.net/harvic880925/article/details/40117115 Thank you.

If my article can help you, remember to pay attention to it.


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.