-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathActivityAnimation4.java
82 lines (75 loc) · 3.37 KB
/
ActivityAnimation4.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.xibei.personaldesign.itemDemo;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.Toast;
import com.xibei.personaldesign.R;
public class ActivityAnimation4 extends Activity implements View.OnClickListener {
Button btn_view_anmation, btn_value_animator, btn_repeat, btn_cancle;
View animation4_view;
ValueAnimator valueAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_animation_4);
animation4_view = findViewById(R.id.animation4_view);
btn_view_anmation = findViewById(R.id.btn_view_anmation);
btn_value_animator = findViewById(R.id.btn_value_animator);
btn_cancle = findViewById(R.id.btn_cancle);
btn_repeat = findViewById(R.id.btn_repeat);
btn_value_animator.setOnClickListener(this);
btn_repeat.setOnClickListener(this);
btn_cancle.setOnClickListener(this);
btn_view_anmation.setOnClickListener(this);
animation4_view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_view_anmation:
Toast.makeText(ActivityAnimation4.this, "执行完点击黑色方块看看有没有点击效果。", Toast.LENGTH_LONG).show();
TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 400f, 0.0f, 400f);
translateAnimation.setDuration(2000);
translateAnimation.setFillAfter(true);
animation4_view.startAnimation(translateAnimation);
break;
case R.id.btn_value_animator:
runAnimator(false);
Toast.makeText(ActivityAnimation4.this, "执行完点击黑色方块看看有没有点击效果。", Toast.LENGTH_LONG).show();
break;
case R.id.animation4_view:
Toast.makeText(ActivityAnimation4.this, "黑色方块被点击了!", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_cancle:
if (null != valueAnimator) {
valueAnimator.cancel();
}else {
Toast.makeText(ActivityAnimation4.this, "先执行Value Animator!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_repeat:
runAnimator(true);
break;
}
}
private void runAnimator(boolean repeat) {
valueAnimator = ValueAnimator.ofInt(0, 400, 20, 90, 10, 100);
valueAnimator.setDuration(1000);
if (repeat){
valueAnimator.setRepeatCount(3);
valueAnimator.setRepeatMode(ValueAnimator.RESTART);
}
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int currentValue = (int) animation.getAnimatedValue();
animation4_view.layout(currentValue, currentValue, currentValue + animation4_view.getWidth(),
currentValue + animation4_view.getHeight());
}
});
valueAnimator.start();
}
}