Android Animation introduction-Animation implements loading Animation effect
It is not difficult to use Animation. Here is a brief introduction to the usage.
First look:
The effect is good. Let's take a look at the usage.
Animation effects are implemented through Animation. There are four types of Animation effects:
AlphaAnimation: gradient transparency Animation
ScaleAnimation: Size gradient Animation
TranslateAnimation: horizontal Animation
RotateAnimation: rotating Animation
So in order to achieve my performance. All our animations are used.
First, we add an ImageView and a TextView to the Activity layout file to center the layout.
Then modify MainActivity. java
First, declare the attribute to be declared.
private ImageView mImageView; private TextView mTextView; private AnimationSet mImageAni; private AnimationSet mTextAni;
Here, because both ImageView and TextView use a combination of animation effects, two containers, AnimationSet, are required to store the animation effects.
Let's take a look at the specific animation in the two containers.
TranslateAnimation ta = new TranslateAnimation(200,0,300,0); ta.setDuration(5000); RotateAnimation ra = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); ra.setDuration(5000); mImageAni.addAnimation(ta); mImageAni.addAnimation(ra);
MImageAni stores a TranslateAnimation, that is, the horizontal movement animation. Its parameter is from where to where, where it is from 200 --> 0,300 --> 0, and then calls setDuration () method To set the animation duration. The other is RotateAnimation, which is a rotation animation. Its parameters are from the number of degrees to the number of degrees (0 --> 360), the following parameters are based on itself, 50% is the center point rotation.
These two animation effects are combined to achieve the rotation effect of the small ball.
Next let's take a look at mTextAni.
ScaleAnimation sa = new ScaleAnimation(0,2.5f,0,2.5f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); sa.setDuration(5000); AlphaAnimation aa = new AlphaAnimation(0,1); aa.setDuration(5000); mTextAni.addAnimation(sa); mTextAni.addAnimation(aa);
The first animation effect is ScaleAnimation, that is, the scaling effect animation. Its parameters are similar to the translation, from the number to the number, and the subsequent parameters are also based on 50%, that is, the center.
The AlphaAnimation transparent animation parameter is relatively simple. Its parameter ranges from transparency to transparency, from disappearance to display.
Finally, we add a listener event to the ImageView to play the animation effect when you click it.
mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mImageView.startAnimation(mImageAni); mTextView.startAnimation(mTextAni); } });
Success! Try it now. If you are interested, you can also see other parameters and how to use xml for animation.