Xml usage of android Animation

Source: Internet
Author: User

Xml usage of android Animation

In Android applications, using animation effects can bring users a better feeling. animation can be implemented through XML or Android code.

Animation effects can be implemented in two ways: tweened animation (gradient animation) and frame by frame Animation (screen conversion animation ).

Tweened animation gradient animation has the following two types:

1. alpha gradient transparency animation effect

2. scale the scaling animation effect of the gradient size

Frame by frame animation:

1. translate the animation effect after converting the position of the screen

2. rotate screen transfer rotation animation effect

Here, I use XML for animation. Implement basic animations, such as fade in and rotate.

Step: 1. Create an anim folder in the res directory and create an animation xml resource file in anim.

Anim/alpha. xml (gradient animation)

 
      
  
 

Anim/scale. xml (scaling animation)

 
    
  
 
 
Anim/translate. xml (mobile animation)
 
      
  
  
 

Anim/rotate. xml (rotation animation)

 
     
    
  
 

2. After the xml resource file is created, call these resource files. Take alpha as an example to create AlphaActivity first.

Put a background image in activity_alpha.xml.

 
AlphaActivity. java
Package com. example. animation_demo; import android. OS. bundle; import android. app. activity; import android. view. view; import android. view. animation. animation; import android. view. animation. animation. animationListener; import android. view. animation. animationUtils; import android. widget. toast; public class AlphaActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); View view = View. inflate (this, R. layout. activity_alpha, null); setContentView (view); // use the static method loadAnimation () of the AnimationUtils class to load the Animation XML file animation Animation = AnimationUtils in XML. loadAnimation (this, R. anim. alpha); view. startAnimation (animation); animation. setAnimationListener (new AnimationListener () {@ Override public void onAnimationStart (Animation arg0) {}// use @ Override public void onAnimationRepeat (Animation arg0) when the Animation starts) {} // use @ Override public void onAnimationEnd (Animation arg0) {Toast. makeText (AlphaActivity. this, "Use at animation end", Toast. LENGTH_SHORT ). show ();}});}}

This is the animation of the entire layout file, and any UI element can call the starAnimation method.

For example, textMsg. startAnimation (animation). In this case, you do not need to fill in the following code to specify xml.

View view=View.inflate(this, R.layout.activity_alpha, null);setContentView(view);
SetContentView (R. layout. activity_alpha); OK. Pawn.

I just used it today. I will post an image later ..


Attached to some of the basic xml results:

Fade In: Fade In

Alpha is the gradient transparency effect. The value ranges from 0 to 1.

fade_in.xml  
   
            
  
Fade Out: Fade Out
The opposite of Fade In is true. The value ranges from 1 to 0.
fade_out.xml  
   
            
   
Cross Fading: fade in and out of the Cross
Both Fade in and Fade out can achieve crossover effect.
public class CrossfadeActivity extends Activity implements AnimationListener {        TextView txtMessage1, txtMessage2;      Button btnStart;      Animation animFadeIn, animFadeOut;      @Override      protected void onCreate(Bundle savedInstanceState) {          // TODO Auto-generated method stub          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_crossfade);          txtMessage1 = (TextView) findViewById(R.id.txtMessage1);          txtMessage2 = (TextView) findViewById(R.id.txtMessage2);          btnStart = (Button) findViewById(R.id.btnStart);          // load animations          animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),                  R.anim.fade_in);          animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),                  R.anim.fade_out);          // set animation listeners          animFadeIn.setAnimationListener(this);          animFadeOut.setAnimationListener(this);            // button click event          btnStart.setOnClickListener(new View.OnClickListener() {                @Override              public void onClick(View v) {                  txtMessage2.setVisibility(View.VISIBLE);                  txtMessage2.startAnimation(animFadeIn);                  txtMessage1.startAnimation(animFadeOut);              }        });    }      @Override      public void onAnimationEnd(Animation animation) {                    if (animation == animFadeOut) {              txtMessage1.setVisibility(View.GONE);          }                    if(animation == animFadeIn){              txtMessage2.setVisibility(View.VISIBLE);          }      }        @Override      public void onAnimationRepeat(Animation animation) {          // TODO Auto-generated method stub      }        @Override      public void onAnimationStart(Animation animation) {          // TODO Auto-generated method stub      }  }

BLink: invisible

blink.xml  
   
         
   
Zoom In: Zoom In
zoom_in.xml  
   
       
        
    
   
Zoom Out: Zoom Out
   
       
        
    
   
Rotate: Rotate
rotate.xml
   
       
    
 
So long... Now that you have all seen this, let's say a few more ..

Root Node Properties:

Name

Attribute

Remarks

Android: Movie interpolator

Whether to share the inserter

When shared, each of the four subnodes uses an insertor.

Android: interpolator

Specifies an animation insertor.

Use System Resources

Android: fillEnabled

When it is set to true, fillAfter, fill, and Befroe will both be true. In this case, the fillBefore and fillAfter attributes will be ignored.

Android: fillAfter

Whether the animation is applied after the animation ends

Boolean

Android: fillBefore

Whether the animation is applied before the animation starts.

Boolean

Android: repeatMode

Repeat mode

"Restart" or "reverse"

Android: repeatCount

Repeated times

Integer

Android: duration

Animation duration

Integer

Android: startOffset

Animation Interval

Long

Android: zAdjustment

Define the z order Transformation of an animation

[Normal] or [top] or [bottom]

Android: detachWallpaper

Boolean


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.