PopupWindow的基本使用

寫在前面的話

PopupWindow 對于我們來說已經(jīng)是再熟悉不過了,但是今天在涉及到一個需要用到 PopupWindow 的功能的時候帅掘,在顯示PopupWindow 位置的時候卻出問題了,干開發(fā)時間也不短了堂油,以前在 用到 PopupWindow的時候修档,總是馬虎的應(yīng)付過,雖然功能是實現(xiàn)了府框,但是對于PopupWindow的一些基本使用還是模模糊糊吱窝,感到很難堪啊,再加上以前封裝的關(guān)于PopupWindow位置顯示的類今天在使用的時候竟然出現(xiàn)了些問題迫靖,實在不得不把它拿出來重新弄弄院峡,也算是做個復(fù)習(xí)吧。

本篇文章要講解的重點是

  • PopupWindow 基于 view 的位置顯示系宜,如顯示在view左邊撕予,右邊,上邊蜈首,下邊
  • PopupWindow 彈出動畫的方式实抡,從上到下,等等欢策。
    整理的原因是避免每次要用到的時候吆寨,都在網(wǎng)上翻來覆去的找。

ok踩寇,下面就開始正式講解了啄清。

第一步,為了方便新建Popuwindow俺孙,我自建了一個BasePopupWindow辣卒,里面封裝了PopupWindow 基于 view 位置顯示的方法,下面看看 BasePopupWindow.java 代碼吧:
/***
 * PopupWindow基類
 * 
 * @author pei
 * @version 1.0
 * @cretae 2016-7-21
 * @注:若要popwindow點擊外部消失睛榄,則設(shè)置 this.setFocusable(true)
 *     若要popwindow點擊外部不消失荣茫,不做setFocusable設(shè)置,也不要設(shè)置成this.setFocusable(false)
 * 
 */
public abstract class BasePopupWindow extends PopupWindow {

    protected View mLayoutView;
    protected int mLayoutId;
    protected Context mContext;
    protected int mWidth;
    protected int mHeight;

    public BasePopupWindow(int width, int height, int layoutId, Context context) {
        this.mWidth = width;
        this.mHeight = height;
        this.mLayoutId = layoutId;
        this.mContext = context;
        mLayoutView = LayoutInflater.from(context).inflate(mLayoutId, null);
        setWindow();
    }

    /** PopupWindow基本設(shè)置 **/
    protected void setWindow() {
        this.setContentView(mLayoutView);
        this.setWidth(mWidth);
        this.setHeight(mHeight);
        // this.setFocusable(true);// 可點擊
        // 實例化一個ColorDrawable顏色為半透明(半透明遮罩顏色代碼#66000000)
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
        this.setBackgroundDrawable(dw);
    }

    /** PopupWindow背景設(shè)置 **/
    protected void setBackground(int color) {
        // 實例化一個ColorDrawable顏色為半透明
        ColorDrawable dw = new ColorDrawable(color);
        this.setBackgroundDrawable(dw);
    }

    protected abstract void initView();
    protected abstract void initData();
    protected abstract void setListener();

    /** PopupWindow點擊間隙處理场靴,根據(jù)實際情況重寫 **/
    protected void onTouchdimiss() {
        // mMenuView添加OnTouchListener監(jiān)聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
        mLayoutView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
//              int height = mLayoutView.getTop();
//              int y = (int) event.getY();
//              if (event.getAction() == MotionEvent.ACTION_UP) {
//                  if (y < height) {
//                      dismiss();
//                  }
//              }
                return false;
            }
        });
    }

    /**
     * 顯示在控件正上方
     * 
     * @param view
     *            依賴的控件
     * @param marginDp
     *            設(shè)置的間距(直接寫數(shù)字即可啡莉,已經(jīng)做過dp2px轉(zhuǎn)換)
     */
    public void showAtLocationTop(View view, float marginDp) {
        mLayoutView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        int popupWidth = mLayoutView.getMeasuredWidth();
        int popupHeight = mLayoutView.getMeasuredHeight();
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        showAtLocation(view, Gravity.NO_GRAVITY, (location[0] + view.getWidth() / 2) - popupWidth / 2, location[1] - popupHeight - dp2px(marginDp));
        update();
    }

    /**
     * 顯示在控件正下方
     * 
     * @param view
     *            依賴的控件
     * @param marginDp
     *            設(shè)置的間距(直接寫數(shù)字即可港准,已經(jīng)做過dp2px轉(zhuǎn)換)
     */
    public void showAtLocationGravityBottom(View view, float marginDp) {
        mLayoutView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        int popupWidth = mLayoutView.getMeasuredWidth();
        int popupHeight = mLayoutView.getMeasuredHeight();
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        showAtLocation(view, Gravity.NO_GRAVITY, (location[0]+view.getWidth()/2)-popupWidth/2,
                location[1]+view.getHeight()+dp2px(marginDp));
        update();
    }

    /**顯示在控件下方
     *
     * @param view 依賴的控件
     * @param marginDp  設(shè)置的間距(直接寫數(shù)字即可,已經(jīng)做過dp2px轉(zhuǎn)換)
     */
    public void showAtLocationBottom(View view, float marginDp){
        showAsDropDown(view, 0, dp2px(marginDp));
        update();
    }


    /**
     * 顯示在控件左方
     * 
     * @param view
     *            依賴的控件
     * @param marginDp
     *            設(shè)置的間距(直接寫數(shù)字即可咧欣,已經(jīng)做過dp2px轉(zhuǎn)換)
     */
    public void showAtLocationLeft(View view, float marginDp) {
        mLayoutView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        int popupWidth = mLayoutView.getMeasuredWidth();
        int popupHeight = mLayoutView.getMeasuredHeight();
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        showAtLocation(view, Gravity.NO_GRAVITY, location[0] - popupWidth - dp2px(marginDp), (location[1] + view.getHeight() / 2) - popupHeight / 2);
        update();
    }

    /**
     * 顯示在控件右方
     * 
     * @param view
     *            依賴的控件
     * @param marginDp
     *            設(shè)置的間距(直接寫數(shù)字即可浅缸,已經(jīng)做過dp2px轉(zhuǎn)換)
     */
    public void showAtLocationRight(View view, float marginDp) {
        mLayoutView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        int popupWidth = mLayoutView.getMeasuredWidth();
        int popupHeight = mLayoutView.getMeasuredHeight();
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        showAtLocation(view, Gravity.NO_GRAVITY, location[0] + view.getWidth() + dp2px(marginDp), (location[1] + view.getHeight() / 2) - popupHeight / 2);
        update();
    }

    /** dp轉(zhuǎn)px **/
    private int dp2px(float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, mContext.getResources().getDisplayMetrics());
    }

    /** 通過id獲得view **/
    @SuppressWarnings("unchecked")
    protected <T extends View> T getView(int viewId) {
        View view = null;
        if (mLayoutView == null) {
            mLayoutView = LayoutInflater.from(mContext).inflate(mLayoutId, null);
        }
        view = mLayoutView.findViewById(viewId);
        return (T) view;
    }
}
第二步,寫一個簡單的TestPop繼承BasePopuWindow魄咕,用于做測試衩椒,TestPop 代碼如下:
/**
 * Instruction:用于測試的popuwindow
 * <p>
 * Author:pei
 * Date: 2017/6/28
 * Description:
 */


public class TestPop extends BasePopupWindow{

    public TestPop(Context context) {
        super(ScreenUtil.dp2px(100,context), ScreenUtil.dp2px(50,context), R.layout.pop_order, context);

        initView();
        initData();
        setListener();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        setFocusable(true);
        setAnimationStyle(R.style.popuwindow_up_style);//popuwindow顯示隱藏的動畫

    }

    @Override
    protected void setListener(){

    }
}

需要說明的是,為了方便測試哮兰,我TestPop設(shè)的固定大小

width=ScreenUtil.dp2px(100,context);
hight=ScreenUtil.dp2px(50,context);

TestPop 涉及到的popuwindow的布局 layout.pop_order.xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_c0c0c0">

</LinearLayout>
第三步毛萌,mainactivity中的測試代碼很簡單,就是寫了一個點擊事件用來控制TestPop的顯示和消失奠蹬。

下面,讓我們來看看嗡午,Mainactivity中的點擊時間是怎么控制TestPop
的:

    @Override
    public void onClick(View v) {
         switch (v.getId()) {
            case R.id.tv_spanner:
                TestPop testPop = new TestPop(mContext);
                if (testPop.isShowing()) {
                    testPop.dismiss();
                } else {
                    testPop.showAtLocationBottom(mTvSpanner, 5);
                }
                break;
            default:
                break;
        }
    }

下面囤躁,讓我們來看看,popwi固定顯示在某個view方位的具體方法:

//顯示在控件正上方
showAtLocationTop(View view, float marginDp)
//顯示在控件正下方(中線對齊)
showAtLocationGravityBottom(View view, float marginDp)
//顯示在控件下方(左邊對齊)
showAtLocationBottom(View view, float marginDp)
//顯示在控件左方
showAtLocationLeft(View view, float marginDp)
//顯示在控件右方
showAtLocationRight(View view, float marginDp)
之前講到popuwindow的位置顯示問題荔睹,下面講講popuwindow動畫彈出的問題
  • popuwindow從屏幕上面進入
    在 res/anim 文件夾下建xml文件(anim不存在的話自己創(chuàng)建)
    popu_up_in.xml 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- 從屏幕上面進入 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromYDelta="-100%p"
        android:toYDelta="0" />

    <alpha
        android:duration="500"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

popu_up_out.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- 從屏幕上面退出 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromYDelta="0"
        android:toYDelta="-100%p" />

    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

新建style主題狸演,用于popuwindow引用

   <style name="popuwindow_up_style">
        <item name="android:windowEnterAnimation">@anim/popu_up_in</item>
        <item name="android:windowExitAnimation">@anim/popu_up_out</item>
    </style>

在popuwindow中的引用如下:

setAnimationStyle(R.style.popuwindow_up_style);//popuwindow顯示隱藏的動畫

效果圖如下:

up.gif
  • popuwindow 的left_top效果
    在 res/anim 文件夾下建xml文件(anim不存在的話自己創(chuàng)建)
    popu_left_top_in.xml 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="500"
        android:fillAfter="false"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="0%"
        android:pivotY="0%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

popu_left_top_out.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <scale
        android:duration="500"
        android:fillAfter="false"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="0%"
        android:pivotY="0%"
        android:toXScale="0.0"
        android:toYScale="0.0" />
</set>

新建style主題,用于popuwindow引用

<style name="popuwindow_left_top_style">
        <item name="android:windowEnterAnimation">@anim/popu_left_top_in</item>
        <item name="android:windowExitAnimation">@anim/popu_left_top_out</item>
    </style>

在popuwindow中的引用如下:

setAnimationStyle(R.style.popuwindow_left_top_style);//popuwindow顯示隱藏的動畫

效果圖如下:

left_top.gif
  • popuwindow 的right_top效果
    在 res/anim 文件夾下建xml文件(anim不存在的話自己創(chuàng)建)
    popu_right_top_in.xml 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="500"
        android:fillAfter="false"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="100%"
        android:pivotY="0%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

popu_right_top_out.xml 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <scale
        android:duration="500"
        android:fillAfter="false"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="100%"
        android:pivotY="0%"
        android:toXScale="0.0"
        android:toYScale="0.0" />
</set>

新建style主題僻他,用于popuwindow引用

<style name="popuwindow_right_top_style">
        <item name="android:windowEnterAnimation">@anim/popu_right_top_in</item>
        <item name="android:windowExitAnimation">@anim/popu_right_top_out</item>
    </style>

在popuwindow中的引用如下:

setAnimationStyle(R.style.popuwindow_right_top_style);//popuwindow顯示隱藏的動畫

效果圖如下:

right_top.gif
  • popuwindow 的middle_style效果
    在 res/anim 文件夾下建xml文件(anim不存在的話自己創(chuàng)建)
    popu_middle_in.xml 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="500"
        android:fillAfter="false"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="0%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

popu_middle_out.xml 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <scale
        android:duration="500"
        android:fillAfter="false"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="0%"
        android:toXScale="0.0"
        android:toYScale="0.0" />
</set>

新建style主題宵距,用于popuwindow引用

<style name="popuwindow_middle_style">
        <item name="android:windowEnterAnimation">@anim/popu_middle_in</item>
        <item name="android:windowExitAnimation">@anim/popu_middle_out</item>
    </style>

在popuwindow中的引用如下:

setAnimationStyle(R.style.popuwindow_middle_style);//popuwindow顯示隱藏的動畫

效果圖如下:

middle_style.gif

ok,關(guān)于popuwindow就寫到這里了,謝謝誒吨拗。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末满哪,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子劝篷,更是在濱河造成了極大的恐慌哨鸭,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件娇妓,死亡現(xiàn)場離奇詭異像鸡,居然都是意外死亡,警方通過查閱死者的電腦和手機哈恰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進店門只估,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人着绷,你說我怎么就攤上這事蛔钙。” “怎么了荠医?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵夸楣,是天一觀的道長。 經(jīng)常有香客問我,道長豫喧,這世上最難降的妖魔是什么石洗? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮紧显,結(jié)果婚禮上讲衫,老公的妹妹穿的比我還像新娘。我一直安慰自己孵班,他們只是感情好涉兽,可當(dāng)我...
    茶點故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著篙程,像睡著了一般枷畏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上虱饿,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天拥诡,我揣著相機與錄音,去河邊找鬼氮发。 笑死渴肉,一個胖子當(dāng)著我的面吹牛爽冕,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播乌奇,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼眯娱!你這毒婦竟也來了华弓?” 一聲冷哼從身側(cè)響起困乒,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎娜搂,沒想到半個月后迁霎,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡百宇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了昌粤。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡凄贩,死狀恐怖袱讹,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情捷雕,我是刑警寧澤,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布壶熏,位于F島的核電站浦译,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏淆衷。R本人自食惡果不足惜渤弛,卻給世界環(huán)境...
    茶點故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望晴氨。 院中可真熱鬧碉输,春花似錦、人聲如沸枝哄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至尔店,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間匀哄,已是汗流浹背挑秉。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留曾沈,地道東北人卧土。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓颤霎,卻偏偏與公主長得像,于是被迫代替她去往敵國和親霸琴。 傳聞我的和親對象是個殘疾皇子澎迎,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內(nèi)容