目录
效果展示
代码展示
class AnimMenuActivity : AppCompatActivity(){
private var isOpen = false //菜单是否打开
private var menuArray = ArrayList<View>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_animmenu)
img_00.setOnClickListener {
opreaMenu()
}
menuArray.add(img_01)
menuArray.add(img_02)
menuArray.add(img_03)
menuArray.add(img_04)
menuArray.add(img_05)
menuArray.add(img_06)
for ((index,view) in menuArray.withIndex()){
view.setOnClickListener {
Toast.makeText(this@AnimMenuActivity,"我是$index",Toast.LENGTH_SHORT).show()
}
}
}
/**
* 操作菜单
*/
private fun opreaMenu() {
val openAnimator:ValueAnimator = if(isOpen){
isOpen = false
ValueAnimator.ofFloat(200f, 0f)
}else{
isOpen = true
ValueAnimator.ofFloat(0f, 200f)
}
openAnimator.duration = 600
openAnimator.addUpdateListener {
val animVal = it.animatedValue as Float
for ((index,view) in menuArray.withIndex()){
//位移
val degree = 360.0 / menuArray.size * index
//计算位置
val translateX = animVal * cos(Math.toRadians(degree))
val translateY = animVal * sin(Math.toRadians(degree))
view.translationX = translateX.toFloat()
view.translationY = translateY.toFloat()
//旋转
view.rotation = 360f * it.animatedFraction
//缩放
if(animVal > 0){
view.scaleX = animVal / 200f
view.scaleY = animVal / 200f
}
//透明
if(animVal > 0){
view.alpha = animVal / 200f
}
}
}
openAnimator.start()
}
}
难点公式
以(x,y)为中心,中心到顶点的距离为a,degree为旋转的角度,正N边形的顶点坐标公式为:
X=a * cos(degree)+x
Y=a * sin(degree)+y
这个公式是用来计算位移位置的,之前的一篇蛛网效果也主要用的是它(https://www.jianshu.com/p/26e5a2465fae)
网友评论