酷炫的弹力菜单,有横向和圆弧两种

移动开发
酷炫的弹力菜单,有横向和圆弧两种。

源码简介

酷炫的弹力菜单,有横向和圆弧两种。

注:该源码圆弧菜单点击时,会导致程序崩溃.

源码运行截图

源码片段:

  1. package com.spring.menu.activity; 
  2.   
  3. import com.spring.menu.R; 
  4. import com.spring.menu.animation.SpringAnimation; 
  5. import com.spring.menu.animation.EnlargeAnimationOut; 
  6. import com.spring.menu.animation.ShrinkAnimationOut; 
  7. import com.spring.menu.animation.ZoomAnimation; 
  8. import com.spring.menu.utility.DeviceUtility; 
  9.   
  10. import android.app.Activity; 
  11. import android.os.Bundle; 
  12. import android.view.View; 
  13. import android.view.ViewGroup; 
  14. import android.view.View.OnClickListener; 
  15. import android.view.animation.Animation; 
  16. import android.view.animation.AnimationUtils; 
  17. import android.view.animation.AnticipateInterpolator; 
  18. import android.widget.RelativeLayout; 
  19.   
  20. /** 
  21.  * Android实现伸缩弹力分布菜单效果 
  22.  * @Description: Android实现伸缩弹力分布菜单效果 
  23.   
  24.  * @File: MainActivity.java 
  25.   
  26.  * @Package com.spring.menu.activity 
  27.   
  28.  * @Author Hanyonglu 
  29.   
  30.  * @Date 2012-10-25 下午09:41:31 
  31.   
  32.  * @Version V1.0 
  33.  */ 
  34. public class MainActivity extends Activity { 
  35.     private boolean    areMenusShowing; 
  36.     private ViewGroup menusWrapper; 
  37.     private View imageViewPlus; 
  38.     private View shrinkRelativeLayout; 
  39.     private RelativeLayout layoutMain; 
  40.     // 顺时针旋转动画 
  41.     private Animation animRotateClockwise; 
  42.     // 你试着旋转动画 
  43.     private Animation animRotateAntiClockwise; 
  44.     private Class<!--?-->[] intentActivity = { 
  45.             SecondActivity.class,ThreeActivity.class,FourActivity.class
  46.             SecondActivity.class,ThreeActivity.class,FourActivity.class}; 
  47.     private int[] mainResources = { 
  48.             R.drawable.bg_main_1,R.drawable.bg_main_2, 
  49.             R.drawable.bg_main_3,R.drawable.bg_main_4, 
  50.             R.drawable.bg_main_1,R.drawable.bg_main_4}; 
  51.   
  52.     /** Called when the activity is first created. */ 
  53.     @Override 
  54.     public void onCreate(Bundle savedInstanceState) { 
  55.         super.onCreate(savedInstanceState); 
  56.         setContentView(R.layout.main_activity); 
  57.           
  58.         // 初始化 
  59.         initViews(); 
  60.     } 
  61.       
  62.     // 初始化 
  63.     private void initViews(){ 
  64.         imageViewPlus = findViewById(R.id.imageview_plus); 
  65.         menusWrapper = (ViewGroup) findViewById(R.id.menus_wrapper); 
  66.         shrinkRelativeLayout = findViewById(R.id.relativelayout_shrink); 
  67.         layoutMain = (RelativeLayout) findViewById(R.id.layout_content); 
  68.           
  69.         animRotateClockwise = AnimationUtils.loadAnimation( 
  70.                 this,R.anim.rotate_clockwise); 
  71.         animRotateAntiClockwise = AnimationUtils.loadAnimation( 
  72.                 this,R.anim.rotate_anticlockwise); 
  73.   
  74.         shrinkRelativeLayout.setOnClickListener(new OnClickListener() { 
  75.   
  76.             public void onClick(View v) { 
  77.                 // TODO Auto-generated method stub 
  78.                 showLinearMenus(); 
  79.             } 
  80.         }); 
  81.           
  82.         for (int i = 0; i < menusWrapper.getChildCount(); i++) { 
  83.             menusWrapper.getChildAt(i).setOnClickListener( 
  84.                     new SpringMenuLauncher(null,mainResources[i])); 
  85.         } 
  86.     } 
  87.   
  88.     /** 
  89.      * 以直线型展开菜单 
  90.      */ 
  91.     private void showLinearMenus() { 
  92.         int[] size = DeviceUtility.getScreenSize(this); 
  93.           
  94.         if (!areMenusShowing) { 
  95.             SpringAnimation.startAnimations( 
  96.                     this.menusWrapper, ZoomAnimation.Direction.SHOW, size); 
  97.             this.imageViewPlus.startAnimation(this.animRotateClockwise); 
  98.         } else { 
  99.             SpringAnimation.startAnimations( 
  100.                     this.menusWrapper, ZoomAnimation.Direction.HIDE, size); 
  101.             this.imageViewPlus.startAnimation(this.animRotateAntiClockwise); 
  102.         } 
  103.           
  104.         areMenusShowing = !areMenusShowing; 
  105.     } 
  106.   
  107.     // 分布菜单事件监听器 
  108.     private class SpringMenuLauncher implements OnClickListener { 
  109.         private final Class<!--?--> cls; 
  110.         private int resource; 
  111.   
  112.         private SpringMenuLauncher(Class<!--?--> c,int resource) { 
  113.             this.cls = c; 
  114.             this.resource = resource; 
  115.         } 
  116.   
  117.         public void onClick(View v) { 
  118.             // TODO Auto-generated method stub 
  119.             MainActivity.this.startSpringMenuAnimations(v); 
  120.             layoutMain.setBackgroundResource(resource); 
  121.               
  122. //            MainActivity.this.startActivity( 
  123. //                    new Intent( 
  124. //                            MainActivity.this, 
  125. //                            MainActivity.SpringMenuLauncher.this.cls)); 
  126.         } 
  127.     } 
  128.   
  129.     /** 
  130.      * 展现菜单动画效果 
  131.      * @param view 
  132.      * @param runnable 
  133.      */ 
  134.     private void startSpringMenuAnimations(View view) { 
  135.         areMenusShowing = true
  136.         Animation shrinkOut1 = new ShrinkAnimationOut(300); 
  137.         Animation growOut = new EnlargeAnimationOut(300); 
  138.         shrinkOut1.setInterpolator(new AnticipateInterpolator(2.0F)); 
  139.         shrinkOut1.setAnimationListener(new Animation.AnimationListener() { 
  140.   
  141.             public void onAnimationEnd(Animation animation) { 
  142.                 // TODO Auto-generated method stub 
  143.                 MainActivity.this.imageViewPlus.clearAnimation(); 
  144.             } 
  145.   
  146.             public void onAnimationRepeat(Animation animation) { 
  147.                 // TODO Auto-generated method stub 
  148.                   
  149.             } 
  150.   
  151.             public void onAnimationStart(Animation animation) { 
  152.                 // TODO Auto-generated method stub 
  153.                   
  154.             } 
  155.         }); 
  156.           
  157.         view.startAnimation(growOut); 
  158.     } 
  159.   
  160. 在点击红色按钮时弹出最上面的菜单,点击某个菜单时变换上面的背景图片,当然也可直接进入某个Activity。所以上面定义了intentActivity和mainResources两个数组,分别代表切换的Activity和要变换的图片。大家可根据实际的需要进行设置。在进行点击红色按钮时中间的加号向右进行旋转225变成叉号,通过如下的动画: 
  161.   
  162. View Row Code 
  163. <!--?xml version="1.0" encoding="UTF-8"?--> 
  164. <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"
  165.   
  166.  再次点击则向左旋转还原,将上面的android:fromDegrees和android:toDegrees替换下即可。 
  167.   
  168. 下面了解下另一个重要的动画类是SpringAnimation,由它来控制各个菜单的动画效果,代码如下所示: 
  169.   
  170. View Row Code 
  171. package com.spring.menu.animation; 
  172. import com.spring.menu.control.ImageButtonExtend; 
  173. import android.view.View; 
  174. import android.view.ViewGroup; 
  175. import android.view.ViewGroup.MarginLayoutParams; 
  176. import android.view.animation.AnticipateInterpolator; 
  177. import android.view.animation.AnticipateOvershootInterpolator; 
  178. import android.view.animation.OvershootInterpolator; 
  179. import android.view.animation.TranslateAnimation; 
  180. /** 
  181. * 分布菜单加载和伸缩动画 
  182. * @Description: 分布菜单加载和伸缩动画 
  183. * @File: SpringAnimation.java 
  184. * @Package com.spring.menu.animation 
  185. * @Author Hanyonglu 
  186. * @Date 2012-10-25 下午12:18:39 
  187. * @Version V1.0 
  188. */ 
  189. public class SpringAnimation extends ZoomAnimation { 
  190. private static int[] size; 
  191. private static int xOffset = 210
  192. private static int yOffset = -15
  193. public static final int DURATION = 300
  194. /** 
  195. * 构造器 
  196. * @param direction 
  197. * @param duration 
  198. * @param view 
  199. */ 
  200. public SpringAnimation(Direction direction, long duration, View view) { 
  201. super(direction, duration, new View[] { view }); 
  202. SpringAnimation.xOffset = SpringAnimation.size[0] / 2 - 30
  203. /** 
  204. * 开始显示动画效果 
  205. * @param viewgroup 
  206. * @param direction 
  207. * @param size 
  208. */ 
  209. public static void startAnimations(ViewGroup viewgroup, 
  210. ZoomAnimation.Direction direction, int[] size) { 
  211. SpringAnimation.size = size; 
  212. switch (direction) { 
  213. case HIDE: 
  214. startShrinkAnimations(viewgroup); 
  215. break
  216. case SHOW: 
  217. startEnlargeAnimations(viewgroup); 
  218. break
  219. /** 
  220. * 开始呈现菜单 
  221. * @param viewgroup 
  222. */ 
  223. private static void startEnlargeAnimations(ViewGroup viewgroup) { 
  224. for (int i = 0; i < viewgroup.getChildCount(); i++) { 
  225. if (viewgroup.getChildAt(i) instanceof ImageButtonExtend) { 
  226. ImageButtonExtend inoutimagebutton = (ImageButtonExtend) viewgroup 
  227. .getChildAt(i); 
  228. SpringAnimation animation = new SpringAnimation( 
  229. ZoomAnimation.Direction.HIDE, DURATION, inoutimagebutton); 
  230. animation.setStartOffset((i * 200
  231. / (-1 + viewgroup.getChildCount())); 
  232. animation.setInterpolator(new OvershootInterpolator(4F)); 
  233. inoutimagebutton.startAnimation(animation); 
  234. /** 
  235. * 开始隐藏菜单 
  236. * @param viewgroup 
  237. */ 
  238. private static void startShrinkAnimations(ViewGroup viewgroup) { 
  239. for (int i = 0; i < viewgroup.getChildCount(); i++) { 
  240. if (viewgroup.getChildAt(i) instanceof ImageButtonExtend) { 
  241. ImageButtonExtend inoutimagebutton = (ImageButtonExtend) viewgroup 
  242. .getChildAt(i); 
  243. SpringAnimation animation = new SpringAnimation( 
  244. ZoomAnimation.Direction.SHOW, DURATION, 
  245. inoutimagebutton); 
  246. animation.setStartOffset((100 * ((-1 + viewgroup 
  247. .getChildCount()) - i)) 
  248. / (-1 + viewgroup.getChildCount())); 
  249. animation.setInterpolator(new AnticipateOvershootInterpolator(2F)); 
  250. inoutimagebutton.startAnimation(animation); 
  251. @Override 
  252. protected void addShrinkAnimation(View[] views) { 
  253. // TODO Auto-generated method stub 
  254. MarginLayoutParams mlp = (MarginLayoutParams) views[0]. 
  255. getLayoutParams(); 
  256. addAnimation(new TranslateAnimation( 
  257. xOffset + -mlp.leftMargin, 
  258. F,yOffset + mlp.bottomMargin, 0F)); 
  259. @Override 
  260. protected void addEnlargeAnimation(View[] views) { 
  261. // TODO Auto-generated method stub 
  262. MarginLayoutParams mlp = (MarginLayoutParams) views[0]. 
  263. getLayoutParams(); 
  264. addAnimation(new TranslateAnimation( 
  265. F, xOffset + -mlp.leftMargin, 
  266. F,yOffset + mlp.bottomMargin)); 
  267. }</rotate> 

源码链接:http://down.51cto.com/data/1968733

【责任编辑:chenqingxiang TEL:(010)68476606】

责任编辑:chenqingxiang 来源: 网络整理
相关推荐

2015-10-20 15:58:28

弹力菜单android源码

2023-04-26 15:27:11

JavaScript技巧元素

2021-02-24 13:51:45

BIMAI建筑技术

2020-02-21 17:33:17

SparkKafka数据

2009-09-14 19:25:09

Ruby form

2010-10-11 10:31:51

MySQL分区

2013-05-27 14:31:34

Hadoop 2.0

2021-07-26 14:14:40

VRAI人工智能

2023-11-09 09:09:36

ZookeeperCP组件

2010-07-13 10:47:18

Perl面向对象

2009-06-19 17:05:08

MVC框架Struts和Spri

2020-01-03 10:50:16

Python编程语言Mac电脑

2011-06-22 14:14:27

pageEncodincontentType

2012-12-24 13:30:34

iOS

2020-09-24 10:09:01

GETPOST前端

2021-05-27 10:57:01

TCP定时器网络协议

2019-02-22 12:58:51

Windows 10微软开始菜单

2010-03-11 14:34:47

Python环境

2011-03-03 10:26:04

Pureftpd

2017-11-16 09:20:20

内存虚拟化技术
点赞
收藏

51CTO技术栈公众号