Project requirements: Text scrolling is required. Considering that there are two ways to implement animations in Android, Tween and flame, we still need to use tween after reading the materials, I wrote a tool class animutil, which calls the tool class to implement the view animation effect. In order to facilitate the update of the View Interface, animutil also integrates the self-handler. The specific reference code is as follows:
package com.vtion.sleb.banca.utils;import android.content.Context;import android.os.Handler;import android.view.View;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.AnimationUtils;import com.vtion.sleb.activities.R;public class AnimUtils extends Handler implements AnimationListener {private View view;private Animation anim_right;private Animation anim_left;public AnimUtils(View view, Context context) {anim_right = AnimationUtils.loadAnimation(context, R.anim.right_in);anim_left = AnimationUtils.loadAnimation(context, R.anim.left_out);anim_right.setAnimationListener(this);anim_right.setFillAfter(true);anim_left.setFillAfter(true);this.view = view;this.view.startAnimation(anim_right);}@Overridepublic void onAnimationEnd(Animation animation) {this.postDelayed(new Runnable() {@Overridepublic void run() {view.startAnimation(anim_left);}}, 2000);}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationStart(Animation animation) {System.out.println();System.out.println();}}
The anim folder contains the animation-related xml configuration files:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="700" android:fromXDelta="100%p" android:toXDelta="50%p" /> <alpha android:duration="700" android:fromAlpha="0.1" android:toAlpha="1.0" /></set>
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="700" android:fromXDelta="50%p" android:toXDelta="0" /> <alpha android:duration="700" android:fromAlpha="1.0" android:toAlpha="0.0" /></set>
Supplement:
Introduction to the attributes of animation configuration in XML files
ALPHA:Gradient transparency animation effect
Scale:Gradient scaling animation effect
Translate:Mobile animation effect
Rotate:Rotating animation effect
- <! --
- Fromxscale [float]: The scaling size on the X coordinate at the start of the animation. The value 0.0 indicates that the scale is reduced to none.
- Fromyscale [float]: The scaling size on the Y coordinate at the start of the animation. The value 0.0 indicates that the scale is reduced to none.
- 1.0 indicates normal scaling-free
- A value smaller than 1.0 indicates contraction.
- Value greater than 1.0 indicates Amplification
- Toxscale [float]: The scaling size on the X coordinate when the animation ends.
- Toyscale [float]: The scaling size on the X coordinate when the animation ends.
- Horizontal TX [float]: the starting position of the animation relative to the X coordinate of the object
- Interval ty [float]: the starting position of the animation relative to the X and Y coordinates of the object.
- 50,50%, 50% p. The three statements represent absolute, relative_to_self, and relative_to_parent respectively.
- Attribute value description: from 0%-100%, 50% is the midpoint position on the X or Y coordinate of the object.
- Fillafter [Boolean]: when it is set to true, the animation is applied after the animation ends.
- Startoffset [long]: The time interval between animations, starting from the time when the animation was stopped.
- Repeatcount [int]: Number of animation repetitions -->
Interpolator: Specifies an animation insertor to control the animation speed changes.
Fromalpha: Transparency 0.0 indicates full transparency 1.0 indicates full transparency at the beginning of the animation
The above value is a float data type number between 0.0 and 1.0.
Toalpha: transparency at animation end
Duration: Duration