Cool elastic menu, with horizontal and arc

Cool elastic menu, with horizontal and arc

Source code introduction

Cool elastic menu, with horizontal and circular options.

Note: Clicking the source code arc menu will cause the program to crash.

Source code running screenshot

Source code snippet:

  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 implements the effect of elastic distribution menu
  22. * @Description: Android implements the effect of retractable elastic distribution menu
  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 PM
  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. // Clockwise rotation animation  
  41. private Animation animRotateClockwise;
  42. // You try to rotate the animation  
  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. // Initialization  
  59. initViews();
  60. }
  61.       
  62. // Initialization  
  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. * Expand the menu in a straight line
  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. //Distribution menu event listener  
  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. * Display menu animation effects
  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.   
  161. When you click the red button, the top menu pops up. When you click a menu, the background image above changes. Of course, you can also directly enter a certain Activity. Therefore, two arrays, intentActivity and mainResources, are defined above, representing the switched Activity and the image to be changed respectively. You can set them according to your actual needs. When you click the red button, the plus sign in the middle rotates 225 degrees to the right and becomes a cross sign, through the following animation:
  162.   
  163. View Row Code
  164. <!--?xml version= "1.0" encoding= "UTF-8" ?-->
  165. <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" >
  166.   
  167. Click again to rotate left and restore, and replace the android:fromDegrees and android:toDegrees above.
  168.   
  169. Let's take a look at another important animation class, SpringAnimation, which controls the animation effects of each menu. The code is as follows:
  170.   
  171. View Row Code
  172. package com.spring.menu.animation;
  173. import com.spring.menu.control.ImageButtonExtend;
  174. import android.view.View;
  175. import android.view.ViewGroup;
  176. import android.view.ViewGroup.MarginLayoutParams;
  177. import android.view.animation.AnticipateInterpolator;
  178. import android.view.animation.AnticipateOvershootInterpolator;
  179. import android.view.animation.OvershootInterpolator;
  180. import android.view.animation.TranslateAnimation;
  181. /**
  182. * Distributed menu loading and scaling animation
  183. * @Description: Distributed menu loading and scaling animation
  184. * @File: SpringAnimation.java
  185. * @Package com.spring.menu.animation
  186. * @Author Hanyonglu
  187. * @Date 2012-10-25 12:18:39 PM
  188. * @Version V1.0
  189. */  
  190. public   class SpringAnimation extends ZoomAnimation {
  191. private   static   int [] size;
  192. private   static   int xOffset = 210 ;
  193. private   static   int yOffset = - 15 ;
  194. public   static   final   int DURATION = 300 ;
  195. /**
  196. * Constructor
  197. * @param direction
  198. * @param duration
  199. * @param view
  200. */  
  201. public SpringAnimation(Direction direction, long duration, View view) {
  202. super (direction, duration, new View[] { view });
  203. SpringAnimation.xOffset = SpringAnimation.size[ 0 ] / 2 - 30 ;
  204. }
  205. /**
  206. * Start displaying animation effects
  207. * @param viewgroup
  208. * @param direction
  209. * @param size
  210. */  
  211. public   static   void startAnimations(ViewGroup viewgroup,
  212. ZoomAnimation.Direction direction, int [] size) {
  213. SpringAnimation.size = size;
  214. switch (direction) {
  215. case HIDE:
  216. startShrinkAnimations(viewgroup);
  217. break ;
  218. case SHOW:
  219. startEnlargeAnimations(viewgroup);
  220. break ;
  221. }
  222. }
  223. /**
  224. * Start presenting the menu
  225. * @param viewgroup
  226. */  
  227. private   static   void startEnlargeAnimations(ViewGroup viewgroup) {
  228. for ( int i = 0 ; i < viewgroup.getChildCount(); i++) {
  229. if (viewgroup.getChildAt(i) instanceof ImageButtonExtend) {
  230. ImageButtonExtend inoutimagebutton = (ImageButtonExtend) viewgroup
  231. .getChildAt(i);
  232. SpringAnimation animation = new SpringAnimation(
  233. ZoomAnimation.Direction.HIDE, DURATION, inoutimagebutton);
  234. animation.setStartOffset((i * 200 )
  235. / (- 1 + viewgroup.getChildCount()));
  236. animation.setInterpolator( new OvershootInterpolator(4F));
  237. inoutimagebutton.startAnimation(animation);
  238. }
  239. }
  240. }
  241. /**
  242. * Start hiding menu
  243. * @param viewgroup
  244. */  
  245. private   static   void startShrinkAnimations(ViewGroup viewgroup) {
  246. for ( int i = 0 ; i < viewgroup.getChildCount(); i++) {
  247. if (viewgroup.getChildAt(i) instanceof ImageButtonExtend) {
  248. ImageButtonExtend inoutimagebutton = (ImageButtonExtend) viewgroup
  249. .getChildAt(i);
  250. SpringAnimation animation = new SpringAnimation(
  251. ZoomAnimation.Direction.SHOW, DURATION,
  252. inoutimagebutton);
  253. animation.setStartOffset(( 100 * ((- 1 + viewgroup
  254. .getChildCount()) - i))
  255. / (- 1 + viewgroup.getChildCount()));
  256. animation.setInterpolator( new AnticipateOvershootInterpolator(2F));
  257. inoutimagebutton.startAnimation(animation);
  258. }
  259. }
  260. }
  261. @Override  
  262. protected   void addShrinkAnimation(View[] views) {
  263. // TODO Auto-generated method stub  
  264. MarginLayoutParams mlp = (MarginLayoutParams) views[ 0 ].
  265. getLayoutParams();
  266. addAnimation( new TranslateAnimation(
  267. xOffset + -mlp.leftMargin,
  268. F,yOffset + mlp.bottomMargin, 0F));
  269. }
  270. @Override  
  271. protected   void addEnlargeAnimation(View[] views) {
  272. // TODO Auto-generated method stub  
  273. MarginLayoutParams mlp = (MarginLayoutParams) views[ 0 ].
  274. getLayoutParams();
  275. addAnimation( new TranslateAnimation(
  276. F, xOffset + -mlp.leftMargin,
  277. F,yOffset + mlp.bottomMargin));
  278. }
  279. }</rotate>

Source code link: http://download..com/data/1968733

[Editor: chenqingxiang TEL: (010) 68476606]

<<:  QR code generation and scanning source code download

>>:  Android open source time selection control

Recommend

After being banned for 3 years, WeChat’s important “feature” may return!

Recently, according to feedback from some iOS use...

Apple Watch actually has these little secrets?

Apple has always been reluctant to release detail...

2019 Pinduoduo Product Operation Analysis Report

Pinduoduo has rapidly grown into the third larges...

How to price your product? Here are 4 ways

Price is an important means of competition. Prici...

Soul product analysis!

Contemporary young people use social software - y...