弹射状菜单

xiaoxiao2025-10-09  22

声明:转载至:https://blog.csdn.net/qq_36510659/article/details/78834302

感觉原创作者写的杠杠的,然后就偷偷摸摸的弄过来了,稍微充实一下自己的博客,顺便,再给大家稍微详细那么一点吧

先放个效果图,瞅瞅

怎么样,很赞吧,是不是很炫

好了。咋们还是由简易到。。繁琐

先来个平平的。

废话不多说,上代码。(人家原创的代码哦,我尽可能的在自己的理解上给大家注释一下子)

首先是xml布局,不多说:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_margin="10dp" android:text="按钮" /> <TextView android:id="@+id/tv6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_margin="15dp" android:background="#da7979" android:padding="5dp" android:text="洒" android:textColor="#ffffff" android:visibility="gone" /> </RelativeLayout>

好了。要开始正式内容了,准备好了吗?

点击事件:也就只有一个button:

btn2.setOnClickListener(this);

内容:

@Override public void onClick(View v) { switch (v.getId()) { case R.id.btn2: if (!LockMenu) { LockMenu = true; openAnim(tv6, 0, 1, 400); } else { LockMenu = false; closeAnim(tv6, 0, 1, 400); } break; } }

这里的LockMenu用来判断开关是否打开:可以先定义:

private boolean LockMenu = false;

openAnim()呢是打开的方法:

/** * 打开菜单 * view:动画控件 * index:第几个控件 * num:有几个控件 * radius:扇形半径 */ public void openAnim(View view, int index, int num, int radius) { if (view.getVisibility() != View.VISIBLE) { view.setVisibility(View.VISIBLE); } double angle = Math.toRadians(180) / (num - 1) * index; int translationX = -(int) (radius * Math.cos(angle)); int translationY = -(int) (radius * Math.sin(angle)); /** * ObjectAnimator animator = ObjectAnimator.ofFloat(tv1, "translationX", 0, -300); * ObjectAnimator animator = ObjectAnimator.ofFloat("执行动画的控件名字", "执行什么样的动画","开始位置", "结束位置"); */ ObjectAnimator one = ObjectAnimator.ofFloat(view, "translationX", 0, translationX); ObjectAnimator two = ObjectAnimator.ofFloat(view, "translationY", 0, translationY); ObjectAnimator three = ObjectAnimator.ofFloat(view, "rotation", 0, 360); ObjectAnimator four = ObjectAnimator.ofFloat(view, "scaleX", 0f, 1f); ObjectAnimator five = ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f); ObjectAnimator six = ObjectAnimator.ofFloat(view, "alpha", 0f, 1); /** * translationX:X方向移动 * translationY:Y方向移动 * rotation:中心点旋转(rotationX:围绕X轴旋转,rotationY:围绕Y轴旋转) * scaleX:X轴方向缩放 * scaleY:Y轴方向缩放 * alpha:渐变 */ AnimatorSet set = new AnimatorSet(); set.setDuration(2000);//时间 /** * set.playTogether:所有动画一起播放 * set.playSequentially:依次播放 */ set.playTogether(one, two, three, four, five, six); // set.playSequentially(one, two, three, four, five, six); //回弹效果 // set.setInterpolator(new BounceInterpolator()); set.start(); }

注释都放里边了哈。。如果看不清。可以粘贴出来看,嗯(。。粘了可以直接用。很神奇)

接着是closeAnim()关闭的方法:

//关闭菜单 public void closeAnim(View view, int index, int num, int radius) { if (view.getVisibility() != View.VISIBLE) { view.setVisibility(View.VISIBLE); } double angle = Math.toRadians(180) / (num - 1) * index; Log.e("angle", angle + ""); int translationX = -(int) (radius * Math.cos(angle)); int translationY = -(int) (radius * Math.sin(angle)); ObjectAnimator one = ObjectAnimator.ofFloat(view, "translationX", translationX, 0); ObjectAnimator two = ObjectAnimator.ofFloat(view, "translationY", translationY, 0); ObjectAnimator three = ObjectAnimator.ofFloat(view, "rotation", 0, 360); ObjectAnimator four = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0f); ObjectAnimator five = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0f); ObjectAnimator six = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f); AnimatorSet set = new AnimatorSet(); set.playTogether(one, two, three, four, five, six); set.setDuration(2000); //回弹效果 // set.setInterpolator(new BounceInterpolator()); set.start(); }

跟打开的方法差不多哦。看懂那个,这个也就懂了,,啊哈哈

此外。。疑难解答。原创里就有哦。我。。再粘一遍:

先看看点击事件里的这个:

openAnim(tv6, 0, 1, 400);

其实,它是这样的:

openAnim("你的控件名儿,这里是个TextView", "这个是index,下面给你解释哦", "这个是要变化控件的个数,好像是可以随便写,反正我写了0-9都没错。。", "最后这个是radius,也是下面解释");

好了,接下来是:角度转化为弧度、也顺便说说上面的index

double angle = Math.toRadians(180) / (num - 1) * index;

好了,来,关门!放图。。。

虽然,我也没怎么看懂吧,但大致能看清那个数字的大致方位了。。。

嗯。好了。下一个:

计算X,Y的距离、还有上面的radius。。

int translationX = -(int) (radius * Math.cos(angle)); int translationY = -(int) (radius * Math.sin(angle));

好。又来了。关门,,放。。。狗,,,图。。

大家伙儿,能看懂的不?。。。老实说。我也没看懂,反正就是,数值越大,距离越远啦。凑合凑合用吧。哈哈哈。。

小白出门。。刚刚起步。请多多指教。多谢多谢多谢多谢。。。(省略一万多谢)。

转载请注明原文地址: https://www.6miu.com/read-5037599.html

最新回复(0)