Android编程实现仿优酷旋转菜单效果(附demo源码)

所属分类: 软件编程 / Android 阅读数: 1628
收藏 0 赞 0 分享

本文实例讲述了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程序设计有所帮助。

更多精彩内容其他人还在看

老生常谈Android HapticFeedback(震动反馈)

下面小编就为大家带来一篇老生常谈Android HapticFeedback(震动反馈)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详谈OnTouchListener与OnGestureListener的区别

下面小编就为大家带来一篇详谈OnTouchListener与OnGestureListener的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿知乎悬浮功能按钮FloatingActionButton效果

前段时间在看属性动画,恰巧这个按钮的效果可以用属性动画实现,下面通过本文给大家分享adroid仿知乎悬浮功能按钮FloatingActionButton效果,需要的朋友参考下吧
收藏 0 赞 0 分享

解决Android V7后自定义Toolbar、ActionBar左侧有空白问题

这篇文章主要介绍的Android V7后自定义Toolbar、ActionBar左侧有空白问题的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android常见控件使用详解

这篇文章主要为大家详细介绍了Android常见控件的使用方法,包括ProgressBar进度条控件、AlertDialog对话框控件等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现简洁的APP更新dialog数字进度条

这篇文章主要为大家详细介绍了Android实现简洁的APP更新dialog数字进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 判断当前语言环境是否是中文环境

本文主要介绍了Android 判断当前语言环境是否是中文环境的方法。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详谈Android中Matrix的set、pre、post的区别

下面小编就为大家带来一篇详谈Android中Matrix的set、pre、post的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现登录界面记住密码的存储

这篇文章主要为大家详细介绍了Android SharedPreferrences实现登录界面记住密码的存储,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 使用SharedPreferrences储存密码登录界面记住密码功能

Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences, 其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入,下面通过实例代码给大家讲解下,需要的朋友参考下吧
收藏 0 赞 0 分享
查看更多