Android PopupWindow動畫執行個體

來源:互聯網
上載者:User

大家好,今天給大家共用一下對PopupWindow使用動畫,先看一下目錄結構:

MainActivity.java檔案

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放置在res/values目錄下,注意這個,要不然看不到動畫效果

<?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> 

效果的好壞,可以自己調,哈哈,manifest.xml就不貼出來了

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.