源码简介
酷炫的弹力菜单,有横向和圆弧两种。
注:该源码圆弧菜单点击时,会导致程序崩溃.
源码运行截图
源码片段:
- package com.spring.menu.activity;
- import com.spring.menu.R;
- import com.spring.menu.animation.SpringAnimation;
- import com.spring.menu.animation.EnlargeAnimationOut;
- import com.spring.menu.animation.ShrinkAnimationOut;
- import com.spring.menu.animation.ZoomAnimation;
- import com.spring.menu.utility.DeviceUtility;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.View.OnClickListener;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.view.animation.AnticipateInterpolator;
- import android.widget.RelativeLayout;
- /**
- * Android实现伸缩弹力分布菜单效果
- * @Description: Android实现伸缩弹力分布菜单效果
- * @File: MainActivity.java
- * @Package com.spring.menu.activity
- * @Author Hanyonglu
- * @Date 2012-10-25 下午09:41:31
- * @Version V1.0
- */
- public class MainActivity extends Activity {
- private boolean areMenusShowing;
- private ViewGroup menusWrapper;
- private View imageViewPlus;
- private View shrinkRelativeLayout;
- private RelativeLayout layoutMain;
- // 顺时针旋转动画
- private Animation animRotateClockwise;
- // 你试着旋转动画
- private Animation animRotateAntiClockwise;
- private Class<!--?-->[] intentActivity = {
- SecondActivity.class,ThreeActivity.class,FourActivity.class,
- SecondActivity.class,ThreeActivity.class,FourActivity.class};
- private int[] mainResources = {
- R.drawable.bg_main_1,R.drawable.bg_main_2,
- R.drawable.bg_main_3,R.drawable.bg_main_4,
- R.drawable.bg_main_1,R.drawable.bg_main_4};
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main_activity);
- // 初始化
- initViews();
- }
- // 初始化
- private void initViews(){
- imageViewPlus = findViewById(R.id.imageview_plus);
- menusWrapper = (ViewGroup) findViewById(R.id.menus_wrapper);
- shrinkRelativeLayout = findViewById(R.id.relativelayout_shrink);
- layoutMain = (RelativeLayout) findViewById(R.id.layout_content);
- animRotateClockwise = AnimationUtils.loadAnimation(
- this,R.anim.rotate_clockwise);
- animRotateAntiClockwise = AnimationUtils.loadAnimation(
- this,R.anim.rotate_anticlockwise);
- shrinkRelativeLayout.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // TODO Auto-generated method stub
- showLinearMenus();
- }
- });
- for (int i = 0; i < menusWrapper.getChildCount(); i++) {
- menusWrapper.getChildAt(i).setOnClickListener(
- new SpringMenuLauncher(null,mainResources[i]));
- }
- }
- /**
- * 以直线型展开菜单
- */
- private void showLinearMenus() {
- int[] size = DeviceUtility.getScreenSize(this);
- if (!areMenusShowing) {
- SpringAnimation.startAnimations(
- this.menusWrapper, ZoomAnimation.Direction.SHOW, size);
- this.imageViewPlus.startAnimation(this.animRotateClockwise);
- } else {
- SpringAnimation.startAnimations(
- this.menusWrapper, ZoomAnimation.Direction.HIDE, size);
- this.imageViewPlus.startAnimation(this.animRotateAntiClockwise);
- }
- areMenusShowing = !areMenusShowing;
- }
- // 分布菜单事件监听器
- private class SpringMenuLauncher implements OnClickListener {
- private final Class<!--?--> cls;
- private int resource;
- private SpringMenuLauncher(Class<!--?--> c,int resource) {
- this.cls = c;
- this.resource = resource;
- }
- public void onClick(View v) {
- // TODO Auto-generated method stub
- MainActivity.this.startSpringMenuAnimations(v);
- layoutMain.setBackgroundResource(resource);
- // MainActivity.this.startActivity(
- // new Intent(
- // MainActivity.this,
- // MainActivity.SpringMenuLauncher.this.cls));
- }
- }
- /**
- * 展现菜单动画效果
- * @param view
- * @param runnable
- */
- private void startSpringMenuAnimations(View view) {
- areMenusShowing = true;
- Animation shrinkOut1 = new ShrinkAnimationOut(300);
- Animation growOut = new EnlargeAnimationOut(300);
- shrinkOut1.setInterpolator(new AnticipateInterpolator(2.0F));
- shrinkOut1.setAnimationListener(new Animation.AnimationListener() {
- public void onAnimationEnd(Animation animation) {
- // TODO Auto-generated method stub
- MainActivity.this.imageViewPlus.clearAnimation();
- }
- public void onAnimationRepeat(Animation animation) {
- // TODO Auto-generated method stub
- }
- public void onAnimationStart(Animation animation) {
- // TODO Auto-generated method stub
- }
- });
- view.startAnimation(growOut);
- }
- }
- 在点击红色按钮时弹出最上面的菜单,点击某个菜单时变换上面的背景图片,当然也可直接进入某个Activity。所以上面定义了intentActivity和mainResources两个数组,分别代表切换的Activity和要变换的图片。大家可根据实际的需要进行设置。在进行点击红色按钮时中间的加号向右进行旋转225变成叉号,通过如下的动画:
- View Row Code
- <!--?xml version="1.0" encoding="UTF-8"?-->
- <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator" android:duration="200" android:fromdegrees="0.0" android:todegrees="225.0" android:pivotx="50.0%" android:pivoty="50.0%" android:fillafter="true" android:fillenabled="true">
- 再次点击则向左旋转还原,将上面的android:fromDegrees和android:toDegrees替换下即可。
- 下面了解下另一个重要的动画类是SpringAnimation,由它来控制各个菜单的动画效果,代码如下所示:
- View Row Code
- package com.spring.menu.animation;
- import com.spring.menu.control.ImageButtonExtend;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.ViewGroup.MarginLayoutParams;
- import android.view.animation.AnticipateInterpolator;
- import android.view.animation.AnticipateOvershootInterpolator;
- import android.view.animation.OvershootInterpolator;
- import android.view.animation.TranslateAnimation;
- /**
- * 分布菜单加载和伸缩动画
- * @Description: 分布菜单加载和伸缩动画
- * @File: SpringAnimation.java
- * @Package com.spring.menu.animation
- * @Author Hanyonglu
- * @Date 2012-10-25 下午12:18:39
- * @Version V1.0
- */
- public class SpringAnimation extends ZoomAnimation {
- private static int[] size;
- private static int xOffset = 210;
- private static int yOffset = -15;
- public static final int DURATION = 300;
- /**
- * 构造器
- * @param direction
- * @param duration
- * @param view
- */
- public SpringAnimation(Direction direction, long duration, View view) {
- super(direction, duration, new View[] { view });
- SpringAnimation.xOffset = SpringAnimation.size[0] / 2 - 30;
- }
- /**
- * 开始显示动画效果
- * @param viewgroup
- * @param direction
- * @param size
- */
- public static void startAnimations(ViewGroup viewgroup,
- ZoomAnimation.Direction direction, int[] size) {
- SpringAnimation.size = size;
- switch (direction) {
- case HIDE:
- startShrinkAnimations(viewgroup);
- break;
- case SHOW:
- startEnlargeAnimations(viewgroup);
- break;
- }
- }
- /**
- * 开始呈现菜单
- * @param viewgroup
- */
- private static void startEnlargeAnimations(ViewGroup viewgroup) {
- for (int i = 0; i < viewgroup.getChildCount(); i++) {
- if (viewgroup.getChildAt(i) instanceof ImageButtonExtend) {
- ImageButtonExtend inoutimagebutton = (ImageButtonExtend) viewgroup
- .getChildAt(i);
- SpringAnimation animation = new SpringAnimation(
- ZoomAnimation.Direction.HIDE, DURATION, inoutimagebutton);
- animation.setStartOffset((i * 200)
- / (-1 + viewgroup.getChildCount()));
- animation.setInterpolator(new OvershootInterpolator(4F));
- inoutimagebutton.startAnimation(animation);
- }
- }
- }
- /**
- * 开始隐藏菜单
- * @param viewgroup
- */
- private static void startShrinkAnimations(ViewGroup viewgroup) {
- for (int i = 0; i < viewgroup.getChildCount(); i++) {
- if (viewgroup.getChildAt(i) instanceof ImageButtonExtend) {
- ImageButtonExtend inoutimagebutton = (ImageButtonExtend) viewgroup
- .getChildAt(i);
- SpringAnimation animation = new SpringAnimation(
- ZoomAnimation.Direction.SHOW, DURATION,
- inoutimagebutton);
- animation.setStartOffset((100 * ((-1 + viewgroup
- .getChildCount()) - i))
- / (-1 + viewgroup.getChildCount()));
- animation.setInterpolator(new AnticipateOvershootInterpolator(2F));
- inoutimagebutton.startAnimation(animation);
- }
- }
- }
- @Override
- protected void addShrinkAnimation(View[] views) {
- // TODO Auto-generated method stub
- MarginLayoutParams mlp = (MarginLayoutParams) views[0].
- getLayoutParams();
- addAnimation(new TranslateAnimation(
- xOffset + -mlp.leftMargin,
- F,yOffset + mlp.bottomMargin, 0F));
- }
- @Override
- protected void addEnlargeAnimation(View[] views) {
- // TODO Auto-generated method stub
- MarginLayoutParams mlp = (MarginLayoutParams) views[0].
- getLayoutParams();
- addAnimation(new TranslateAnimation(
- F, xOffset + -mlp.leftMargin,
- F,yOffset + mlp.bottomMargin));
- }
- }</rotate>
源码链接:http://down.51cto.com/data/1968733
【责任编辑:chenqingxiang TEL:(010)68476606】