search
HomeWeb Front-endHTML TutorialAndroid animation learning--Tween Animation_html/css_WEB-ITnose

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>

    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>

    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;        }    }}

    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>

    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;

    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>

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

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

    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>

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

    case R.id.trans_button:    Animation transAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_translate);    animImageView.startAnimation(transAnimation);    break;
    Statement
    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
    Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

    The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

    How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

    The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

    What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

    Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

    How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

    This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

    What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

    How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

    This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

    What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment