Effect of Shaking left and right: (not so smooth here)
I. Continuous Broadcast(I don't know what the name should be, that is, playing animation a first and then playing animation B)
There are two methods.
FirstAnimation A and animation B respectively. Then, play animation a and set the animation listener of animation. When onanimationend is triggered (that is, a finishes PLAYING), start playing B.
animation1.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {animation2.start();}});
SecondWrite an animation set, in which animation a and animation B are defined, and set startoffset for animation B. The value is the time required for playing the previous animation.
For example, animation a is transparency from 0.1 to 1.0, and animation B is transparency from 1.0 to 0.1. You can see the entire change process using the animation set below.
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><alpha android:fromAlpha="0.2" android:toAlpha="1.0" android:duration="3000" /> <alpha android:startOffset="3000" android:fromAlpha="1.0" android:toAlpha="0.2" android:duration="3000" /></set>
Android: startoffset = "3000" indicates that the task is executed three seconds later. If Android: startoffset = "3000" is removed, you will not be able to see any effect. Because the two animations play simultaneously.
Ii. Loop
Sometimes, we may need to implement a function that keeps flashing images (such as the emergency alarm function in the weather forecast), or sometimes we need to shake the picture left and right, all of which need to be done through loop animation.
There are also two methods.
First, set two animations A and B. animation a is transparency 0-1, and animation B is 1-0. Then, listen to both animations, and animation a ends execution B, b. Execute .. infinite Loop...
Second, use the setrepeatcount and setrepeatmode of animation to implement an animation loop.
For example, flashing (Transparency bright-> dark, dark-> bright, so cycle)
// Flashing alphaanimation alphaanimation1 = new alphaanimation (0.1f, 1.0f); alphaanimation1.setduration (3000); alphaanimation1.setrepeatcount (animation. infinite); alphaanimation1.setrepeatmode (animation. reverse); IV. setanimation (alphaanimation1); alphaanimation1.start ();
Alphaanimation1.setrepeatcount (animation. Infinite); indicates repeated times. You can also set the number of repeated times, such as alphaanimation1.setrepeatcount (5 );
Alphaanimation1.setrepeatmode (animation. Reverse); indicates that the animation is executed after it is completed. This method has two types of values: restart and reverse. Restart indicates starting from the beginning, and reverse indicates playing back from the end.
The screen recording is too boring, similar to the following:
For example, swing left and right
// Swing translateanimation alphaanimation2 = new translateanimation (150f, 350f, 50, 50); alphaanimation2.setduration (1000); alphaanimation2.setrepeatcount (animation. infinite); alphaanimation2.setrepeatmode (animation. reverse); IV. setanimation (alphaanimation2); alphaanimation2.start ();
IV is an imageview.
Okay, so much.