效果图
class CircleAnim(context: Context, attributeSet: AttributeSet? = null) :
RelativeLayout(context, attributeSet) {
init {
addView(context, 300, "#009ad6")
addView(context, 250, "#fab27b")
addView(context, 200, "#f15b6c")
addView(context, 150, "#6950a1")
addView(context, 100, "#45b97c")
scale(this)
}
private fun addView(context: Context, size: Int, colorStr: String): View {
val view = View(context)
view.background = getCircle(colorStr)
val layoutParams = LayoutParams(size, size)
layoutParams.addRule(CENTER_IN_PARENT)
addView(view, layoutParams)
return view
}
/**
* 动画: 先放大,再缩小,再放大,一直循环
*/
private fun scale(srcView: View) {
val animateTime = 2000L
val scaleDownAnimation = getScaleDownAnimation(animateTime)
val scaleUpAnimation = getScaleUpAnimation(animateTime)
scaleDownAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationEnd(animation: Animation) {
srcView.startAnimation(scaleUpAnimation)
}
override fun onAnimationStart(animation: Animation) {}
override fun onAnimationRepeat(animation: Animation) {}
})
scaleUpAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationEnd(animation: Animation) {
srcView.startAnimation(scaleDownAnimation)
}
override fun onAnimationStart(animation: Animation) {}
override fun onAnimationRepeat(animation: Animation) {}
})
srcView.startAnimation(scaleDownAnimation)
}
/**
* 放大动画
*/
private fun getScaleDownAnimation(animateTime: Long): ScaleAnimation {
val scaleDownAnimation = ScaleAnimation(
1f, 2f, 1f, 2f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
)
scaleDownAnimation.duration = animateTime
return scaleDownAnimation
}
/**
* 缩小动画
*/
private fun getScaleUpAnimation(animateTime: Long): ScaleAnimation {
val scaleUpAnimation = ScaleAnimation(
2f, 1f, 2f, 1f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
)
scaleUpAnimation.duration = animateTime
return scaleUpAnimation
}
/**
* 获取空心圆
*/
private fun getCircle(strokeColorStr: String) = GradientDrawable().also {
it.shape = GradientDrawable.OVAL
val strokeWidth = 2 // 边框宽度
val strokeColor = Color.parseColor(strokeColorStr) //边框颜色
it.setStroke(strokeWidth, strokeColor)
}
}
安卓开发入门教程系列汇总
安卓发展历程及前景
安卓发展历程 安卓开发前景展望
初探安卓安装开发工具 创建第一个安卓工程
开发语言学习Kotlin语言基础
UI控件学习系列UI控件_TextView UI控件_EditText UI控件_Button UI控件_ImageView UI控件_RadioButton UI控件_CheckBox UI控件_ProgressBar
关注头条号,第一时间获取最新文章: