Android animation learning--Tween Animation_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:43:49
Original
1118 people have browsed it

Table of Contents

  • Table of Contents
  • Android Animation Learning
  • Tween Animation
  • scale animation to adjust size
  • alpha animation to adjust transparency
  • rotate animation to rotate
  • translate animation to translate
  • Android animation learning

    There are three types of animation in android:

    1. Tween Animation: generated by continuously performing image transformations (translation, scaling, rotation) on objects in the scene Animation effect is a gradient animation.
    2. Frame Animation: sequentially plays pre-made images, which is a kind of screen transition animation.
    3. Property Animation: Property animation, which achieves animation effects by dynamically changing the properties of objects. Property animation is a new feature of API 11.

    This blog mainly analyzes the basic use of Tween Animation.

    Tween Animation

    Tween Animation has four forms:

    1. alpha: gradient transparent animation effect.
    2. scale: Gradient size expansion and contraction animation effect.
    3. translate: Screen position movement animation effect.
    4. rotate: Screen rotation animation effect.

    These four animation implementation methods are all implemented through the combination of Animation class and AnimationUtils. Animation effects can be pre-configured in the xml file in the res/anim directory.

    Scale animation? Adjust size

    The parameters of scale are as follows:

  • android:fromXScale: The scaling ratio relative to itself in the starting X direction, Floating point value. For example, 1.0 represents no change, 0.5 represents twice the initial size, and 2.0 represents doubling the size.
  • android:toXScale: The scaling ratio relative to itself in the X direction at the end.
  • android:fromYScale: The scaling ratio relative to itself in the Y direction at the beginning.
  • android:toYScale: The scaling ratio relative to itself in the Y direction at the end.
  • android:pivotX: The X-axis coordinate of the zoom starting point, which can be in three forms: numerical value, percentage, and percentage. (Note: The starting point refers to the coordinates of the upper left corner of the current View)
  • android:pivotY: Scale the Y-axis coordinate of the starting point.
  • In addition, the Animation class is the base class for all animations (scale, alpha, translate, rotate). Properties inherited from the Animation class:

  • android:duration: animation duration in milliseconds unit.
  • android:fillAfter: If set to true, when the control animation ends, the last state of the animation will be maintained.
  • android:fillBefore: If set to true, after the control animation ends, it will be restored to the state before the startup animation.
  • android:fillEnabled: has the same effect as android:fillBefore, both restore the control to its initial state at the end of the animation.
  • android:repeatCount: Number of repetitions
  • android:repeatMode: Repeat type, with two values: reverse and restart. Reverse means playback in reverse order, and restart means playback again.
  • Talking without practice is not enough. Here are an anim xml file, a layout file and an Activity class to practice the use of scale animation.

    res/anim/anim_scale.xml file is as follows:

    <?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="700" android:fromXScale="0.0" android:fromYScale="0.0" android:pivotX="150" android:pivotY="300" android:toXScale="1.4" android:toYScale="1.4" android:fillAfter="true" android:repeatCount="10" android:repeatMode="reverse"></scale>
    Copy after login

    res/layout/tween_animation_layout.xml file is as follows:

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >    <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >        <Button  android:id="@+id/alpha_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/alpha" />        <Button  android:id="@+id/scale_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/scale" />        <Button  android:id="@+id/rotate_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/rotate" />        <Button  android:id="@+id/trans_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/trans" />    </LinearLayout>    <ImageView  android:id="@+id/anim_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:contentDescription="@null" android:src="@drawable/splash_logo" /></LinearLayout>
    Copy after login

    Animation demonstration implementation class is implemented as follows :

    import com.example.photocrop.R;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageView;public class TweenAnimationTest extends Activity implements OnClickListener {    private Button alphaButton;    private Button scaleButton;    private Button rotateButton;    private Button transButton;    private ImageView animImageView;    @Override    protected void onCreate(Bundle savedInstanceState) {    // TODO Auto-generated method stub    super.onCreate(savedInstanceState);    setContentView(R.layout.tween_animation_layout);    initView();    }    private void initView() {    animImageView = (ImageView) findViewById(R.id.anim_image);    alphaButton = (Button) findViewById(R.id.alpha_button);    scaleButton = (Button) findViewById(R.id.scale_button);    rotateButton = (Button) findViewById(R.id.rotate_button);    transButton = (Button) findViewById(R.id.trans_button);    alphaButton.setOnClickListener(this);    scaleButton.setOnClickListener(this);    rotateButton.setOnClickListener(this);    transButton.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {    case R.id.alpha_button:        break;    case R.id.scale_button:        Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_scale);        animImageView.startAnimation(animation);        break;    case R.id.rotate_button:        break;    case R.id.trans_button:        break;        }    }}
    Copy after login

    alpha animation? Adjust transparency

    The parameters of alpha are as follows:

  • android:fromAlpha: transparency at the beginning of animation, from 0.0~ 1.0, 0.0 means completely transparent, 1.0 means completely opaque.
  • android:toAlpha: The transparency at the end of the animation is also from 0.0~1.0.
  • Properties inherited from the Animation class:

  • android:duration: animation duration, in milliseconds.
  • android:fillAfter: If set to true, when the control animation ends, the last state of the animation will be maintained.
  • android:fillBefore: If set to true, after the control animation ends, it will be restored to the state before the startup animation.
  • android:fillEnabled: has the same effect as android:fillBefore, both restore the control to its initial state at the end of the animation.
  • android:repeatCount: Number of repetitions
  • android:repeatMode: Repeat type, with two values: reverse and restart. Reverse means playback in reverse order, and restart means playback again.
  • We use the previous TweenAnimationTest class as a template. When the alpha button is clicked, we trigger the transparency animation effect.

    res/anim/anim_alpha.xml file is as follows:

    <?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fillBefore="true" android:fromAlpha="0.0" android:toAlpha="1.0" ></alpha>
    Copy after login

    The listener event in response to clicking the alpha button is:

    case R.id.alpha_button:    Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);    animImageView.startAnimation(alphaAnimation);    break;
    Copy after login

    rotate animation?rotate

    rotate parameters are as follows:

  • android:fromDegrees: the angular position of starting rotation, positive value represents the angle of clockwise rotation, and negative value represents the angle of counterclockwise rotation.
  • android:toDegrees: The angular position to which the rotation is completed. Positive values ​​represent clockwise angles, and negative values ​​represent counterclockwise rotation angles.
  • android:pivotX: X-axis coordinate of the starting point of rotation.
  • android:pivotY: Y-axis coordinate of the starting point of rotation.
  • Properties inherited from the Animation class:

  • android:duration:动画持续时间,以毫秒为单位。
  • android:fillAfter:如果设置为true,控件动画结束时,将保持动画最后时的状态。
  • android:fillBefore:如果设置为true,控件动画结束后,还原到开机动画前的状态。
  • android:fillEnabled:与android:fillBefore的效果相同,都是在动画结束时将控件还原到初始状态。
  • android:repeatCount:重复次数
  • android:repeatMode:重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍。
  • res/anim/anim_rotate.xml文件如下:

    <?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fillAfter="true" android:fromDegrees="0" android:pivotX="0" android:pivotY="0" android:repeatCount="3" android:toDegrees="810" ></rotate>
    Copy after login

    响应点击alpha button的listener事件为:

    case R.id.rotate_button:    Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);    animImageView.startAnimation(rotateAnimation);    break;
    Copy after login

    translate动画?平移

    translate参数:

  • android:fromXDelta:起始X轴坐标。
  • android:fromYDelta:起始Y轴坐标。
  • android:toXDelta:结束X轴坐标。
  • android:toYDelta:结束Y轴坐标。
  • 从Animation类继承的属性:

  • android:duration:动画持续时间,以毫秒为单位。
  • android:fillAfter:如果设置为true,控件动画结束时,将保持动画最后时的状态。
  • android:fillBefore:如果设置为true,控件动画结束后,还原到开机动画前的状态。
  • android:fillEnabled:与android:fillBefore的效果相同,都是在动画结束时将控件还原到初始状态。
  • android:repeatCount:重复次数
  • android:repeatMode:重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍。
  • 注意:京东splash页面的进度条用的就是translate动画。

    res/anim/anim_rotate.xml文件如下:

    <?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromXDelta="0.0" android:toXDelta="60%p" ></translate>
    Copy after login

    响应点击alpha button的listener事件为:

    case R.id.trans_button:    Animation transAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_translate);    animImageView.startAnimation(transAnimation);    break;
    Copy after login
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template