Android Beginner tutorial pop-up form in animated form

Source: Internet
Author: User

This collection of animation knowledge and pop-up form knowledge, combined to pop up the form in the form of animation.

Animation of the knowledge before a few have done a detailed introduction, you can read the previous writing about the animation blog post. Let's take a quick look at how to pop up the form effect:

First, you need an instance of the form: Popupwindow window = new Popupwindow (contentview, width, height);

A total of three parameters are required, and the meanings of the three parameters are:

/**contentview: The form belongs to a "container" so it fills a layout, so a layout is built specifically for it, which is the view that fills the layout (similar to a custom dialog box). What the form displays
* Width: The width of the layout -2 means wrap_content
* Height: The height of the layout
*/

So to place a view object representing the layout for the first parameter

Contentview = View.inflate (Getapplicationcontext (), r.layout.popup, NULL);

Final Display form:

Window.showatlocation (V, gravity.left | Gravity.top, 50, 50);

Well, the basic API of the form is finished, so look at the code for the show form:

Package Com.itydl.showwindow;import Android.os.bundle;import Android.app.activity;import android.view.Gravity; Import Android.view.menu;import Android.view.view;import Android.widget.popupwindow;public class MainActivity Extends Activity {private View contentview;//form displays content that represents a layout to be placed private Popupwindow window;//get Form Object @override protecte        d void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Initpopup ();//Initialize Component}/** * Initialize popup form */private void Initpopup () {//Get layout View Contentview = view.inflate (getappl        Icationcontext (), r.layout.popup, NULL); /**contentview: The form belongs to a "container" so it fills a layout, so a layout is built specifically for it, which is the view that fills the layout (similar to a custom dialog box). What the form displays * Width: the width of the layout -2 means wrap_content * Height: high */window of layout = new Popupwindow (Contentview,-2,-2);//Get Form Object}/** * Point Click button Pop-up form * @param v */public void Popupwindow (View v) {//Display form window.showatlocation (V, gravity.left |   Gravity.top, 50, 50); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the        Action Bar if it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true; }    }
Run the program as follows:

Press the Back button and you'll see the program crashes. This is because all the resources of the form must be freed when the popup form returns, so put it in the ondestry () to bind it to the activity:

@Override    protected void OnDestroy () {    if (window!=null && window.isshowing ()) {    //close along with return interaction closed    Window.dismiss ();    window=null;    }    Super.ondestroy ();    }

Above the pop-up form basic methods introduced almost no next change some parameters, and add the animation together, to achieve the effect of the animation popup:

Directly on the complete code:

Package Com.itydl.showwindow;import Android.os.bundle;import Android.app.activity;import android.graphics.Color; Import Android.graphics.drawable.colordrawable;import Android.view.gravity;import Android.view.menu;import Android.view.view;import Android.view.animation.animation;import Android.view.animation.scaleanimation;import Android.widget.popupwindow;public class Mainactivity extends Activity {private View contentview;//form displays content, representing a layout pr to be placed Ivate popupwindow window;//Get Form Object Private scaleanimation sa;//add animation @override protected void OnCreate (Bundle savedinstanc        EState) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Initpopup ();//Initialize Component}/** * Initialize popup form */private void Initpopup () {//Get layout View Contentview = view.inflate (getappl        Icationcontext (), r.layout.popup, NULL); /**contentview: The form belongs to a "container" so it fills a layout, so a layout is built specifically for it, which is the view that fills the layout (similar to a custom dialog box). What the form displays * Width: the width of the layout -2 indicates wrap_content * Height: high */window of layout = new PoPupwindow (Contentview,-2,-2);//Get Form Object////Add animation sa = new Scaleanimation (1, 1, 0, 1, animation.relative_to_self,0.5f, animation.relative_to_self,0f); sa.setduration (3000);//Set Playback duration 3s}/** * Click the button popup form * @param v */public void pop        Upwindow (View v) {if (Window!=null && window.isshowing ()) {//Judgment statement, implement click button to appear form, then click to disappear Window.dismiss (); }else{int[] location = new Int[2];//location[0] represents the x-axis, location[1] represents the y-axis//Gets the coordinates of the current view component (V is the button), and the coordinates are encapsulated in the parameter array v. Getlocationinwindow (location);//To animate a form, you must set a background for the form if no background animation is displayed. Window.setbackgrounddrawable (New colordrawable (color.transparent));//Set a transparent background contentview.setanimation (SA);// The animation effect gives the/** form on which component * 1, relative parent component * 2, Alignment * 3, x-coordinate * 4, y-coordinate *//Display form settings The form is displayed in the lower-right corner of the button. Gets the coordinates of the current view component, plus its width height as the new coordinate window.showatlocation (V, gravity.left |    Gravity.top, Location[0]+v.getwidth (), Location[1]+v.getheight ()); }} @Override protected void OnDestroy () {if (Window!=null && window.isshowing ()) {//followed by returnInteractive close together close Window.dismiss ();    Window=null;    } Super.ondestroy (); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the A        Ction Bar if it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true; }    }

This enables the form to animate the form of presentation. Here is a list of the animations that are being played:



Android Beginner tutorial pop-up form in animated form

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.