菜单

简介: 引用:http://my.eoe.cn/blue_rain/archive/477.html 首先,看下效果: 首先,看下xml文件: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

引用:http://my.eoe.cn/blue_rain/archive/477.html

首先,看下效果:
111111.jpg
222222.jpg

首先,看下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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?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,就是大家看到的三层,但是关于图片的倾斜 是怎样实现的呢?实际上是个假象,图片是正放的,里面图像是倾斜的。如下图:
33333.png
这样大概能明白,下面就是开始动画效果了,先看下主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
51
52
53
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
MyAnimation.startAnimationsIn(relate_level2, 500);

看一下这个静态方法的实现:

 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坐标的伸缩值。

关于角度问题:

1
2
3
4
5
6
当角度为负数——表示逆时针旋转
                     当角度为正数——表示顺时针旋转              
                     (负数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
27
// 图标的动画(出动画)
    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多长时间以后执行。

源码下载地址:http://dl.vmall.com/c0hkh1m6kw

相关文章
|
17天前
Qt实现的多菜单选择界面
Qt实现的多菜单选择界面
18 0
|
6月前
09EasyUI 菜单与按钮- 创建简单的菜单
09EasyUI 菜单与按钮- 创建简单的菜单
15 0
Sidebar 左右菜单的使用
Sidebar 左右菜单的使用
87 0
|
索引 开发工具 Windows
|
区块链 Python 数据格式
Meclipse 10 设置 new 菜单
第一步 第二步 选择 file-&gt;new-&gt;里面的选项
1091 0