Android之Kotlin实现TweenAnimaion
2017-08-23 本文已影响0人
162f127842fc
补间动画(TweenAnimaion)四种常用实现
1. AlphaAnimation(透明),实现
findViewById(R.id.AlphaAnimation).setOnClickListener {
var alphaAnimation = AlphaAnimation(1.0f, 0.0f)//从全不透明变为全透明
// 3: 确定持续时间
alphaAnimation.duration = 2000;
// 4: 确定Interpolator
alphaAnimation.interpolator = LinearInterpolator()
ivSport.startAnimation(alphaAnimation)
}
2.RotateAnimation(旋转),实现
findViewById(R.id.RotateAnimation).setOnClickListener {
var rotateAnimation = RotateAnimation(0f, 70f)
rotateAnimation.duration = 2000;
rotateAnimation.interpolator = AccelerateDecelerateInterpolator()
ivSport.startAnimation(rotateAnimation)
}
3.ScaleAnimation(放大缩小)动画实现
findViewById(R.id.ScaleAnimation).setOnClickListener {
//放大五倍
var scaleAnimation = ScaleAnimation(0f, 5f, 0f, 5f)
scaleAnimation.duration = 2000; //显示时长
scaleAnimation.interpolator = AccelerateDecelerateInterpolator()//动画显示规律
ivSport.startAnimation(scaleAnimation)
}
4.ScaleAnimation(位移)动画实现
findViewById(R.id.TranslateAnimation).setOnClickListener {
var transAnimation = TranslateAnimation(-400f, 0f, 0f, 0f)
transAnimation.duration = 2000;
transAnimation.interpolator = AccelerateDecelerateInterpolator()
ivSport.startAnimation(transAnimation)
}
5.补间动画组合使用
val mul = findViewById(R.id.MulAnimation);
mul.setOnClickListener {
var scaleAnimation = ScaleAnimation(1f, 0.5f, 1f, 0.5f)
var transAnimation = TranslateAnimation(0f, ivSport.x - mul.width / 2, 0f, mul.y - ivSport.y + mul.height)
var alphaAnimation = AlphaAnimation(1.0f, 0.0f)//从全不透明变为全透明
ivSport.startAnimation(alphaAnimation)
val aniSet = AnimationSet(false)
aniSet.duration=2000
aniSet.interpolator= LinearInterpolator()
aniSet.addAnimation(scaleAnimation)
aniSet.addAnimation(transAnimation)
aniSet.addAnimation(alphaAnimation)
ivSport.startAnimation(aniSet)
}