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 CodeDuration: 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 axisToXDelta: 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.