属性动画实现的扇形菜单效果,超级简单。直接上图上干货。



代码MainActivity:

package com.daidai.sectormenu;import java.util.ArrayList;import java.util.List;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener{private int[] res = {R.id.iv_open, R.id.iv_camera, R.id.iv_music, R.id.iv_place, R.id.iv_sleep, R.id.iv_thought, R.id.iv_with};private List<ImageView> imageViewList = new ArrayList<ImageView>();private boolean isOpen = false;//菜单是否打开状态private final int r = 300;//扇形半径private float angle;//按钮之间的夹角private final long intervalTime = 100; //菜单展开的时间间隔@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initImageView();//计算按钮之间的夹角angle=(float)Math.PI/(2*(res.length-1));}/** * 初始化View */private void initImageView(){ImageView imageView = null;for(int i = 0; i < res.length; i++){imageView = (ImageView) findViewById(res[i]);imageView.setOnClickListener(this);imageViewList.add(imageView);}}/** * 点击事件 */@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.iv_open:if(isOpen){closeAnim();}else{startAnim();}break;default:Toast.makeText(MainActivity.this, ""+view.getId(), Toast.LENGTH_SHORT).show();break;}}/** * 展开菜单 */private void startAnim(){ObjectAnimator animatorX = null;ObjectAnimator animatorY = null;float translationX ;//横坐标偏移距离float translationY ;//纵坐标偏移距离for(int i = 1; i < res.length; i++){translationX = (float) (r*Math.sin(i*angle));translationY = (float) (r*Math.cos(i*angle));animatorX = ObjectAnimator.ofFloat(imageViewList.get(i),"translationX", 0F, translationX);animatorY = ObjectAnimator.ofFloat(imageViewList.get(i),"translationY", 0F, translationY);AnimatorSet animSet = new AnimatorSet();animSet.playTogether(animatorX, animatorY);animSet.setDuration(i * intervalTime);animSet.start();}isOpen = true;}/** * 关闭菜单 */private void closeAnim(){ObjectAnimator animatorX = null;ObjectAnimator animatorY = null;float translationX ;//横坐标偏移距离float translationY ;//纵坐标偏移距离for(int i = res.length - 1; i > 0; i--){translationX = (float) (r*Math.sin(i*angle));translationY = (float) (r*Math.cos(i*angle));animatorX = ObjectAnimator.ofFloat(imageViewList.get(i),"translationX", translationX, 0F);animatorY = ObjectAnimator.ofFloat(imageViewList.get(i),"translationY", translationY, 0F);AnimatorSet animSet = new AnimatorSet();animSet.playTogether(animatorX, animatorY);animSet.setDuration((res.length - i) * intervalTime);animSet.start();}isOpen = false;}/** * 监听是否按下返回键 */public boolean onKeyDown(int keyCode, KeyEvent event){//如果按下返回键,执行退出操作if(keyCode == KeyEvent.KEYCODE_BACK){showDialog();}return super.onKeyDown(keyCode, event);}/** * 退出弹框 */private void showDialog(){new AlertDialog.Builder(this).setTitle("退出").setMessage("要退出么?").setNegativeButton("退出", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {finish();}}).setPositiveButton("返回", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {}}).show();}}

  

布局activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.daidai.sectormenu.MainActivity" >    <ImageView         android:id="@+id/iv_camera"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:paddingLeft="5dp"        android:paddingTop="5dp"        android:src="@drawable/camera"/>        <ImageView         android:id="@+id/iv_music"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:paddingLeft="5dp"        android:paddingTop="5dp"        android:src="@drawable/music"/>        <ImageView         android:id="@+id/iv_place"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:paddingLeft="5dp"        android:paddingTop="5dp"        android:src="@drawable/place"/>        <ImageView         android:id="@+id/iv_sleep"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:paddingLeft="5dp"        android:paddingTop="5dp"        android:src="@drawable/sleep"/>        <ImageView         android:id="@+id/iv_thought"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:paddingLeft="5dp"        android:paddingTop="5dp"        android:src="@drawable/thought"/>        <ImageView         android:id="@+id/iv_with"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:paddingLeft="5dp"        android:paddingTop="5dp"        android:src="@drawable/with"/>        <ImageView         android:id="@+id/iv_open"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:paddingLeft="5dp"        android:paddingTop="5dp"        android:src="@drawable/open"/>        </RelativeLayout>

本人是一名Java开发人员,初次涉及Android开发,希望大家能多多指点。

同时附上源码下载以供参考:http://pan.baidu.com/s/1qWDi0ri

更多相关文章

  1. Android(安卓)SDK开发环境Eclipse安装搭建笔记
  2. Android(安卓)超仿Path时间轴和扇形菜单的效果
  3. Android(安卓)代码提示无效
  4. 【072】◀▶ Android(安卓)(IV) - 显示及后台
  5. onContextItemSelected 与 onMenuItemSelected 的那些事
  6. Android(安卓)闹钟 开发过程记录(七)
  7. android 源码大全
  8. Android菜单的定义及ActionBar的实现

随机推荐

  1. 使用 docker buildx 构建多 CPU 架构镜像
  2. Ubuntu 实例中添加 swap 分区的方法
  3. ubuntu 18.04 如何设置开机自动启动脚本
  4. CentOS 7 执行 yum 命令失败问题的排查方
  5. 如何在 Ubuntu 20.04 上安装 Node.js 和
  6. 【北亚数据恢复】sqlserver数据库被加密
  7. 如何在 Ubuntu 20.04 上安装 Apache
  8. 怎么练习绘画线条?板绘线稿入门方法教程
  9. linux下yum无法安装lrzsz,Error: Failed t
  10. Kubernetes 部署 MySQL 集群