本文实例讲述了android编程实现仿优酷旋转菜单效果。分享给大家供大家参考,具体如下:
首先,看下效果:
不好意思,不会制作动态图片,只好上传静态的了,如果谁会,请教教我吧。
首先,看下xml文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
<?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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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; } }); } } |
应该注意到了:
看一下这个静态方法的实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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:设置动画时间。
再看一下退出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
// 图标的动画(出动画) 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程序设计有所帮助。