-
Notifications
You must be signed in to change notification settings - Fork 2
/
Practice05AnimatorSetLayout.java
57 lines (45 loc) · 1.99 KB
/
Practice05AnimatorSetLayout.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.geekholt.practiceui.view.lesson6;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import com.geekholt.practiceui.R;
public class Practice05AnimatorSetLayout extends RelativeLayout {
View view;
Button animateBt;
public Practice05AnimatorSetLayout(Context context) {
super(context);
}
public Practice05AnimatorSetLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Practice05AnimatorSetLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
view = findViewById(R.id.objectAnimatorView);
animateBt = (Button) findViewById(R.id.animateBt);
animateBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
view.setTranslationX(-200f);
ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "alpha", 0, 1);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "translationX", -200, 200);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, "rotation", 0, 1080);
animator3.setDuration(1000);
AnimatorSet animatorSet = new AnimatorSet();
// 用 AnimatorSet 的方法来让三个动画协作执行
// 要求 1: animator1 先执行,animator2 在 animator1 完成后立即开始
// 要求 2: animator2 和 animator3 同时开始
animatorSet.play(animator1).before(animator2); // 先执行 1 再执行 2
animatorSet.playTogether(animator2, animator3); // 2 和 3 同时开始
animatorSet.start();
}
});
}
}