In-depth study and production of Android animation (1)

When it comes to android animation, we hardly need to think too much. You may immediately think of Animations in your mind. Maybe you can immediately type out TranslateAnimation, AlphaAnimation, SlaceAnimation, RotateAnimation and other animations with a few clicks on the keyboard. kind. Well, let's start with these simple animation classes! We have been using these animation classes all the time. I don’t know if you find that the absence of these animation classes is a bit of a headache, dizzy, and even painful, such as: TranslateAnimation, moving animation, simple example:
`/**
* Animation learning and production
* @author bliss
* {@linkplain}
* */
public class OtherActivity extends Activity {

private ImageView ivImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.other);
    ivImageView = (ImageView) findViewById(R.id.imageView1);

}

// 图片点击事件
public void ivClick(View view) {
    Toast.makeText(OtherActivity.this, "图片", Toast.LENGTH_LONG).show();
}

// 按钮点击事件
public void btnClick(View view) {

//Move animation, x and y move 200 each
TranslateAnimation translateAnimation = new TranslateAnimation(0, 200,
0, 200);
translateAnimation.setDuration(1000);
translateAnimation.setFillAfter(true);
ivImageView.startAnimation(translateAnimation);

}

}`
Before clicking the button
A very simple animation, the interface only puts an avatar and a button, and both set a click event, click the avatar before the button is clicked to pop up a Toast, when you click the button again,
After clicking the button
you find that the animation effect is achieved, at this time You go to click on the avatar again, the stupid thing happens, but the toast doesn't pop up. When you click on the position where the avatar doesn't move, the toast appears again, don't believe me, can you test it?
This is where Animation sucks. Well, simple solution: After moving, set a click event for the avatar.
So Animation is flawed, is there any other class that can solve this problem? Of course there is. Next, let's take a look at the simple and practical classes of Android animation:
1. Attribute animation class ObjectAnimator
ObjectAnimator source code
Usage:

// 按钮点击事件
    @SuppressLint("NewApi")
    public void btnClick(View view) {
        // TranslateAnimation translateAnimation = new TranslateAnimation(0,
        // 200,
        // 0, 200);
        // translateAnimation.setDuration(1000);
        // translateAnimation.setFillAfter(true);
        // ivImageView.startAnimation(translateAnimation);
        // x方向移动
        ObjectAnimator.ofFloat(ivImageView, "translationX", 0.0F, 360.0F)
                .setDuration(1000).start();
        // y方向移动
        ObjectAnimator.ofFloat(ivImageView, "translationY", 0.0F, 360.0F)
                .setDuration(1000).start();
        ;
    }

A simple two sentences of code can achieve the same effect, and you click on the picture again, and you will find that you have clicked Toast.
OK, let's move on to a little more complicated. . . . . . . . . . .
Persevere, persist, persist again, life is endless, struggle is endless - Lone Wolf

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325408879&siteId=291194637