Android Development (31) animated Demo-Eject dialog from the bottom of the page, fade down gradually

Source: Internet
Author: User

I want to achieve an effect that gradually pops up from the bottom up. As shown in the following:

1. When you click the Show button, a Dialog dialog box pops up from the bottom.

2. When the dialog is closed, the slow movement of dialog disappears to the bottom. Very smooth effect.

Implementation method:

Write the code in the activity as follows

public class Mainactivity extends Activity {Button button1; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); button1 = (Button) Findviewbyid ( R.id.button1); Button1.setonclicklistener (Monclicklistener);} Onclicklistener Monclicklistener = new View.onclicklistener () {@Overridepublic void OnClick (View v) {//todo auto-generated Method Stub   Alertdialog dialog = new Alertdialog.builder (mainactivity.this)          . Settitle ("title" ). Setmessage ("message"). Create ();  window window = Dialog.getwindow ();  Window.setgravity (gravity.bottom);  Here you can set the location of the dialog display   window.setwindowanimations (r.style.mystyle);  Add animation   dialog.show (); }}; }

Prepare a style resource file to create a mystyle

  <style name= "MyStyle" parent= "android:animation" >        <item name= "@android: Windowenteranimation" >@ anim/dialog_enter</item>        <item name= "@android: Windowexitanimation" > @anim/dialog_exit</item ></style>

Two animations were used in this mystyle, as follows:

Dialog_enter.xml

<?xml version= "1.0" encoding= "Utf-8"?> <set    xmlns:android= "http://schemas.android.com/apk/res/ Android >        <translate            android:duration= "            android:fromydelta" = "100%p"/ >    </set>

Dialog_exit.xml

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" >    <translate        android:duration= "        android:toydelta" = "100%p"/></set >

This completes.

Code Explanation:

1. Set an animated style for the Windows where dialog is located

2. This style specifies the animation (dialog_enter) and the Left animation (dialog_exit) of the form (Windows) entry

3. In the animated dialog_enter of the entry, a translate change was written, specifying the beginning of the 100% position of the parent container. The 100% position of its parent container is outside the screen and is not visible, which is a starting point. Without specifying an end point, the end point is the default itself where it should appear (where it will appear).

4. Similarly, Dialog_exit defines the animation when leaving. The animation does not specify a start position and specifies that the end position is the 100% position of its parent container, so that it slowly disappears out of the screen.


100% indicates the 100% position of its parent container

And if you write

Android:toydelta= "100%", that is, indicates the 100% position of its own.

Reference:

Http://www.linuxidc.com/Linux/2012-04/59153.htm

http://blog.csdn.net/ztp800201/article/details/7387668

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.