Android Translateanimation-based animated dynamic menu (Non-System menu menu)

Source: Internet
Author: User

Note that the menu here does not appear on the menu as it appears on the machine, but is based on the android.view.animation.TranslateAnimation provided by the Android SDK (extends Android.view.animation.Animation) class instance is appended to a Layout to produce a menu of animated and hidden effects.

Principle: Layout (menu) from the screen (next to the screen edge, in fact, not necessarily, depending on the need for the initial and final state) dynamic movement to the outside of the screen (outside can be next to the edge, also can be away from the point, this does not matter), so you can achieve the effect of the dynamic menu. But because of some of the strange characteristics of animation (setfill** () function effect, this in some of the animation I used has not wanted to understand the effect), it is temporarily ignored this thing, so we also need to use the XML properties of Android: Visibility When layout (menu) is displayed, set android:visibility= "visible", when layout (menu) is hidden, set android:visibility= "Gone", here Android: Visibility can have 3 values, "visible" is visible, "invisible" is invisible but occupies space, "gone" is invisible and does not occupy space (so-called Occupy space, this can write an XML to try to understand).

The use of Class translateanimation: There are two ways to define animation, one for Java code, one for XML, and only for code (because I don't use the kind of XML that defines it). Hey.. )。 Don't say much, look at the code.

Here is (I also added the scaleanimation generated animation in the inside, you can follow the SDK and the effect of the program to understand):;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;Importandroid.view.animation.Animation;Importandroid.view.animation.TranslateAnimation;ImportAndroid.widget.Button;Importandroid.widget.LinearLayout; Public classTranslateanimationmenuextendsActivity {/**Called when the activity is first created.*/    //translateanimation showaction, hideaction;Animation showaction, hideaction;    LinearLayout menu;    Button button; Booleanmenushowed; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                 Setcontentview (R.layout.main); Menu=(LinearLayout) Findviewbyid (; Button=(Button) Findviewbyid (; //This is translateanimation animation .Showaction =Newtranslateanimation (animation.relative_to_self,0.0f, Animation.relative_to_self,0.0f, Animation.relative_to_self, -1.0f, Animation.relative_to_self,0.0f); //This is scaleanimation animation .//showaction = new Scaleanimation (//1.0f, 1.0f, 0.0f, 1.0f, Animation.relative_to_self, 0.0f,//animation.relative_to_self, 0.0f); Showaction.setduration (500); //This is translateanimation animation .Hideaction =Newtranslateanimation (animation.relative_to_self,0.0f, Animation.relative_to_self,0.0f, Animation.relative_to_self,0.0f, Animation.relative_to_self,-1.0f); //This is scaleanimation animation .//hideaction = new Scaleanimation (//1.0f, 1.0f, 1.0f, 0.0f,Animation.relative_to_self, 0.0f, Animation.relative_to_self,0.0f); Hideaction.setduration (500); Menushowed=false;         Menu.setvisibility (View.gone); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub               if(menushowed) {menushowed=false;                    Menu.startanimation (hideaction);             Menu.setvisibility (View.gone); }             Else{menushowed=true;                 Menu.startanimation (showaction);            Menu.setvisibility (view.visible);     }          }                                     }); }}

Here is Main.xml:

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" >    <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello" />    <LinearLayoutAndroid:id= "@+id/menu"Android:layout_width= "Fill_parent"Android:layout_height= "100px"Android:layout_alignparenttop= "true"Android:background= "#ffffff" >        <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:gravity= "Center"Android:text= "I am a Menu" />    </LinearLayout>    <ButtonAndroid:id= "@+id/button"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_alignparentbottom= "true"Android:text= "Click to show/hide Menu" /></Relativelayout>

Android Translateanimation-based animated dynamic menu (Non-System menu menu)

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: 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.