Android编程实现仿优酷旋转菜单效果(附demo源码)
作者:傲慢的上校 发布时间:2022-03-06 11:25:32
本文实例讲述了Android编程实现仿优酷旋转菜单效果。分享给大家供大家参考,具体如下:
首先,看下效果:
不好意思,不会制作动态图片,只好上传静态的了,如果谁会,请教教我吧。
首先,看下xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#c9c9c9" >
<RelativeLayout
android:id="@+id/relate_level3"
android:layout_width="280dp"
android:layout_height="140dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level3" >
<ImageButton
android:id="@+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="6dip"
android:layout_marginLeft="12dip"
android:background="@drawable/channel1" />
<ImageButton
android:id="@+id/c2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/c1"
android:layout_marginBottom="12dip"
android:layout_marginLeft="28dip"
android:background="@drawable/channel2" />
<ImageButton
android:id="@+id/c3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/c2"
android:layout_marginBottom="8dip"
android:layout_marginLeft="6dip"
android:layout_toRightOf="@+id/c2"
android:background="@drawable/channel3" />
<ImageButton
android:id="@+id/c4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="6dip"
android:background="@drawable/channel4" />
<ImageButton
android:id="@+id/c5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/c6"
android:layout_marginBottom="8dip"
android:layout_marginRight="6dip"
android:layout_toLeftOf="@+id/c6"
android:background="@drawable/channel5" />
<ImageButton
android:id="@+id/c6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/c7"
android:layout_alignParentRight="true"
android:layout_marginBottom="12dip"
android:layout_marginRight="28dip"
android:background="@drawable/channel6" />
<ImageButton
android:id="@+id/c7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="6dip"
android:layout_marginRight="12dip"
android:background="@drawable/channel7" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/relate_level2"
android:layout_width="180dp"
android:layout_height="90dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level2" >
<ImageButton
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="6dip"
android:background="@drawable/icon_menu" />
<ImageButton
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dip"
android:background="@drawable/icon_search" />
<ImageButton
android:id="@+id/myyouku"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dip"
android:background="@drawable/icon_myyouku" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/relate_level1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/level1" >
<ImageButton
android:id="@+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:background="@drawable/icon_home" />
</RelativeLayout>
</RelativeLayout>
大家看到主要有三个RalativeLayout,就是大家看到的三层,但是关于图片的倾斜 是怎样实现的呢?实际上是个假象,图片是正放的,里面图像是倾斜的。如下图:
这样大概能明白,下面就是开始动画效果了,先看下主Activity:
public class TestYoukuActivity extends Activity {
/** Called when the activity is first created. */
private boolean areLevel2Showing = true, areLevel3Showing = true;
private RelativeLayout relate_level2, relate_level3;
private ImageButton home, menu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
setListener();
}
private void findViews() {
relate_level2 = (RelativeLayout) findViewById(R.id.relate_level2);
relate_level3 = (RelativeLayout) findViewById(R.id.relate_level3);
home = (ImageButton) findViewById(R.id.home);
menu = (ImageButton) findViewById(R.id.menu);
}
private void setListener() {
// 给大按钮设置点击事件
home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!areLevel2Showing) {
MyAnimation.startAnimationsIn(relate_level2, 500);
} else {
if (areLevel3Showing) {
MyAnimation.startAnimationsOut(relate_level2, 500, 500);
MyAnimation.startAnimationsOut(relate_level3, 500, 0);
areLevel3Showing = !areLevel3Showing;
} else {
MyAnimation.startAnimationsOut(relate_level2, 500, 0);
}
}
areLevel2Showing = !areLevel2Showing;
}
});
menu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!areLevel3Showing) {
MyAnimation.startAnimationsIn(relate_level3, 500);
} else {
MyAnimation.startAnimationsOut(relate_level3, 500, 0);
}
areLevel3Showing = !areLevel3Showing;
}
});
}
}
应该注意到了:
MyAnimation.startAnimationsIn(relate_level2, 500);
看一下这个静态方法的实现:
public static void startAnimationsIn(ViewGroup viewgroup, int durationMillis) {
viewgroup.setVisibility(0);
for (int i = 0; i < viewgroup.getChildCount(); i++) {
viewgroup.getChildAt(i).setVisibility(0);
viewgroup.getChildAt(i).setClickable(true);
viewgroup.getChildAt(i).setFocusable(true);
}
Animation animation;
animation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
animation.setFillAfter(true);
animation.setDuration(durationMillis);
viewgroup.startAnimation(animation);
}
RotateAnimation是画面转移旋转动画效果,看一下它的构造方法:
RotateAnimation(Context context, AttributeSet attrs)
Constructor used when a RotateAnimation is loaded from a resource.
RotateAnimation(float fromDegrees, float toDegrees)
Constructor to use when building a RotateAnimation from code.
RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
Constructor to use when building a RotateAnimation from code
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
Constructor to use when building a RotateAnimation from code
在这里使用的是第四个构造方法:
fromDegrees:旋转的开始角度。
toDegrees:旋转的结束角度。
pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
pivotXValue:X坐标的伸缩值。
pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
pivotYValue:Y坐标的伸缩值。
关于角度问题:
当角度为负数——表示逆时针旋转
当角度为正数——表示顺时针旋转
(负数from——to正数:顺时针旋转)
(负数from——to负数:逆时针旋转)
(正数from——to正数:顺时针旋转)
(正数from——to负数:逆时针旋转)
关于pivotXValue:这一点的X坐标的对象被旋转,在指定的绝对数字0是左边边缘。如果pivotXType数是绝对的这个值可以是一个绝对,另外也可以是百分比(在1.0为100%)。50%是x中点,100%为右边缘。
同理,pivotYValue:这一点的Y坐标的对象被旋转,在指定的绝对数字0是顶部边缘。如果pivotYType数是绝对的这个值可以是一个绝对,另外也可以是百分比(在1.0为100%)。50%是Y中点,100%为下边缘。
然后再看下调用的其他的方法:
setFillAfter:
If fillAfter is true, the transformation that this animation performed will persist when it is finished. Defaults to false if not set. Note that this applies when using an AnimationSet to chain animations. The transformation is not applied before the AnimationSet itself starts.
如果fillAfter为真,transformation 动画将一直运行直到它完成。默认设置为假。注意:这适用于当使用一个AnimationSet连锁动画。transformation 是不适用AnimationSet本身之前开始。
setDuration:设置动画时间。
再看一下退出:
// 图标的动画(出动画)
public static void startAnimationsOut(final ViewGroup viewgroup,
int durationMillis, int startOffset) {
Animation animation;
animation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
animation.setFillAfter(true);
animation.setDuration(durationMillis);
animation.setStartOffset(startOffset);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {}
@Override
public void onAnimationRepeat(Animation arg0) {}
@Override
public void onAnimationEnd(Animation arg0) {
viewgroup.setVisibility(8);
for (int i = 0; i < viewgroup.getChildCount(); i++) {
viewgroup.getChildAt(i).setVisibility(8);
viewgroup.getChildAt(i).setClickable(false);
viewgroup.getChildAt(i).setFocusable(false);
}
}
});
viewgroup.startAnimation(animation);
}
有一个animation.setStartOffset(startOffset);是设置animation多长时间以后执行。
最后:代码下载地址:
此处本站下载。
希望本文所述对大家Android程序设计有所帮助。


猜你喜欢
- 企业级项目开发中都会有文件、图片、视频等文件上传并能够访问的场景,对于初学者Demo可能会直接存储在应用服务器上;对于传统项目可能会单独搭建
- 先贴代码,后面做一些简单说明:public static string sendPostHttpRequest_2(string url,
- 原因分析@Anysc注解会开启一个新的线程,主线程的Request和子线程是不共享的,所以获取为null在使用springboot的自定带的
- 最近想关闭一个包的日志打印,经过一番研究实际上就一句话的事,一直没成功是因为name写错了。<logger name="pa
- 自定义View,1. 自定义一个Runnable线程TouchEventCountThread , 用来统计500ms内的点击次
- 1.建议设置窗体为双缓冲绘图,可有效避免界面刷时引起的闪烁this.SetStyle(ControlStyles.AllPaintingIn
- 示例我们先来以这样一个场景引入: 在电脑城装机总有这样的经历。我们到了店里,先会有一个销售人员来询问你希望装的机器是怎么样的配置,
- Java中的final关键字1、修饰类的成员变量 这是final的主要用途之一,和C/C++的const,即该成员被修饰为常量,意味着不可修
- 本文实例讲述了Android实现学生管理系统,分享给大家供大家参考。具体如下:(1)管理系统实现的功能主要是:学生、教师的注册登录,和选课,
- HashMap 在不同的 JDK 版本下的实现是不同的,在 JDK 1.7 时,HashMap 底层是通过数组 + 链表实现的;而在 JDK
- 本文实例讲述了Java编程中文件读写的方法。分享给大家供大家参考,具体如下:Java中文件读写操作的作用是什么?回答这个问题时应该先想到的是
- Java继承方法重写是Java语言多态的特性,必须满足以下条件在子类中,方法名称与父类方法名称完全相同方法的参数个数和类型完全相同,返回类型
- 为什么Android要申请权限简单说下在Android6.0及6.0以上一些google认为涉及“危险和用户隐私”的一些权限不仅要做清单文件
- 1、前言随着技术的发展,微信的一系列服务渗透进了我们的生活,但是我们应该怎样进行微信方面的开发呢。相信很多的小伙伴们都很渴望知道吧。这篇文章
- 资源服务器就是业务服务 如用户服务,订单服务等 第三方需要到资源服务器调用接口获取资源ResourceServerConfigResourc
- 前言我们在搭建完集群环境后,不得不考虑的一个问题就是用户访问产生的session如何处理。session的处理有很多种方法,详情见转载的上篇
- 引言:前面几个专题对委托进行了详细的介绍的,然后我们在编写代码过程中经常会听到“事件”这个概念的,尤其是写UI的时候,当我们点击一个按钮后V
- 公司有一个需求,实现一个多级的树形菜单,并且支持多选功能,实现这个功能之前,我在网上找了找,树形菜单很好找,但是支持多选功能并没有很合适的,
- 前言Springt通过任务执行器(TaskExecutor)来实现多线程和并发编程。使用ThreadPoolTaskExecutor可实现一
- 下面给大家介绍spring不能注入static变量的原因,具体详情如下所示:Spring 依赖注入 是依赖 set方法set方法是 是普通的