Android Master Advanced Tutorial (26)---the Android Super Imitation path menu features! _android

Source: Internet
Author: User

hi~ everyone good, out of business for almost 3 months, everything is good, some time ago spent a part-time job to do a question-and-answer class website Yqma. Want to do China's stackoverflow, haha, just yy, I hope that we support a lot!

Well, today to share is the path menu simple implementation, can support the custom direction (upper left, right, lower right, lower left), and can customize the number of menus, the difficulty is the location of the menu (Dynamic settings margin), as well as the realization of animation, In fact, the animation is simply using a translateanimation,n menu to move together when it feels very cool~

Here also used a custom label, the children's shoes do not know here can see my Android Master Advanced Tutorial (iv)----The use of custom properties Android (Attr.xml,typedarray)! This article. Okay, no more nonsense.

First create an Android project named Pathtest. The directory structure is as follows:

Step two: Under the Values folder, create a new Attrs.xml file with the following code:

<?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>  

Step three: Create a new Pathmenuview.java This is our custom Path menu control with the following code:

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 * Super Imitation path menu * Position defines the location of the menu, currently supported: Upper left, upper right, bottom right, lower left four directions. * Menuresids defines the resource ID of the menu that appears/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; 
   
  /** * The default position is in the lower right corner */private int position = 3; 
   
  /** * that round menu/private ImageView mhome; /** * Context/private ContexT Mcontext; 
   
  /** * The width of the device * * * private int mwidth = 0; 
   
  /** * The height of the equipment * * Private int mheight = 0; 
   
  /** * Equipment Density * * private float mdensity; 
   
  /** * menu is displayed/private Boolean bmenushow; 
  private static int xoffset = 15; 
   
  private static int yoffset =-13; /** * Menu Resource number * * Private int[] Menuresids = {rdrawablecomposer_camera,rdrawablecomposer_music, rdrawable 
   
 
  Composer_sleep,rdrawablecomposer_music,rdrawablecomposer_place}; 
    Public Pathmenuview {Super (context); 
  Setupviews (); 
    Public Pathmenuview (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 (!b 
      Menushow) {Startanimationin (pathmenuviewthis, 300); 
      else {startanimationout (pathmenuviewthis, 300); 
    } bmenushow =!bmenushow; 
   
   
  } 
  }; /** * Menu Hidden Animation * * @param Group * @param duration/private void Startanimationin (ViewGroup group, I NT duration) {for (int i = 1; i < Groupgetchildcount (); i++) {ImageView ImageView = (ImageView) groupgetc 
      Hildat (i); 
      Imageviewsetvisibility (0); Marginlayoutparams MLP = (marginlayoutparams) ImageView 
          Getlayoutparams (); 
       
       
      Animation Animation = null; Switch (position) {Case left_top:animation = new Translateanimation (0f,-mlpleftmargin+xoffset,0f,-mlptop 
        Margin + yoffset); 
      Break 
        Case right_top:animation = new Translateanimation (Mlprightmargin-xoffset,0f,-mlptopmargin + YOffset,0F);      
      Break Case left_bottom:animation = new Translateanimation (0F,-mlpleftmargin+ xoffset, 0F,-yoffset + mlpbottommargin) 
        ; 
         
      Break 
        Case right_bottom:animation = new Translateanimation (Mlprightmargin-xoffset,0f,-yoffset + mlpbottommargin, 0F); 
      Break 
      Default:break; 
      } animationsetfillafter (True); 
      Animationsetduration (duration); 
      Animationsetstartoffset ((I *)/( -1 + groupgetchildcount ()); 
      Animationsetinterpolator (New Overshootinterpolator (2F)); Imageviewstartanimation (Animation); }/** * Menu display animation * * @param Group * @param duration/private void Startanimationout (Vie Wgroup group,int duration) {for (int i = 1; i < Groupgetchildcount (); i++) {final ImageView ImageView = (i 
      Mageview) group Getchildat (i); 
       
      Marginlayoutparams MLP = (marginlayoutparams) imageviewgetlayoutparams (); 
       
      Animation Animation = null; Switch (position) {Case left_top:animation = new Translateanimation (-mlpleftmargin+xoffset,0f,-mlptopmar 
        gin + yoffset,0f); 
      Break 
        Case right_top:animation = new Translateanimation (0f,mlprightmargin-xoffset,0f,-mlptopmargin + yOffset); 
 
      Break 
        Case left_bottom:animation = new Translateanimation (-mlpleftmargin+xoffset,0f,-yoffset + mlpbottommargin,0f); 
 
      Break Case right_bottom:animation = new Translateanimation (0f,mlprightmargin-xoffset, 0f,-yoffset + MLPBOTtommargin); 
      Break 
      Default:break; 
      } animationsetfillafter (True); animationsetduration (duration); 
      Animationsetstartoffset (((Groupgetchildcount ()-i) *)/( -1 + groupgetchildcount ())); 
      Animationsetinterpolator (New Anticipateinterpolator (2F)); 
    Imageviewstartanimation (animation);  } 
  } 
 
}

The fourth step: Pathtestactivity.java and the used layout file Main.xml code as follows:

Pathtestactivity.java (basic unmodified Code) code is as follows:

Package Comtutorpath; 
 
Import androidappactivity; 
Import Androidosbundle; 
 
public class Pathtestactivity extends activity { 
 
  @Override public 
  void OnCreate (Bundle savedinstancestate) { 
    superoncreate (savedinstancestate); 
    Setcontentview (Rlayoutmain);   
  } 

The Main.xml code is as follows:

<?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" 
     /> 
 

Run the Click Effect as follows:

Figure 1: The default is in the lower right here Menuresids defines five menus


Figure 2: Click on the Red menu to retrieve the menu.

Here we modify the Main.xml Tutor property to Left_bottom and modify the Menuresids in Pathmenuview.java.

 
 

The effect is as follows:

Figure 3: Custom in the lower left corner, six menus.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.