简单的补间动画Animation和属性动画ObjectAnimator
2018-01-10 09:33
369 查看
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Mycircle ci;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ci = findViewById(R.id.ci);
Button tran = findViewById(R.id.tran);
Button rotate = findViewById(R.id.rotate);
Button scale = findViewById(R.id.scale);
Button alpha = findViewById(R.id.alpha);
Button zh = findViewById(R.id.zh);
Button py = findViewById(R.id.py);
tran.setOnClickListener(this);
rotate.setOnClickListener(this);
scale.setOnClickListener(this);
alpha.setOnClickListener(this);
zh.setOnClickListener(this);
py.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tran:
//平移动画 补间动画
TranslateAnimation trans = new TranslateAnimation(0, 500, 0, 500);
trans.setDuration(3000);
ci.startAnimation(trans);
break;
case R.id.scale:
//缩放
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 2, 0, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
ci.startAnimation(scaleAnimation);
break;
case R.id.rotate:
//旋转
Animation rotateAnimation = new RotateAnimation(0, 270, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
ci.startAnimation(rotateAnimation);
break;
case R.id.alpha:
//透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(3000);
ci.startAnimation(alphaAnimation);
break;
case R.id.py:
//属性动画
float tr = ci.getTranslationX();
ObjectAnimator translationX = ObjectAnimator.ofFloat(ci, "translationX", 300f);
translationX.setDuration(3000);
translationX.start();
break;
case R.id.zh:
//平移 属性动画
//TranslateAnimation
ObjectAnimator tranX = ObjectAnimator.ofFloat(ci, "translationX", 0, 300);
//创建透明度动画
ObjectAnimator alpha = ObjectAnimator.ofFloat(ci, "alpha", 1.0f, 0f);
//旋转
ObjectAnimator rotation = ObjectAnimator.ofFloat(ci, "rotation", 0f, 360f);
//缩放
ObjectAnimator scaleY = ObjectAnimator.ofFloat(ci, "scaleY", 1f, 3f);
//动画集合
AnimatorSet set = new AnimatorSet();
//添加动画
set.play(rotation).with(alpha).after(tranX);//可选
//设置时间等
set.setDuration(5000);
set.start();
//动画监听
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
ci.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animator animation) {
super.onAnimationRepeat(animation);
}
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
}
});
break;
}
}
}
private Mycircle ci;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ci = findViewById(R.id.ci);
Button tran = findViewById(R.id.tran);
Button rotate = findViewById(R.id.rotate);
Button scale = findViewById(R.id.scale);
Button alpha = findViewById(R.id.alpha);
Button zh = findViewById(R.id.zh);
Button py = findViewById(R.id.py);
tran.setOnClickListener(this);
rotate.setOnClickListener(this);
scale.setOnClickListener(this);
alpha.setOnClickListener(this);
zh.setOnClickListener(this);
py.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tran:
//平移动画 补间动画
TranslateAnimation trans = new TranslateAnimation(0, 500, 0, 500);
trans.setDuration(3000);
ci.startAnimation(trans);
break;
case R.id.scale:
//缩放
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 2, 0, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
ci.startAnimation(scaleAnimation);
break;
case R.id.rotate:
//旋转
Animation rotateAnimation = new RotateAnimation(0, 270, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
ci.startAnimation(rotateAnimation);
break;
case R.id.alpha:
//透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(3000);
ci.startAnimation(alphaAnimation);
break;
case R.id.py:
//属性动画
float tr = ci.getTranslationX();
ObjectAnimator translationX = ObjectAnimator.ofFloat(ci, "translationX", 300f);
translationX.setDuration(3000);
translationX.start();
break;
case R.id.zh:
//平移 属性动画
//TranslateAnimation
ObjectAnimator tranX = ObjectAnimator.ofFloat(ci, "translationX", 0, 300);
//创建透明度动画
ObjectAnimator alpha = ObjectAnimator.ofFloat(ci, "alpha", 1.0f, 0f);
//旋转
ObjectAnimator rotation = ObjectAnimator.ofFloat(ci, "rotation", 0f, 360f);
//缩放
ObjectAnimator scaleY = ObjectAnimator.ofFloat(ci, "scaleY", 1f, 3f);
//动画集合
AnimatorSet set = new AnimatorSet();
//添加动画
set.play(rotation).with(alpha).after(tranX);//可选
//设置时间等
set.setDuration(5000);
set.start();
//动画监听
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
ci.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animator animation) {
super.onAnimationRepeat(animation);
}
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
}
});
break;
}
}
}
相关文章推荐
- 简单的补间动画Animation和属性动画ObjectAnimator加页面跳转动画
- 帧动画(FrameAnimation) 补间动画(TweeAnimation) 属性动画(ObjectAnimation)
- Android属性动画ObjectAnimator源码简单分析
- Android Animation、ObjectAnimator与ValueAnimator实现视图动画和属性动画
- 逐帧动画 补间动画 属性动画ObjectAnimator ValueAnimator AnimatorSet)演示 TypeEvaluator
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Android属性动画完全解析 (ValueAnimator,ObjectAnimator)(上)
- Android属性动画完全解析(二)-ValueAnimator和ObjectAnimator的高级用法
- Android-Animator属性动画( ObjectAnimator , AnimatorSet , ValueAnimator )
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- 属性动画之ObjectAnimator
- Android属性动画AnimatorSet源码简单分析
- 安卓属性动画PropertyValuesHolder、ObjectAnimator
- 属性动画详解(ValueAnimator,ObjectAnimator,Animator监听器,XML编写动)
- 【转】android 属性动画之 ObjectAnimator
- 属性动画ObjectAnimator
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- PropertyAnimation属性动画 ObjectAnimator三种方式实现
- 安卓动画全解:补间动画(视图动画)、布局动画、属性动画、逐帧动画。动画Animation属性、Alpha属性、Scale属性、Translate属性、Rotate属性,动画集AnimationSet