Android frame animation, motion tween, attribute animation usage _android

Source: Internet
Author: User
Tags touch

In Android development, you often use some animations, so how do you use them in development?

Frame animation: Not for view to make some changes in shape, but to play a picture, such as some boot animation, similar to movie playback, using animationdrawable to play frame animation
Res/drawable

<?xml version= "1.0" encoding= "Utf-8"?> <animation-list xmlns:android= 
"http://schemas.android.com/apk" /res/android "  
  android:oneshot= true" 
  > 
   
  <item android:drawable= "@drawable/g1" android:duration= "<item" ></item> 
  android:drawable= "@drawable/g2" android:duration= "></item> 
  " <item android:drawable= "@drawable/g3" android:duration= "></item> <item android:drawable= 
  " @ Drawable/g4 "android:duration=" ></item> 
  <item android:drawable= "@drawable/g5" Android: duration= "></item>" 
 
ImageView IV = (ImageView) Findviewbyid (R.ID.IV); 
Iv.setbackgrounddrawable (Getresources (). getdrawable (R.drawable.frame_anim)); 
     
Animationdrawable animationdrawable = (animationdrawable) iv.getbackground (); 
     
Sets whether to perform only one 
//animationdrawable.setoneshot (false); 
     
Animationdrawable.start (); 

Motion Tween (view animation): If View only does some animation, does not click or touch on the view of some operations, you can use motion tween, because view animation, does not change the view position, just do some rendering. The four transformation effects of the view animation correspond to the four subclasses of animation:translateanimation, Scaleanimation, Rotateanimation, and Alphaanimation.

The
 

Property Animation: API11 new features, if you do not only do some animation to view, but also do some click-Touch action on the view, you can use the property animation, because the property animation will change the location of the view. Property animation class has Valueanimator, Objectanimator, Animatorset.

Here's a description of the two property animations

valueanimator Value Animation , it is not used to do some animation of view, it is only for the two values of an excessive animation (in time to the two difference between the two halves, and then according to the time to add a little bit upward), The system provides some valueanimator.ofint (), valueanimator.offloat () for excessive animation between integer and floating-point two, what if two values are their own custom types? Android provides a valueanimator.ofobject () method, where one parameter is the Typeevaluator type (Type estimator), Typeevaluator is an interface, an extension to the developer, and an interface that has a public object evaluate (float fraction, object Startvalue, Object Endvalue) method, which is called continuously during the Valueanimator animation, fraction is the rate of change between 0-1 , the Startvalue is (the custom type's) Start value, Endvalue is (the custom type) end value, the return type is the custom type, and you can calculate how the value of the period changes (if it can be a running trajectory) based on your own needs.

public class Floatevaluator implements Typeevaluator {public  
  object evaluate (float fraction, Object startvalue, Obje CT endvalue) {  
    float startfloat = ((number) startvalue). Floatvalue ();  
    return startfloat + fraction * (((number) endvalue). Floatvalue ()-startfloat);  
      

Use

 Valueanimator MANIMATORENETR = Valueanimator.ofobject (New Floatevaluator (GetContext 
      ()), 0,10); Manimatorenetr.addupdatelistener (New Valueanimator.animatorupdatelistener () {@Override public void onAn 
           
        Imationupdate (Valueanimator animation) {(Float) Animation.getanimatedvalue ()//This is the return of the changing value 
      } 
      }); 
      Manimatorenetr.setduration (1000); Manimatorenetr.addlistener (New Animator.animatorlistener () {@Override public void Onanimationstart (Anim 
           
        Ator animation) {} @Override public void Onanimationend (animator animation) { @Override public void Onanimationcancel (animator animation) {} @Over 
      Ride public void Onanimationrepeat (animator animation) {}}); 

Manimatorenetr.start (); 

Objectanimator animation, it is for view to do some attributes worth changing, it not only on the value of an excessive change, but also will change the value set to the property to be changed, so that it produces animation effect
Objectanimator.ofint (View, "Translationx", 10,20). Setduration. Start (); The argument view is the view that produces the animation, the "Translationx" attribute, and the subsequent argument is the interval of the change
Objectanimator do attribute animation, and does not change the view of the Left,top,right,bottom value, it changes only Translationx and Translationy values, the relationship between these values is X = left + Translationx, y = top + translationy. In the translation process, only the values of x and Translationx, Y, and translatioiny are changed. where x and y are the coordinates of the upper-left corner of the view.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.