化繁為簡(jiǎn)之SimplePopupWindow

最近項(xiàng)目里總是用到PopupWindow,功能不是很復(fù)雜但是寫起來很繁瑣、代碼很亂,所以就自己封裝了一個(gè)SimplePopupWindow,基于Builder模式。

功能

  • 可點(diǎn)擊空白地方或者返回鍵使PopupWindow消失
  • 可設(shè)置并調(diào)節(jié)半透明黑色背景,并且PopupWindow消失后背景色自動(dòng)恢復(fù)
  • 不會(huì)被軟鍵盤覆蓋
  • 可設(shè)置是否同時(shí)彈出軟鍵盤
  • 可設(shè)置進(jìn)出動(dòng)畫

一個(gè)簡(jiǎn)單帶動(dòng)畫的PopupWindow彈出

image

只需四行代碼加一個(gè)接口回調(diào)

SimplePopupWindow.with(MainActivity.this)
    .setView(R.layout.popup_window_main)  //設(shè)置你的視圖資源文件
    .setAnimationStyle(R.style.anim_simple_popup_window)  //設(shè)置你的動(dòng)畫資源文件
    .show(new SimplePopupWindow.Callback() {
    @Override
    public void getView(View view, PopupWindow popupWindow) {
        //view是你資源文件生成的視圖
    
    }
});

NOTE:后面放出動(dòng)畫的資源文件

帶半透明灰色背景

image
SimplePopupWindow.with(MainActivity.this)
    .setView(R.layout.popup_window_main)
    .setBackgroundAlpha(0.4f)  //設(shè)置你的背景透明度
    .show(new SimplePopupWindow.Callback() {
    @Override
    public void getView(View view, PopupWindow popupWindow) {
    
    }
});

API文檔

方法 用途
with(Activity activity) 初始化
show(Callback callback) 彈出PopupWindow
setView(int ResId) 設(shè)置視圖的資源文件
setLocation(int Lacation) 設(shè)置彈出位置
setBackgroundAlpha(float bgAlpha) 設(shè)置黑色背景透明度
setAnimationStyle(int animationStyle) 設(shè)置動(dòng)畫的資源文件
setAutoPopupInput(boolean isShowInput) 設(shè)置是否自動(dòng)彈出軟鍵盤

Note:

  • 不要在Activity的onCreate()方法中直接show(),因?yàn)榇藭r(shí)Activity的視圖開沒有繪制完成。
  • 一定要在show()之前調(diào)用setView()。
  • layout布局文件要設(shè)置固定高度或wrap_content

SimplePopupWindow代碼

//直接新建SimplePopupWindow類拷貝過去
public class SimplePopupWindow {
private Activity mActivity;
private View mView;
private boolean mIsShowInput;
private int animationStyle;
private int LOCATION = -1;


public static SimplePopupWindow with(Activity activity) {
    return new SimplePopupWindow(activity);
}


public SimplePopupWindow(Activity activity) {
    mActivity = activity;
    animationStyle = -1;
}


//設(shè)置彈出位置,默認(rèn)底部彈出
public void setLocation(int Lacation){
    LOCATION = Lacation;
}


//視圖資源,必須設(shè)置
public SimplePopupWindow setView(int ResId) {
    mView = LayoutInflater.from(mActivity).inflate(ResId, null);
    return this;
}


//彈出PopupWindow
public void show(Callback callback) {
    if (mView == null){
        Toast.makeText(mActivity,"請(qǐng)?jiān)O(shè)置View", Toast.LENGTH_SHORT).show();
        return;
    }

    PopupWindow window = new PopupWindow(mView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

    if (animationStyle != -1)  //如果設(shè)置了動(dòng)畫,則啟用動(dòng)畫
        window.setAnimationStyle(animationStyle);

    window.setBackgroundDrawable(new ColorDrawable());

    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);  //不會(huì)被軟鍵盤覆蓋

    window.setOnDismissListener(new PopupWindow.OnDismissListener() { //彈窗消失時(shí)恢復(fù)背景色
        @Override
        public void onDismiss() {
            setBackgroundAlpha(1f);
        }
    });

    callback.getView(mView,window);

    if (LOCATION != -1)
        window.showAtLocation(mActivity.getWindow().getDecorView().findViewById(android.R.id.content), LOCATION, 0, 0);
    else
        window.showAtLocation(mActivity.getWindow().getDecorView().findViewById(android.R.id.content), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    
    showInPut();
}


//設(shè)置黑色半透明背景,建議設(shè)置0.4f,默認(rèn)不設(shè)置
public SimplePopupWindow setBackgroundAlpha(float bgAlpha) {
    WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
    lp.alpha = bgAlpha;
    if (bgAlpha == 1) {
        mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除該Flag的話,在有視頻的頁(yè)面上的視頻會(huì)出現(xiàn)黑屏的bug
    } else {
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代碼主要是解決在華為手機(jī)上半透明效果無效的bug
    }
    mActivity.getWindow().setAttributes(lp);
    return this;
}


//設(shè)置是否自動(dòng)彈出軟鍵盤,默認(rèn)為否
public SimplePopupWindow setAutoPopupInput(boolean isShowInput) {
    mIsShowInput = isShowInput;
    return this;
}


//設(shè)置進(jìn)出動(dòng)畫
public SimplePopupWindow setAnimationStyle(int animationStyle){
    this.animationStyle = animationStyle;
    return this;
}


//顯示軟鍵盤
private void showInPut(){
    if (mIsShowInput){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager)mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 0);
    }
}


//接口回調(diào)
public interface Callback {
    void getView(View view, PopupWindow popupWindow);
  }
}

動(dòng)畫資源文件

transition/out.xml (新建transition或anim目錄)

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="250"  
        android:fromYDelta="0.0"
        android:toYDelta="100%" />
</set>

transition/in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="250"
        android:fromYDelta="100.0%"
        android:toYDelta="0.0" />
</set>

values/styles.xml

<style name="anim_simple_popup_window">
    <item name="android:windowEnterAnimation">@transition/in</item>
    <item name="android:windowExitAnimation">@transition/out</item>
</style>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評(píng)論 25 708
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,389評(píng)論 0 17
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,692評(píng)論 4 61
  • 據(jù)統(tǒng)計(jì)全球,每24人當(dāng)中就1個(gè)無良癥;東亞國(guó)家的無良癥要少于歐美,1000人當(dāng)中才有1個(gè),這個(gè)比例相對(duì)較少,跟我們...
    小唐姐姐閱讀 10,046評(píng)論 0 0
  • 這個(gè)命題是一個(gè)社區(qū)的作業(yè)。 假如只有六個(gè)月的生命,你會(huì)如何度過?思考這個(gè)問題,讓我感覺有點(diǎn)透不過氣來。 六點(diǎn)鐘起床...
    皮皮姐閱讀 5,539評(píng)論 0 3

友情鏈接更多精彩內(nèi)容