Android popupwindow animation instance

Source: Internet
Author: User

Hello everyone, today I will share with you the animation used for popupwindow. Let's take a look at the directory structure:

Mainactivity. Java File

package com.tracy.test;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.PopupWindow;import android.widget.LinearLayout.LayoutParams;public class MainActivity extends Activity {    /** Called when the activity is first created. */    boolean flag = false;    PopupWindow popupWindow;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        init();    }    public void init() {        Button btn_show = (Button) findViewById(R.id.btn_show);        LayoutInflater inflater = LayoutInflater.from(this);        View layout = inflater.inflate(R.layout.popup, null);        popupWindow = new PopupWindow(layout, LayoutParams.FILL_PARENT,                LayoutParams.FILL_PARENT);        Button btn_dismiss = (Button) layout.findViewById(R.id.btn_dismiss);        btn_dismiss.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                openMenu();            }        });        btn_show.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                openMenu();            }        });    }    public void btn_showOnClicked() {        openMenu();    }    public void btn_dismissOnClicked() {        openMenu();    }    public void openMenu() {        if (!flag) {                    popupWindow.setAnimationStyle(R.style.PopupAnimation);            popupWindow.showAtLocation(findViewById(R.id.btn_show), Gravity.NO_GRAVITY, 0, 0);            popupWindow.setFocusable(true);            popupWindow.update();            flag = true;        } else {            popupWindow.dismiss();            popupWindow.setFocusable(false);            flag = false;        }    }}

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"    android:id="@+id/layout"    >    <Button        android:id="@+id/btn_show"        android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="show"        /></RelativeLayout>

Popup. xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"    android:layout_width="fill_parent"     android:layout_height="fill_parent"    android:background="#cccccc"     >     <Button         android:id="@+id/btn_dismiss"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="dismiss"/></LinearLayout>

Styles. XML is placed in the Res/values directory. Pay attention to this, or you will not be able to see the animation effect.

<?xml version="1.0" encoding="utf-8"?><resources>    <style name="PopupAnimation">        <item name="android:windowEnterAnimation">@anim/in</item>        <item name="android:windowExitAnimation">@anim/out</item>    </style></resources>

Anim/in. xml

<?xml version="1.0" encoding="utf-8"?>    <set xmlns:android="http://schemas.android.com/apk/res/android">        <translate android:interpolator="@android:anim/accelerate_decelerate_interpolator"           android:fromYDelta="-100"           android:toYDelta="0"           android:duration="1000"           android:fillEnabled="true"           android:fillAfter="true"           />            <scale android:fromXScale="0.6" android:toXScale="1.0"                    android:fromYScale="0.6" android:toYScale="1.0" android:pivotX="50%"                    android:pivotY="50%" android:duration="2000" />            <alpha android:interpolator="@android:anim/decelerate_interpolator"                    android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />    </set>  

Anim/out. xml

<?xml version="1.0" encoding="utf-8"?>    <set xmlns:android="http://schemas.android.com/apk/res/android"        android:oneshot="true"        >        <translate android:interpolator="@android:anim/accelerate_decelerate_interpolator"           android:fromYDelta="0"           android:toYDelta="-100"           android:duration="1000"           android:fillEnabled="true"           android:fillAfter="true"           />                   <scale android:fromXScale="1.0" android:toXScale="0.4"                    android:fromYScale="1.0" android:toYScale="0.4" android:pivotX="50%"                    android:pivotY="50%" android:duration="2000" />        <alpha android:interpolator="@android:anim/decelerate_interpolator"           android:fromAlpha="1.0"            android:toAlpha="0.0"            android:duration="2000"            />    </set> 

The effect is good or bad. You can tune it yourself. Haha, manifest. xml won't be pasted out.

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.