Android Animation (Tween Animation) Basic Learning _android

Source: Internet
Author: User

Objective

I told you before. In Android, there are two ways to implement an animated Animation: Tween Animation (gradient animation) and frame Animation (frames animation). Gradient animation is the animation effect of the object in the scene by constantly making the image transform (panning, zooming, rotating, etc.). Frame animations are animated by sequentially playing an image prepared in advance, similar to a movie.

Small series also share with you the basics of frame-by-step animation, below we will learn the basics of frame-by-step animation in Android.

principle: The first and end two keyframes are given, and the interpolation frames between two keyframes are computed automatically by the computer.

Category: AlphaAnimation (transparency) ScaleAnimation (zoom) TranslateAnimation (displacement) RotateAnimation (rotate) AnimationSet Combination

Way:

1. In the Codenew

2. Define an animated XML resource under the Anim folder

Effect

Code

Step one: Prepare animation resources

Directory

<?xml version= "1.0" encoding= "Utf-8"?> <alpha xmlns:android=
"http://schemas.android.com/apk/res/" Android "
 android:duration=" "
 android:fromalpha=" 1.0 "
 android:interpolator=" @android: anim/ Linear_interpolator "
 android:toalpha=" 0.3 ">

</alpha>
<?xml version= "1.0" encoding= "Utf-8"?> <rotate xmlns:android=
"http://schemas.android.com/apk/res/" Android "
 android:interpolator=" @android: Interpolator/linear "
 android:repeatcount=" Infinite
 " Android:repeatmode= "Reverse"
 android:duration= "android:fromdegrees=" "
 0"
 android:todegrees= "1080" >
 android:pivotx= "50%"
 android:pivoty= "50%"
</rotate>
<?xml version= "1.0" encoding= "Utf-8"?> <scale xmlns:android=
"http://schemas.android.com/apk/res/" Android "
 android:interpolator=" @android: Anim/linear_interpolator "
 android:duration="
 android: Fillafter= "true"
 android:fromxscale= "1.0"
 android:fromyscale= "
 1.0" android:pivotx= "50%" Android:pivoty= "50%"
 android:toxscale= "0.3"
 android:toyscale= "0.3" >

</scale>
<?xml version= "1.0" encoding= "Utf-8"?> <translate xmlns:android=
"Http://schemas.android.com/apk/res" /android "
  android:interpolator=" @android: Anim/linear_interpolator "
  android:duration=
  " Android:fromxdelta= "Android:fromydelta="
  android:toxdelta= "ten"
  android:toydelta= "300" >

</translate>
<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android "
 android:interpolator=" @android: Anim/linear_interpolator "
 android:duration=" >

 < Alpha Android:fromalpha= "0.3"
  android:toalpha= "1.0"/> <rotate android:fromdegrees=

 "0"
  android: todegrees= "360"
  android:pivotx= "0"
  android:pivoty= "0"
  android:repeatmode= "restart"
  android: Repeatcount= "Infinite"/>
</set>

Step two: Activity_main.xml (slightly)

Step Three: Mainactivity.java

Package Com.lyp.anim;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import android.view.animation.Animation;
Import Android.view.animation.AnimationUtils;
Import Android.widget.Button;

Import Android.widget.ImageView;
 public class Mainactivity extends appcompatactivity implements view.onclicklistener{private Button Btnscale;
 Private Button btnrotate;
 Private Button btntranslate;
 Private Button Btnalpha;

 Private Button Btnall;

 Private ImageView Mimage;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

 Setcontentview (R.layout.activity_main);
 Initview ();
 private void Initview () {btnscale= (Button) Findviewbyid (R.id.btn_scale);
 Btnrotate= (Button) Findviewbyid (r.id.btn_rotate);
 Btntranslate= (Button) Findviewbyid (r.id.btn_translate);
 Btnalpha= (Button) Findviewbyid (R.id.btn_alpha);

 Btnall= (Button) Findviewbyid (R.id.btn_all); Mimage= (ImageView) findviewbyiD (R.id.image);
 Btnscale.setonclicklistener (this);
 Btnrotate.setonclicklistener (this);
 Btntranslate.setonclicklistener (this);
 Btnalpha.setonclicklistener (this);
 Btnall.setonclicklistener (this); @Override public void OnClick (View v) {switch (V.getid ()) {case R.id.btn_scale://Load Zoom animation Animation scale = an
  Imationutils.loadanimation (this, r.anim.scale); Scale.setfillafter (TRUE);
  Leave animation end state, set invalid in XML file!!
  Mimage.startanimation (scale);
  Break
  Case R.id.btn_rotate://Load rotary animation Animation rotate = animationutils.loadanimation (this, r.anim.rotate);
  Mimage.startanimation (rotate);
  Break
  Case R.id.btn_translate://Load Displacement animation Animation translate = Animationutils.loadanimation (this, r.anim.translate);
  Mimage.startanimation (translate);
  Break
  Case R.ID.BTN_ALPHA://Load Transparency gradient animation Animation Alpha = Animationutils.loadanimation (this, r.anim.alpha);
  Mimage.startanimation (Alpha);
  Break Case R.id.btn_all://load combination animation Animation all = Animationutils.loadanimation(This, r.anim.all);
  Mimage.startanimation (All);
 Break }
 }
}

Summarize

The above Android animation (Tween Animation) Foundation of the entire content of the animation Animation implementation of the two ways is now all to share, I hope you can help Android developers, if you have questions you can message exchange.

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.