Android animated menu based on translateanimation

Source: Internet
Author: User
Tags xml attribute

Post: http://hi.baidu.com/juwend/blog/item/95b98f726fdb04178701b016.html

 

Note that the menu here does not appear in the menu on the machine, but is based on the android SDK. view. animation. translateanimation (extends android. view. animation. animation) class instances are appended to a layout to produce animations and hide menus.

Principle: Layout dynamically moves from the screen (by the screen edge, not necessarily, depending on the initial and last State) to the screen (by the edge outside, you can also stay away. This doesn't matter), so that you can achieve the dynamic menu effect. However, due to some of the strange characteristics of animation (the effects of setfill ** () functions, some of the animations I use do not have the desired effect ), so we still need to use the XML Attribute 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 three values. "visible" is visible, "invisible" is invisible but occupies space, and "gone" is invisible and does not occupy space, you can write an XML file to try it ).

Use of class translateanimation: There are two defining methods for Animation: Java code and XML, here we will only describe the definition using code (because I have never used the one defined using XML .. Hey hey ..). Read the code.

Here is the translateanimationmenu. Java (I also added an animation generated by scaleanimation. You can follow the SDK and program effects to understand it ):
Package com. translateanimation. Menu;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. View. animation. animation;
Import Android. View. animation. translateanimation;
Import Android. widget. Button;
Import Android. widget. linearlayout;

Public class translateanimationmenu extends activity {
/** Called when the activity is first created .*/
// Translateanimation showaction, hideaction;
Animation showaction, hideaction;
Linearlayout menu;
Button button;
Boolean menushowed;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Menu = (linearlayout) findviewbyid (R. Id. Menu );
Button = (button) findviewbyid (R. Id. Button );

// Here is the translateanimation animation.
Showaction = new translateanimation (
Animation. relative_to_self, 0.0f, animation. relative_to_self, 0.0f,
Animation. relative_to_self,-1.0f, animation. relative_to_self, 0.0f );
// 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 );

// Here is the translateanimation animation.
Hideaction = new translateanimation (
Animation. relative_to_self, 0.0f, animation. relative_to_self, 0.0f,
Animation. relative_to_self, 0.0f, animation. relative_to_self,-1.0f );
// 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 (New onclicklistener (){

@ Override
Public void onclick (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"?>
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Textview Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" Android: text = "@ string/Hello"/>
<Linearlayout Android: Id = "@ + ID/menu"
Android: layout_height = "100px" Android: layout_width = "fill_parent"
Android: layout_alignparenttop = "true" Android: Background = "# ffffff">
<Textview Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" Android: text = "I Am a menu"
Android: gravity = "center"/>
</Linearlayout>
<Button Android: 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>

 

 

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.