Android高手进阶教程(二十六)之---Android超仿Path菜单的功能实现!

Android高手进阶教程(二十六)之---Android超仿Path菜单的功能实现!,第1张

概述Hi~大家好,出来创业快3个月了,一切还不错,前一段时间用了业余时间搞了个问答类网站YQMA.想做中国的stackoverflow,哈哈,只是YY下,希望大家多多支持!

Hi~大家好,出来创业快3个月了,一切还不错,前一段时间用了业余时间搞了个问答类网站YQMA.想做中国的stackoverflow,哈哈,只是YY下,希望大家多多支持!

好了,今天给大家分享的是Path菜单的简单实现,可以支持自定义方向(左上,右上,右下,左下),并且可以自定义菜单的个数,难点就是菜单的摆放位置(动态设置margin),还有动画的实现,其实动画只是简单用了个TranslateAnimation,N个菜单一起移动的时候感觉很cool~

这里也用到了自定义标签,这里不懂的童鞋可以看我 AndroID高手进阶教程(四)之----AndroID 中自定义属性(attr.xml,TypedArray)的使用! 这篇文章.好了废话不多说了,

首先创建一个androID工程命名为PathTest.目录结构如下图:


第二步:在values文件夹下新建一个attrs.xml文件,代码如下:

<?xml version="1.0" enCoding="utf-8"?> <resources>   <declare-styleable name="PathMenuVIEw">      <attr name="position">        <enum name="left_top" value="0"></enum>       <enum name="right_top" value="1"></enum>       <enum name="right_bottom" value="2"></enum>       <enum name="left_bottom" value="3"></enum>     </attr>   </declare-styleable>  </resources> 

第三步:新建一个PathMenuVIEw.Java这个就是我们自定义的Path菜单控件,代码如下:

package com.tutor.path;  import androIDcontentContext; import androIDcontentresTypedArray; import androIDutilAttributeSet; import androIDvIEwGravity; import androIDvIEwVIEw; import androIDvIEwVIEwGroup; import androIDvIEwanimationAnimation; import androIDvIEwanimationAnticipateInterpolator; import androIDvIEwanimationOvershootInterpolator; import androIDvIEwanimationTranslateAnimation; import androIDWidgetFrameLayout; import androIDwIDgetimageVIEw;   /**  * @author frankIEwei  * 超级仿path菜单  * position定义菜单的位置,目前支持:左上;右上;右下;左下四个方向。  * menuResIDs定义出现的菜单的资源ID  */ public class PathMenuVIEw extends FrameLayout {      private static final int left_top = 0;      private static final int RIGHT_top = 1;      private static final int RIGHT_BottOM = 2;      private static final int left_BottOM = 3;         /**    * 默认的位置是在右下角    */   private int position = 3;      /**    * 那个圆形菜单    */   private ImageVIEw mHome;      /**    * 上下文    */   private Context mContext;         /**    * 设备的宽度    */   private int mWIDTH = 0;      /**    * 设备的高度    */   private int mHEIGHT = 0;      /**    * 设备的density    */   private float mDensity;      /**    * 菜单是否显示    */   private boolean bMenuShow;      private static int xOffset   = 15;   private static int yOffset   = -13;      /**    * 菜单的资源个数    */   private int[] menuResIDs = {Rdrawablecomposer_camera,Rdrawablecomposer_music,Rdrawablecomposer_sleep,Rdrawablecomposer_place};       public PathMenuVIEw(Context context){     super(context);     setupVIEws();   }      public PathMenuVIEw(Context context,AttributeSet attrs) {     super(context,attrs);     TypedArray a = contextobtainStyledAttributes(attrs,RstyleablePathMenuVIEw);          position = agetInt(RstyleablePathMenuVIEw_position,3);          arecycle();     setupVIEws();   }      private voID setupVIEws(){     mContext = getContext();      mHEIGHT = mContextgetResources()getdisplayMetrics()heightPixels;     mWIDTH = mContextgetResources()getdisplayMetrics()wIDthPixels;     mDensity = mContextgetResources()getdisplayMetrics()density;          xOffset = (int) (667 * mDensity);     yOffset = (int) (667 * mDensity);          mHome = new ImageVIEw(mContext);          mHomesetimageResource(Rdrawablecomposer_button);     mHomesetonClickListener(Listener);          addVIEw(mHome);          LayoutParams mHomeparams = (FrameLayoutLayoutParams)mHomegetLayoutParams();     mHomeparamswIDth = LayoutParamsWRAP_CONTENT;     mHomeparamsheight = LayoutParamsWRAP_CONTENT;          switch (position) {     case left_top:       mHomeparamsgravity = Gravityleft | Gravitytop;       for (int i = 0; i < menuResIDslength; i++) {          int wIDth_padding = mWIDTH / ((menuResIDslength - 1) * 2);         int height_padding = mHEIGHT / ((menuResIDslength - 1) * 2);          ImageVIEw imageVIEw = new ImageVIEw(mContext);         imageVIEwsetimageResource(menuResIDs[i]);         addVIEw(imageVIEw);         LayoutParams params = (FrameLayoutLayoutParams) imageVIEw             getLayoutParams();         paramswIDth = LayoutParamsWRAP_CONTENT;         paramsheight = LayoutParamsWRAP_CONTENT;         paramsleftmargin = mWIDTH / 2             - ((menuResIDslength - i - 1) * wIDth_padding);         paramstopmargin = mHEIGHT / 2 - i * height_padding;         paramsgravity = Gravityleft | Gravitytop;         imageVIEwsetLayoutParams(params);        }              break;     case RIGHT_top:       mHomeparamsgravity = GravityRIGHT | Gravitytop;       for (int i = 0; i < menuResIDslength; i++) {          int wIDth_padding = mWIDTH / ((menuResIDslength - 1) * 2);         int height_padding = mHEIGHT / ((menuResIDslength - 1) * 2);          ImageVIEw imageVIEw = new ImageVIEw(mContext);         imageVIEwsetimageResource(menuResIDs[i]);         addVIEw(imageVIEw);         LayoutParams params = (FrameLayoutLayoutParams) imageVIEw             getLayoutParams();         paramswIDth = LayoutParamsWRAP_CONTENT;         paramsheight = LayoutParamsWRAP_CONTENT;         paramsrightmargin = mWIDTH / 2             - ((menuResIDslength - i - 1) * wIDth_padding);         paramstopmargin = mHEIGHT / 2 - i * height_padding;         paramsgravity = GravityRIGHT | Gravitytop;         imageVIEwsetLayoutParams(params);        }       break;     case RIGHT_BottOM:       mHomeparamsgravity = GravityRIGHT | GravityBottOM;       for (int i = 0; i < menuResIDslength; i++) {          int wIDth_padding = mWIDTH / ((menuResIDslength - 1) * 2);         int height_padding = mHEIGHT / ((menuResIDslength - 1) * 2);          ImageVIEw imageVIEw = new ImageVIEw(mContext);         imageVIEwsetimageResource(menuResIDs[i]);         addVIEw(imageVIEw);         LayoutParams params = (FrameLayoutLayoutParams) imageVIEw             getLayoutParams();         paramswIDth = LayoutParamsWRAP_CONTENT;         paramsheight = LayoutParamsWRAP_CONTENT;         paramsrightmargin = mWIDTH / 2             - ((menuResIDslength - i - 1) * wIDth_padding);         paramsbottommargin = mHEIGHT / 2 - i * height_padding;         paramsgravity = GravityRIGHT | GravityBottOM;         imageVIEwsetLayoutParams(params);        }       break;     case left_BottOM:       mHomeparamsgravity = Gravityleft | GravityBottOM;       for(int i = 0; i < menuResIDslength; i++){                  int wIDth_padding = mWIDTH / ((menuResIDslength - 1) * 2);         int height_padding = mHEIGHT / ((menuResIDslength -1) * 2);                  ImageVIEw imageVIEw = new ImageVIEw(mContext);         imageVIEwsetimageResource(menuResIDs[i]);         addVIEw(imageVIEw);         LayoutParams params = (FrameLayoutLayoutParams)imageVIEwgetLayoutParams();         paramswIDth = LayoutParamsWRAP_CONTENT;         paramsheight = LayoutParamsWRAP_CONTENT;              paramsleftmargin = mWIDTH / 2 - ((menuResIDslength - i - 1) * wIDth_padding);         paramsbottommargin = mHEIGHT / 2 - i * height_padding;         paramsgravity = Gravityleft | GravityBottOM;         imageVIEwsetLayoutParams(params);                  }       break;     default:         break;     }            mHomesetLayoutParams(mHomeparams);      }      private OnClickListener Listener = new OnClickListener() {          public voID onClick(VIEw v) {       if (!bMenuShow) {         startAnimationIn(PathMenuVIEwthis,300);       } else {         startAnimationOut(PathMenuVIEwthis,300);       }       bMenuShow = !bMenuShow;     }   };         /**    * 菜单隐藏动画    *    * @param group    * @param duration    */   private voID startAnimationIn(VIEwGroup group,int duration) {     for (int i = 1; i < groupgetChildCount(); i++) {       ImageVIEw imagevIEw = (ImageVIEw) groupgetChildAt(i);       imagevIEwsetVisibility(0);       marginLayoutParams mlp = (marginLayoutParams) imagevIEw           getLayoutParams();                     Animation animation = null;                     switch (position) {       case left_top:         animation = new TranslateAnimation(0F,-mlpleftmargin+xOffset,0F,-mlptopmargin + yOffset);         break;       case RIGHT_top:         animation = new TranslateAnimation(mlprightmargin - xOffset,-mlptopmargin + yOffset,0F);         break;            case left_BottOM:         animation = new TranslateAnimation(0F,-mlpleftmargin+ xOffset,-yOffset + mlpbottommargin);         break;                case RIGHT_BottOM:         animation = new TranslateAnimation(mlprightmargin-xOffset,-yOffset + mlpbottommargin,0F);         break;       default:         break;       }        animationsetFillAfter(true);       animationsetDuration(duration);       animationsetStartOffset((i * 100) / (-1 + groupgetChildCount()));       animationsetInterpolator(new OvershootInterpolator(2F));       imagevIEwstartAnimation(animation);      }   }      /**    * 菜单显示动画    *    * @param group    * @param duration    */   private voID startAnimationOut(VIEwGroup group,int duration){     for (int i = 1; i < groupgetChildCount(); i++) {       final ImageVIEw imagevIEw = (ImageVIEw) group           getChildAt(i);       marginLayoutParams mlp = (marginLayoutParams) imagevIEwgetLayoutParams();              Animation animation = null;              switch (position) {       case left_top:         animation = new TranslateAnimation(-mlpleftmargin+xOffset,0F);         break;       case RIGHT_top:         animation = new TranslateAnimation(0F,mlprightmargin - xOffset,-mlptopmargin + yOffset);         break;        case left_BottOM:         animation = new TranslateAnimation(-mlpleftmargin+xOffset,0F);         break;        case RIGHT_BottOM:         animation = new TranslateAnimation(0F,mlprightmargin-xOffset,-yOffset + mlpbottommargin);         break;       default:         break;       }              animationsetFillAfter(true);animationsetDuration(duration);       animationsetStartOffset(((groupgetChildCount()-i) * 100)           / (-1 + groupgetChildCount()));       animationsetInterpolator(new AnticipateInterpolator(2F));       imagevIEwstartAnimation(animation);     }   }  } 

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

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

package comtutorpath;  import androIDappActivity; import androIDosBundle;  public class PathTestActivity extends Activity {    @OverrIDe   public voID onCreate(Bundle savedInstanceState) {     superonCreate(savedInstanceState);     setContentVIEw(Rlayoutmain);     } } 

main.xml代码如下:

<?xml version="0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemasandroIDcom/apk/res/androID"   xmlns:tutor="http://schemasandroIDcom/apk/res/comtutorpath"   androID:layout_wIDth="fill_parent"   androID:layout_height="fill_parent"   androID:orIEntation="vertical" >    <comtutorpathPathMenuVIEw     androID:ID="@+ID/text"     androID:layout_wIDth="fill_parent"     androID:layout_height="fill_parent"     tutor:position="right_bottom"      />  </linearLayout> 

运行点击效果如下:


图1:默认是在右下方这里menuResIDs定义了五个菜单              


图2:点击红色菜单,菜单收回.

下面我们修改main.xml的tutor属性为left_bottom,并且修改PathMenuVIEw.java中的menuResIDs.

tutor:position="left_bottom" 

效果如下:


图3:自定义在左下角,六个菜单。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android高手进阶教程(二十六)之---Android超仿Path菜单的功能实现!全部内容,希望文章能够帮你解决Android高手进阶教程(二十六)之---Android超仿Path菜单的功能实现!所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147715.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存