Android Game Development Study Notes (1): tweened animation

Source: Internet
Author: User

Custom animation in android has two modes: tweened animation and frame by frame. Here we introduce a method to implement tweened animation through xml.
Tweened animation (gradient animation) has four types of animation: alpha (transparency), scale (size scaling), translate (location conversion), and rotate (graphical rotation ).
First, create a project named Animation, create an anim directory under res, and create an xml file named myanim. xml in the directory. The Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Set xmlns: android = "http://schemas.android.com/apk/res/android">
<Alpha
Android: fromalphi = "0.1"
Android: toAlpha = "1.0"
Android: duration= "2000"
/> <! -- Change transparency -->
<! -- The fromAlpha attribute is the transparency when the animation starts, and the toAlpha attribute is the transparency when the animation ends,
Duration is the animation duration -->
<Scale
Android: interpolator = "@ android: anim/accelerate_decelerate_interpolator"
Android: fromXScale = "0.0"
Android: toXScale = "1.4"
Android: fromYScale = "0.0"
Android: toYScale = "1.4"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: fillAfter = "false"
Android: duration= "3000"
/> <! -- Changing the size -->
<! -- Interpolator specifies an animation insertor. The fromXScale attribute is on the x coordinate at the start of the animation.
Scaling size. The toCScale attribute is the scaling size on the x coordinate at the animation end, And the fromYScale attribute is the animation
The scaling size on the y coordinate at the start, the toYScale attribute is the scaling size on the y coordinate at the end of the animation, and the limit TX
And when the animation is set relative to its position, fillAfter indicates whether the animation is converted after the animation ends
Applied -->
<Translate
Android: fromXDelta = "30"
Android: toXDelta = "0"
Android: fromYDelta = "30"
Android: toYDelta = "50"
Android: duration= "3000"
/> <! -- Location change -->
<! -- The fromXDelta attribute is the position on the x coordinate at the animation start, and the toXDelta attribute is the x coordinate at the animation end.
The position on. The fromYDelta attribute is the position on the y coordinate when the animation starts, and the toYDelta attribute is y when the animation ends.
Coordinates -->
<Rotate
Android: interpolator = "@ android: anim/accelerate_decelerate_interpolator"
Android: fromDegrees = "0"
Android: toDegrees = "+ 350"
Android: Required Tx = "50%"
Android: Ty = "50%"
Android: duration= "3000"
/> <! -- Rotating and transforming -->
<! -- Interpolator is also an animation plug-in. The fromDegrees attribute is the object at the animation start.
The toDegrees attribute is the Rotation Angle of the object when the animation ends. -->
</Set>
Then write the layout file main. xml. The Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<ImageView
Android: id = "@ + id/myImageView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: src = "@ drawable/preview"
/>
</LinearLayout>
Preview is an image placed in res/drawable-mdpi. We use this image as an animated demonstration.
Finally, write the code for loading the animation in MainActivity. java:
Package game. test;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. animation. Animation;
Import android. view. animation. AnimationUtils;
Import android. widget. ImageView;
 
Public class MainActivity extends Activity {
Animation myAnimation;
ImageView myImageView;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MyAnimation = AnimationUtils. loadAnimation (this, R. anim. myanim );
MyImageView = (ImageView) this. findViewById (R. id. myImageView );
MyImageView. startAnimation (myAnimation );
}
}
Author's "Android Learning Experience"

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.