Android Animations--frame animations and motion tweens

Source: Internet
Author: User

Frame animation
    • First we define an XML file under the Drawable folder
    • It contains pictures of the animations we want to play, and the length of time each frame is played
<?xml version= "1.0" encoding= "Utf-8"?><animation-list xmlns:android="Http://schemas.android.com/apk/res/android" >    <item android:drawable= "@mipmap/feiben1" android:duration="100 "/>    <item android:drawable="@mipmap/feiben2" android:duration="100" />    <item android:drawable= "@mipmap/feiben3" android:duration="100 "/>    <item android:drawable="@mipmap/feiben4" android:duration= "/>"    <item android:drawable="@mipmap/feiben5" android:duration="100" />    <item android:drawable= "@mipmap/feiben6" android:duration="100 "/>    <item android:drawable="@mipmap/feiben7" android:duration= "/>"    <item android:drawable="@mipmap/feiben8" android:duration="100" /></animation-list>
    • The following is a reference to our XML file to show the animation, here we have two ways
1. Referencing in the layout file
    • You only need to refer to the corresponding XML file in the SRC attribute.
    <ImageView        android:id="@+id/image_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/run"/>
    • Note in the layout of the reference, I test the time in the 5.0 version of the simulator, and my own phone will not play, replaced by 4.4.4 on it, I do not know whether it is only my this, special instructions
2. Referencing in code
imageView.setBackgroundResource(R.drawable.run);                AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();                animationDrawable.start();

Motion Tweens
    • Tween animations are divided into four types
      Displacement Animation translateanimation
      Rotate animation rotateanimation
      Zoom Animation Scaleanimation
      Fade animation alphaanimation

    • tweened animations Create a new Anim folder under the Res folder, with an animated XML file placed underneath

Related properties
    • Setduration (Long Durationmills)
      Set animation duration (in milliseconds)
    • Setfillafter (Boolean fillafter)
      If the value of Fillafter is true, the control stays at the end of execution after the animation is executed
    • Setfillbefore (Boolean Fillbefore)
      If the value of Fillbefore is true, the control returns to the state before the animation executes after the animation is executed
    • Setstartoffset (Long Startoffset)
      Set the wait time before the animation executes
    • Setrepeatcount (int repeatcount)
      Set the number of times an animation repeats execution

    • Interpolator defines the rate at which the animation changes, and defines several interpolator in the animations framework.
      Acceleratedecelerateinterpolator: At the beginning and end of the animation, the rate change is relatively slow, in the middle of the time speed.
      Accelerateinterpolator: At the beginning of the animation, the rate change is slow, and then start accelerating
      Cycleinterpolator: Animation loop plays a specific number of times, rate changes along the sine curve
      Decelerateinterpolator: At the beginning of the animation, the rate change is slow, and then start slowing down
      Linearinterpolator: Animations change at a uniform rate

    • Interpolator is generally defined in the label of the set
Displacement Animation translateanimation
    • The corresponding XML layout file is as follows, where the properties are relatively simple and are no longer explained
<?xml version= "1.0" encoding= "Utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android: Duration="> "    <translateandroid:fromxdelta="0"android:fromydelta="0" Android:toxdelta="$"android:toydelta="0">                                </Translate></Set>
    • The corresponding code is as follows
imageView2.clearAnimation();                Animation animationTranslate = AnimationUtils.loadAnimation(this, R.anim.translateanimation);                imageView2.setImageResource(R.mipmap.ic_launcher);                imageView2.setAnimation(animationTranslate);

Rotate animation rotateanimation
    • The corresponding XML file
    • RepeatCount indicates repetition, 1 means loop
    • Pivotx and Pivoty represent the center point of rotation, and 50% means the center of the image.
<?xml version= "1.0" encoding= "Utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"android: Duration=">     "    <rotateandroid:repeatcount="-1"android:fromdegrees="0"  android:pivotx="50%"android:pivoty="50%"android: Todegrees="The" >                                        </rotate></Set>
    • The corresponding code is as follows
imageView2.clearAnimation();                Animation animationRotate = AnimationUtils.loadAnimation(this, R.anim.rotateanimation);                imageView2.setImageResource(R.mipmap.ic_launcher);                imageView2.setAnimation(animationRotate);

Zoom Animation Scaleanimation
    • The corresponding XML file is as follows
    • Fromxscale starting picture scale, 1 for normal size
    • Toxscale the scale of the gradient, 2 means twice times the magnification
<?xml version= "1.0" encoding= "Utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"android: Duration=">     "    <scaleAndroid:fromxscale="1"android:fromyscale="1"  Android:pivotx="50%"android:pivoty= "50%"android:toxscale="2"  Android:toyscale="2">                                                </scale ></Set>
    • The corresponding code is as follows
imageView2.clearAnimation();                Animation animationScale = AnimationUtils.loadAnimation(this, R.anim.scaleanimation);                imageView2.setImageResource(R.mipmap.ic_launcher);                imageView2.setAnimation(animationScale);

Fade animation alphaanimation
    • The corresponding XML file is as follows
    • Fromalpha represents the beginning of transparency, 1 means opaque
    • Toalpha represents the final transparency, 0 is completely transparent
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="5000">    <alpha        android:fromAlpha="1"        android:toAlpha="0"></alpha></set>
    • The corresponding logic code is as follows
imageView2.clearAnimation();                Animation animationAlpha = AnimationUtils.loadAnimation(this, R.anim.alphaanimation);                imageView2.setImageResource(R.mipmap.ic_launcher);                imageView2.setAnimation(animationAlpha);

Combination of tweened animations
    • The corresponding XML file is as follows
<?xml version= "1.0" encoding= "Utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android: Duration="> "    <Alphaandroid:fromalpha="1"android:toalpha="0">                 </Alpha>    <rotateandroid:fromdegrees="0"android:pivotx="50%"  Android:pivoty="50%"android:todegrees=">                                 "</rotate>    <translateandroid:fromxdelta="0"android:fromydelta="0"  Android:toxdelta="$"android:toydelta="0">                                </Translate></Set>
    • The corresponding logic code is as follows
imageView2.clearAnimation();                Animation animationMany = AnimationUtils.loadAnimation(this, R.anim.manyanimation);                imageView2.setImageResource(R.mipmap.ic_launcher);                imageView2.setAnimation(animationMany);

Listener Events for tweened animations
    • With the above combination of motion tweens as an example
    • The code here is easy to understand, no longer one by one explained
ImageView2. Clearanimation();Animation Animationmany = animationutils. Loadanimation(This, R. Anim. Manyanimation);ImageView2. Setimageresource(R. Mipmap. IC_launcher);ImageView2. Setanimation(Animationmany);Animationmany. Setanimationlistener(New Animation. Animationlistener() {@Override public void Onanimationstart (Animation Animation) { Toast. Maketext(mainactivity. this,"animation begins.", Toast. LENGTH_short). Show();} @Override public void Onanimationend (Animation Animation) { Toast. Maketext(mainactivity. this,"The animation is over.", Toast. LENGTH_short). Show();} @Override public void Onanimationrepeat (Animation Animation) { Toast. Maketext(mainactivity. this,"animation repeats.", Toast. LENGTH_short). Show();}                });

Android Animations--frame animations and motion tweens

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.