Tween animation is implemented through encoding and xml files.

Source: Internet
Author: User

Tween has four animation effects: alpha (transparent), rotate (rotating), translate (moving), scale (scaling );

It can be implemented through hard encoding and xml files.

Xml implementation:

Step 1: Create a folder named anim under the res file of the project (required)

Step 2: Create an xml file under the anim folder and set the animation effect in the xml file.

Step 3: Use AnimationUtils. loadAnimation (MainActivity. this, R. anim. xx); In the Activity to obtain it.

1. alpha

Alpha xml configuration file:

 
In the above Code

Duration: the time required to execute the animation.

FillAfter: whether to stay at the last frame of the animation after the animation is executed. The default value is false. When the animation ends, the image will return to the starting position.

FromAlpha: transparency at the beginning

ToAlpha: transparency at the end

Alpha View:

    
      
Member variables:

private ImageView ivalpha, ivrotate, ivtranslate, ivscale;private Animation alphaAnimation, rotateAnimation, translateAnimation,scaleAnimation;private Button btalpha, btrotate, bttranslate, btscale;


Btalpha click event:

alphaAnimation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha);ivalpha.startAnimation(alphaAnimation);

This completes a complete alpha animation.

2. scale

Scale xml file:

  
  

FromXScale and fromYScale: scale the X axis and Y axis at the beginning of the animation. For example, if fromXScale is 0.5 and fromYScale is 2, the image width is half the original width, the height is twice the original size and then the scaling starts.

ToXScale and toYScale: scale the X and Y axes of the image at the end.

Encoding implementation:

1. Create an Animation object

2. Set Animation Parameters

3. Set setAnimation in ImageView

Translate

    
      

Bttranslate click event:

 translateAnimation=new TranslateAnimation(0, 100, 0, 100); translateAnimation.setDuration(2000); ivtranslate.setAnimation(translateAnimation);
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
FromXDelta: coordinates of the Start X axis

ToXDelta: Position of the X axis coordinate at the end

FromYDelta: Start Y axis Coordinate Position

ToYDelta: Position of the Y axis at the end

This is an animated effect of moving an image.

Rotate

rotateAnimation=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(2000); rotateAnimation.setFillAfter(true); ivrotate.setAnimation(rotateAnimation);
new RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue);

FromDegrees: The image angle when the rotation starts.

ToDegrees: the Rotation Angle

PivotXType: Where is the reference rotation, where is the image itself

Rotate txvalue: The image center point, which is the first half of the image width.






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.