其他分享
首页 > 其他分享> > Android Path菜单的简单实现,android开发软件

Android Path菜单的简单实现,android开发软件

作者:互联网

  1. mHomeparams.width = LayoutParams.WRAP_CONTENT;

  2. mHomeparams.height = LayoutParams.WRAP_CONTENT;

  3. switch (position) {

  4. case LEFT_TOP:

  5. mHomeparams.gravity = Gravity.LEFT | Gravity.TOP;

  6. for (int i = 0; i < menuResIds.length; i++) {

  7. int width_padding = mWIDTH / ((menuResIds.length - 1) * 2);

  8. int height_padding = mHEIGHT / ((menuResIds.length - 1) * 2);

  9. ImageView imageView = new ImageView(mContext);

  10. imageView.setImageResource(menuResIds[i]);

  11. addView(imageView);

  12. LayoutParams params = (FrameLayout.LayoutParams) imageView

  13. .getLayoutParams();

  14. params.width = LayoutParams.WRAP_CONTENT;

  15. params.height = LayoutParams.WRAP_CONTENT;

  16. params.leftMargin = mWIDTH / 2

  17. - ((menuResIds.length - i - 1) * width_padding);

  18. params.topMargin = mHEIGHT / 2 - i * height_padding;

  19. params.gravity = Gravity.LEFT | Gravity.TOP;

  20. imageView.setLayoutParams(params);

  21. }

  22. break;

  23. case RIGHT_TOP:

  24. mHomeparams.gravity = Gravity.RIGHT | Gravity.TOP;

  25. for (int i = 0; i < menuResIds.length; i++) {

  26. int width_padding = mWIDTH / ((menuResIds.length - 1) * 2);

  27. int height_padding = mHEIGHT / ((menuResIds.length - 1) * 2);

  28. ImageView imageView = new ImageView(mContext);

  29. imageView.setImageResource(menuResIds[i]);

  30. addView(imageView);

  31. LayoutParams params = (FrameLayout.LayoutParams) imageView

  32. .getLayoutParams();

  33. params.width = LayoutParams.WRAP_CONTENT;

  34. params.height = LayoutParams.WRAP_CONTENT;

  35. params.rightMargin = mWIDTH / 2

  36. - ((menuResIds.length - i - 1) * width_padding);

  37. params.topMargin = mHEIGHT / 2 - i * height_padding;

  38. params.gravity = Gravity.RIGHT | Gravity.TOP;

  39. imageView.setLayoutParams(params);

  40. }

  41. break;

  42. case RIGHT_BOTTOM:

  43. mHomeparams.gravity = Gravity.RIGHT | Gravity.BOTTOM;

  44. for (int i = 0; i < menuResIds.length; i++) {

  45. int width_padding = mWIDTH / ((menuResIds.length - 1) * 2);

  46. int height_padding = mHEIGHT / ((menuResIds.length - 1) * 2);

  47. ImageView imageView = new ImageView(mContext);

  48. imageView.setImageResource(menuResIds[i]);

  49. addView(imageView);

  50. LayoutParams params = (FrameLayout.LayoutParams) imageView

  51. .getLayoutParams();

  52. params.width = LayoutParams.WRAP_CONTENT;

  53. params.height = LayoutParams.WRAP_CONTENT;

  54. params.rightMargin = mWIDTH / 2

  55. - ((menuResIds.length - i - 1) * width_padding);

  56. params.bottomMargin = mHEIGHT / 2 - i * height_padding;

  57. params.gravity = Gravity.RIGHT | Gravity.BOTTOM;

  58. imageView.setLayoutParams(params);

  59. }

  60. break;

  61. case LEFT_BOTTOM:

  62. mHomeparams.gravity = Gravity.LEFT | Gravity.BOTTOM;

  63. for(int i = 0; i < menuResIds.length; i++){

  64. int width_padding = mWIDTH / ((menuResIds.length - 1) * 2);

  65. int height_padding = mHEIGHT / ((menuResIds.length -1) * 2);

  66. ImageView imageView = new ImageView(mContext);

  67. imageView.setImageResource(menuResIds[i]);

  68. addView(imageView);

  69. LayoutParams params = (FrameLayout.LayoutParams)imageView.getLayoutParams();

  70. params.width = LayoutParams.WRAP_CONTENT;

  71. params.height = LayoutParams.WRAP_CONTENT;

  72. params.leftMargin = mWIDTH / 2 - ((menuResIds.length - i - 1) * width_padding);

  73. params.bottomMargin = mHEIGHT / 2 - i * height_padding;

  74. params.gravity = Gravity.LEFT | Gravity.BOTTOM;

  75. imageView.setLayoutParams(params);

  76. }

  77. break;

  78. default:

  79. break;

  80. }

  81. mHome.setLayoutParams(mHomeparams);

  82. }

  83. private OnClickListener listener = new OnClickListener() {

  84. public void onClick(View v) {

  85. if (!bMenuShow) {

  86. startAnimationIn(PathMenuView.this, 300);

  87. } else {

  88. startAnimationOut(PathMenuView.this, 300);

  89. }

  90. bMenuShow = !bMenuShow;

  91. }

  92. };

  93. /**

  94. * 菜单隐藏动画.

  95. *

  96. * @param group

  97. * @param duration

  98. */

  99. private void startAnimationIn(ViewGroup group, int duration) {

  100. for (int i = 1; i < group.getChildCount(); i++) {

  101. ImageView imageview = (ImageView) group.getChildAt(i);

  102. imageview.setVisibility(0);

  103. MarginLayoutParams mlp = (MarginLayoutParams) imageview

  104. .getLayoutParams();

  105. Animation animation = null;

  106. switch (position) {

  107. case LEFT_TOP:

  108. animation = new TranslateAnimation(0F,-mlp.leftMargin+xOffset,0F,-mlp.topMargin + yOffset);

  109. break;

  110. case RIGHT_TOP:

  111. animation = new TranslateAnimation(mlp.rightMargin - xOffset,0F,-mlp.topMargin + yOffset,0F);

  112. break;

  113. case LEFT_BOTTOM:

  114. animation = new TranslateAnimation(0F, -mlp.leftMargin+ xOffset, 0F, -yOffset + mlp.bottomMargin);

  115. break;

  116. case RIGHT_BOTTOM:

  117. animation = new TranslateAnimation(mlp.rightMargin-xOffset,0F,-yOffset + mlp.bottomMargin, 0F);

  118. break;

  119. default:

  120. break;

  121. }

  122. animation.setFillAfter(true);

  123. animation.setDuration(duration);

  124. animation.setStartOffset((i * 100) / (-1 + group.getChildCount()));

  125. animation.setInterpolator(new OvershootInterpolator(2F));

  126. imageview.startAnimation(animation);

  127. }

  128. }

  129. /**

  130. * 菜单显示动画.

  131. *

  132. * @param group

  133. * @param duration

  134. */

  135. private void startAnimationOut(ViewGroup group,int duration){

  136. for (int i = 1; i < group.getChildCount(); i++) {

  137. final ImageView imageview = (ImageView) group

  138. .getChildAt(i);

  139. MarginLayoutParams mlp = (MarginLayoutParams) imageview.getLayoutParams();

  140. Animation animation = null;

  141. switch (position) {

  142. case LEFT_TOP:

  143. animation = new TranslateAnimation(-mlp.leftMargin+xOffset,0F,-mlp.topMargin + yOffset,0F);

  144. break;

  145. case RIGHT_TOP:

  146. animation = new TranslateAnimation(0F,mlp.rightMargin - xOffset,0F,-mlp.topMargin + yOffset);

  147. break;

  148. case LEFT_BOTTOM:

  149. animation = new TranslateAnimation(-mlp.leftMargin+xOffset,0F, -yOffset + mlp.bottomMargin,0F);

  150. break;

  151. case RIGHT_BOTTOM:

  152. animation = new TranslateAnimation(0F,mlp.rightMargin-xOffset, 0F,-yOffset + mlp.bottomMargin);

  153. break;

  154. default:

  155. break;

  156. }

  157. animation.setFillAfter(true);animation.setDuration(duration);

  158. animation.setStartOffset(((group.getChildCount()-i) * 100)

  159. / (-1 + group.getChildCount()));

  160. animation.setInterpolator(new AnticipateInterpolator(2F));

  161. imageview.startAnimation(animation);

  162. }

  163. }

  164. }

第四步:PathTestActivity.java以及用到的布局文件main.xml代码如下:

PathTestActivity.java(基本没修改代码)代码如下:

[java]  view plain copy

  1. package com.tutor.path;

  2. import android.app.Activity;

  3. import android.os.Bundle;

  4. public class PathTestActivity extends Activity {

  5. @Override

  6. public void onCreate(Bundle savedInstanceState) {

  7. super.onCreate(savedInstanceState);

  8. setContentView(R.layout.main);

  9. }

  10. }

main.xml代码如下:

[java]  view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

  3. xmlns:tutor=“http://schemas.android.com/apk/res/com.tutor.path”

  4. android:layout_width=“fill_parent”

  5. android:layout_height=“fill_parent”

  6. android:orientation=“vertical” >

  7. <com.tutor.path.PathMenuView

  8. android:id="@+id/text"

  9. android:layout_width=“fill_parent”

  10. android:layout_height=“fill_parent”

  11. tutor:position=“right_bottom”

  12. />

  13.   

最后

下面是辛苦给大家整理的学习路线

  1. android:layout_height=“fill_parent”

  2. android:orientation=“vertical” >

  3. <com.tutor.path.PathMenuView

  4. android:id="@+id/text"

  5. android:layout_width=“fill_parent”

  6. android:layout_height=“fill_parent”

  7. tutor:position=“right_bottom”

  8. />

  9.   

最后

下面是辛苦给大家整理的学习路线

[外链图片转存中…(img-cse5ggaX-1643518121306)]

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

标签:开发软件,LayoutParams,height,mlp,animation,params,imageView,Path,Android
来源: https://blog.csdn.net/m0_66144910/article/details/122751957