Android animation-based Tween animation practices

Source: Internet
Author: User
Android animations are divided into Tween and Frame animations. The previous section introduces Frame animations through an example. This section describes Tween animations. Tween allows you to zoom in, zoom in, rotate, and gradient objects. Tween animation has four main implementations, which are described below: 1. AlphaAnimation: gradient animation, which mainly controls transparency change animation. It is often constructed using AlphaAnimation (float fromAlpha, float toAlpha; fromAlpha: transparency at the animation start (value range: 0.0 to 1.0); toAlpha: transparency at the animation end; 2. ScaleAnimation: animation class that mainly controls scaling changes, we often use ScaleAnimation (float fromX, float toX, float fromY, float toY, int limit txtype, float limit txvalue, int limit tytype, float limit tyvalue) to construct; fromX: scaling scale at the animation start X coordinate; toX: Scaling scale at the animation end X coordinate; fromY: Scaling scale at the animation start Y coordinate; toY: Scaling scale on the Y coordinate after the Animation ends; scaling txtype: Scaling mode on the X coordinate; values: Animation. ABSOLUTE, Animation. RELATIVE_TO_SELF, Animation. RELATIVE_TO_PARENT; pivotXValue: The scaling value on the X coordinate; the scaling mode on the tytype: Y coordinate; values: Animation. ABSOLUTE, Animation. RELATIVE_TO_SELF, Animation. scaling; scaling tyvalue: Scaling value on Y coordinate; 3. TranslateAnimation: Mainly used to control animation implementation class of position transformation. It is often used to TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) to construct; fromX Delta: X coordinate of the animation start; toXDelta: X coordinate of the animation end; fromYDelta: Y coordinate of the animation start; toYDelta: Y coordinate of the animation end; 4. RotateAnimation: it mainly controls the animation implementation class of rotation. It is often constructed using lift (float fromDegrees, float toDegrees, int rotate txtype, float rotate txvalue, int rotate tytype, float rotate tyvalue). fromDegrees: rotation start angle; toDegrees: the rotation end angle. The rotation txtype, rotation txvalue, rotation tytype, and rotation tyvalue are similar to the scaling animation ScaleAnimation. The following uses an instance to perform these animations. 1. Add the mmimage to be animated. (to avoid infringement of others' permission, this time MM is different from the previous desktop background. This MM has no Avatar ): 2. Create Layout XML to configure tween. xml file:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: apk = "http://schemas.android.com/apk/res/android" apk: orientation = "vertical" apk: layout_width = "fill_parent" apk: layout_height = "fill_parent">
<! -- Animation MM -->
<ImageView apk: id = "@ + id/TweenMM" apk: src = "@ drawable/tween" apk: layout_width = "wrap_content" apk: layout_height = "wrap_content"/>

<! -- Animation control button -->
<LinearLayout apk: layout_weight = "1" apk: orientation = "horizontal" apk: layout_width = "fill_parent" apk: layout_height = "wrap_content">
<Button apk: text = "ScaleAnim" apk: layout_weight = "1" apk: layout_width = "fill_parent" apk: layout_height = "wrap_content" apk: onClick = "onBtnScaleAnimClick"/>
<Button apk: text = "AlphaAnim" apk: layout_weight = "1" apk: layout_width = "fill_parent" apk: layout_height = "wrap_content" apk: onClick = "onBtnAlphaAnimClick"/>
</LinearLayout>
<LinearLayout apk: layout_weight = "1" apk: orientation = "horizontal" apk: layout_width = "fill_parent" apk: layout_height = "wrap_content">
<Button apk: text = "TranslateAnim" apk: layout_weight = "1" apk: layout_width = "wrap_content" apk: layout_height = "wrap_content" apk: onClick = "onBtnTranslateAnimClick"/>
<Button apk: text = "RotateAnim" apk: layout_weight = "1" apk: layout_width = "wrap_content" apk: layout_height = "wrap_content" apk: onClick = "onBtnRotateAnimClick"/>
</LinearLayout>
</LinearLayout>
One image and four buttons, these four buttons are used to control the image animation. 3. For each animation, we use an XML configuration file to set the attributes of the animation. The configuration file content of AlphaAnimation (tween_alpha.xml:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="5000" />
</set>
Android: duration indicates the animation duration in milliseconds. The configuration file content of RotateAnimation (tween_rotate.xml:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="5000" />
</set>
The configuration file content of ScaleAnimation (tween_scale.xml:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="5000" />
</set>
Content of the configuration file of TranslateAnimation (tween_translate.xml:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="10" android:toXDelta="100" android:fromYDelta="10" android:toYDelta="100" />
</set>

4. Activity interface class:

/*** Copyright (c) 2004-2011 All Rights Reserved. */package com. aboy. android. study. animation; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. animation. animation; import android. view. animation. animationUtils; import android. widget. imageView; import com. aboy. android. study. r;/*** implement Tween animation through XML configuration file *** @ author obullxl@gmail.com * @ version $ Id: TweenXMLActivity. java, v 0.1 01:36:39 oldbulla Exp $ */public class TweenXMLActivity extends Activity {public static final String TAG = "TweenActivity"; // Animated Image private ImageView tweenMM; /*** @ see android. app. activity # onCreate (android. OS. bundle) */public void onCreate (Bundle cycle) {super. onCreate (cycle); super. setContentView (R. layout. tween); // get the animated image this. tweenMM = (ImageView) super. findViewById (R. id. tweenMM);}/*** button: Size Change animation */public void onBtnScaleAnimClick (View view) {// The animation starts this. doStartAnimation (R. anim. tween_scale);}/*** button: gradient animation */public void onBtnAlphaAnimClick (View view) {// The animation starts this. doStartAnimation (R. anim. tween_alpha);}/*** button: location change animation */public void onBtnTranslateAnimClick (View view) {// The animation starts this. doStartAnimation (R. anim. tween_translate);}/*** button: rotation animation */public void onBtnRotateAnimClick (View view) {// The animation starts this. doStartAnimation (R. anim. tween_rotate);}/*** start Animation */private void doStartAnimation (int animId) {// load animation Animation animation = AnimationUtils. loadAnimation (this, animId); // The animation starts this. tweenMM. startAnimation (animation );}}
You can use the AnimationUtils class to easily Construct an animation class. to start an animation, you only need ImageView. startAnimation.
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.