一、弹出菜单
制作一个可以弹出的菜单,具体直接看效果吧~
效果展示
具体实现:
1,在XML文件里面添加图片按钮
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/id_d"
style="@style/ManuBtnStyle"
android:src="@drawable/d"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/id_c"
style="@style/ManuBtnStyle"
android:src="@drawable/c"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/id_b"
style="@style/ManuBtnStyle"
android:src="@drawable/b"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/id_a"
android:background="@null"
style="@style/ManuBtnStyle"
android:src="@drawable/a"/>
2,为按钮设置样式,在values文件中
@null
true
true
3,添加动画
(1),定义数组保存所有的动画按钮的资源ID
private int[] resld = {R.id.id_b,R.id.id_c,R.id.id_d};
(2),获取菜单按钮的状态,并为菜单按钮添加点击事件,isOpen记录按钮菜单的是否是打开状态
private void initView(){
//给菜单添加点击事件
ImageButton menu = findViewById(R.id.id_a);
menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//遍历数组,取出每一个按钮
for(int i = 0;i < resld.length;i++){
//判断是打开 还是关闭
if(isOpen == true){
//之前是打开 ,现在需要关闭
close(i);
}else{
//之间是关闭,现在需要打开
open(i);
}
}
isOpen = !isOpen;
}
});
}
(3),定义方法封装菜单的open和close动画
public void close(int i){
animate(i,true);
}
public void open(int i){
animate(i,false);
}
(4),实现动画,使用AnimationSet管理多个动画
public void animate(int i,boolean state){
//计算菜单平分之后的夹角
double angle = (Math.PI/(resld.length+1));
//获取id对应的控件
ImageButton imageButton = findViewById(resld[i]);
//计算当前控制对应控件的角度
double mAngle = (i+1) * angle;
//计算x距离
float x = (float)(Math.cos(mAngle) * 400);
//计算y距离
float y = (float)(Math.sin(mAngle) * 400);
float startx;