目前,用户对安卓应用程序的UI设计要求越来越高,因此,掌握一些新颖的设计很有必要.
比如菜单,传统的菜单已经不能满足用户的需求. 其中优酷中圆盘旋转菜单的实现就比较优秀,这里我提供下我的思路及实现,仅供参考.
该菜单共分里外三层导航菜单.可以依次从外向里关闭三层菜单,也可以反向打开,并且伴有圆盘旋转的动画效果
首先,看下效果:
以下是具体的代码及解释:
1. 菜单布局文件:
大家看到主要有三个RalativeLayout,就是大家看到的三层,但是关于图片的倾斜 是怎样实现的呢?实际上是个假象,图片是正放的,里面图像是倾斜的
2. MainActivity;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ImageButton;
- import android.widget.RelativeLayout;
-
- public class MainActivity extends Activity {
-
- private ImageButton home;
- private ImageButton menu;
- private RelativeLayout level2;
- private RelativeLayout level3;
-
- private boolean isLevel2Show = true;
- private boolean isLevel3Show = true;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- home = (ImageButton) findViewById(R.id.home);
- menu = (ImageButton) findViewById(R.id.menu);
-
- level2 = (RelativeLayout) findViewById(R.id.level2);
- level3 = (RelativeLayout) findViewById(R.id.level3);
-
- menu.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- if(isLevel3Show){
-
- MyAnimation.startAnimationOUT(level3, 500, 0);
- }else {
-
- MyAnimation.startAnimationIN(level3, 500);
- }
-
- isLevel3Show = !isLevel3Show;
- }
- });
-
- home.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- if(!isLevel2Show){
-
- MyAnimation.startAnimationIN(level2, 500);
- } else {
- if(isLevel3Show){
-
- MyAnimation.startAnimationOUT(level3, 500, 0);
-
- MyAnimation.startAnimationOUT(level2, 500, 500);
- isLevel3Show = !isLevel3Show;
- }
- else {
-
- MyAnimation.startAnimationOUT(level2, 500, 0);
- }
- }
- isLevel2Show = !isLevel2Show;
- }
- });
-
- }
-
- }
3. 自定义动画类MyAnimation:
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.animation.Animation;
- import android.view.animation.Animation.AnimationListener;
- import android.view.animation.RotateAnimation;
-
- public class MyAnimation {
-
- public static void startAnimationIN(ViewGroup viewGroup, int duration){
- for(int i = 0; i < viewGroup.getChildCount(); i++ ){
- viewGroup.getChildAt(i).setVisibility(View.VISIBLE);
- viewGroup.getChildAt(i).setFocusable(true);
- viewGroup.getChildAt(i).setClickable(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(duration);
-
- viewGroup.startAnimation(animation);
-
- }
-
-
- public static void startAnimationOUT(final ViewGroup viewGroup, int duration , 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(duration);
- animation.setStartOffset(startOffSet);
- animation.setAnimationListener(new AnimationListener() {
-
- @Override
- public void onAnimationStart(Animation animation) {
-
-
- }
-
- @Override
- public void onAnimationRepeat(Animation animation) {
-
-
- }
-
- @Override
- public void onAnimationEnd(Animation animation) {
- for(int i = 0; i < viewGroup.getChildCount(); i++ ){
- viewGroup.getChildAt(i).setVisibility(View.GONE);
- viewGroup.getChildAt(i).setFocusable(false);
- viewGroup.getChildAt(i).setClickable(false);
- }
-
- }
- });
-
- viewGroup.startAnimation(animation);
- }
- }
这样,一个高仿优酷三级导航圆盘旋转菜单就完成了.,以后完全可以借鉴这些优秀的UI设计,甚至根据新的需求,可以做出更好的UI.