android 层级菜单,弹出菜单和多级菜单制作

一、弹出菜单

制作一个可以弹出的菜单,具体直接看效果吧~

a3c805a5283c

效果展示

具体实现:

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值