温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Android中实现一个圆盘旋转菜单效果

发布时间:2021-03-01 15:57:43 来源:亿速云 阅读:302 作者:戴恩恩 栏目:移动开发

本文章向大家介绍如何在Android中实现一个圆盘旋转菜单效果的基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

1. 菜单布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >
  <RelativeLayout
    android:layout_width="100dip"
    android:layout_height="50dip"
    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_centerInParent="true"
      android:background="@drawable/icon_home" />
  </RelativeLayout>
  <RelativeLayout
    android:layout_width="180dip"
    android:layout_height="90dip"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/level2"
    android:background="@drawable/level2" >
    <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/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/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:layout_width="280dip"
    android:layout_height="140dip"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/level3"
    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="6dip"
      android:layout_marginLeft="8dip"
      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="6dip"
      android:layout_marginRight="8dip"
      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_marginBottom="12dip"
      android:layout_marginRight="28dip"
      android:layout_alignParentRight="true"
      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_marginBottom="6dip"
      android:layout_marginRight="12dip"
      android:layout_alignParentRight="true"
      android:background="@drawable/channel7" />
  </RelativeLayout>
</RelativeLayout>

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){
          //隐藏3级导航菜单
          MyAnimation.startAnimationOUT(level3, 500, 0);
        }else {
          //显示3级导航菜单
          MyAnimation.startAnimationIN(level3, 500);
        }
        isLevel3Show = !isLevel3Show;
      }
    });
    home.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if(!isLevel2Show){
          //显示2级导航菜单
          MyAnimation.startAnimationIN(level2, 500);
        } else {
          if(isLevel3Show){
            //隐藏3级导航菜单
            MyAnimation.startAnimationOUT(level3, 500, 0);
            //隐藏2级导航菜单
            MyAnimation.startAnimationOUT(level2, 500, 500);
            isLevel3Show = !isLevel3Show;
          }
          else {
            //隐藏2级导航菜单
            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;
    /**
     * 旋转动画
     * RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue)
     * fromDegrees 开始旋转角度
     * toDegrees 旋转到的角度
     * pivotXType X轴 参照物
     * pivotXValue x轴 旋转的参考点
     * pivotYType Y轴 参照物
     * pivotYValue Y轴 旋转的参考点
     */
    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) {
        // TODO Auto-generated method stub
      }
      @Override
      public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub
      }
      @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);
  }
}

以上就是小编为大家带来的如何在Android中实现一个圆盘旋转菜单效果的全部内容了,希望大家多多支持亿速云!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI