Android中實(shí)現(xiàn)popupwindow(全屏+陰影+動(dòng)畫(huà))

1.點(diǎn)擊PopupWindow讓背景變暗

        PopupWindow popupWindow = new PopupWindow();
View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_popupwindow, null);
        popupWindow.setContentView(view);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow .setBackgroundDrawable(null);
        popupWindow .setFocusable(true);//解決點(diǎn)擊view重新打開(kāi)popwindow的問(wèn)題
        popupWindow .setOutsideTouchable(true);//點(diǎn)擊外部popupwindow消失
        popupWindow.showAtLocation(btn, Gravity.BOTTOM, 0, 0);

        Button bt1 = view.findViewById(R.id.bt1);

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        /**
         * 點(diǎn)擊popupWindow讓背景變暗
         */
        final WindowManager.LayoutParams lp = MainActivity.this.getWindow().getAttributes();
        lp.alpha = 0.7f;//代表透明程度,范圍為0 - 1.0f
        MainActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        MainActivity.this.getWindow().setAttributes(lp);
        /**
         * 退出popupWindow時(shí)取消暗背景
         */
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                lp.alpha = 1.0f;
                MainActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                MainActivity.this.getWindow().setAttributes(lp);
            }
        });

2.popupwindow(全屏+陰影+動(dòng)畫(huà))

        View inflate = LayoutInflater.from(this).inflate(R.layout.layout_pop, null, false);
        final PopupWindow popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        //點(diǎn)擊非菜單部分退出
        inflate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        popupWindow.setBackgroundDrawable(null);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setAnimationStyle(R.style.PopAnimation);

        //相對(duì)于屏幕的位置
        int[] position = new int[2];
        btn1_sendNotifation.getLocationOnScreen(position);
        TextView txt = inflate.findViewById(R.id.txt);

        //獲取狀態(tài)欄高度
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");

        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }

        LinearLayout.LayoutParams par = (LinearLayout.LayoutParams) txt.getLayoutParams();
        par.setMargins(position[0], position[1] + btn1_sendNotifation.getHeight() - result, 0, 0);
        popupWindow.showAtLocation(btn1_sendNotifation, Gravity.NO_GRAVITY, 0, 0);

3.創(chuàng)建anim文件夾 anim_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1500">

    <translate
        android:fromXDelta="100%p"
        android:fromYDelta="100%p"
        android:toXDelta="0"
        android:toYDelta="0"
        />

    <alpha
        android:fromAlpha="0"
        android:toAlpha="1.0" />
</set>

4.anim_exit.xml

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

    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0" />
</set>

4.layout_pop.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="#cccccc"
    android:layout_height="match_parent">

    <TextView
        android:text="你好"
        android:textSize="30sp"
        android:textColor="#00DB00"
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

5.開(kāi)發(fā)中常用的布局

<?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="@dimen/qb_px_152"
    android:layout_gravity="bottom"
    android:background="#f5f5f5"
    android:orientation="vertical">

    <TextView
        android:id="@+id/btn_pop_camera"
        android:layout_width="match_parent"
        android:layout_height="@dimen/qb_px_45"
        android:background="@color/colorWhite"
        android:gravity="center"
        android:text="拍攝"
        android:textSize="@dimen/qb_px_18" />

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/qb_px_1"
        android:background="@color/colorE4" />

    <TextView
        android:id="@+id/btn_pop_album"
        android:layout_width="match_parent"
        android:layout_height="@dimen/qb_px_45"
        android:background="@color/colorWhite"
        android:gravity="center"
        android:text="從相冊(cè)選擇上傳"
        android:textSize="@dimen/qb_px_18" />

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/qb_px_1"
        android:background="@color/colorE4" />

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/qb_px_1"
        android:layout_marginTop="@dimen/qb_px_10"
        android:background="@color/colorE4" />

    <TextView
        android:id="@+id/btn_pop_cancel"
        android:layout_width="match_parent"
        android:layout_height="@dimen/qb_px_45"
        android:background="@color/colorWhite"
        android:gravity="center"
        android:text="取消"
        android:textSize="@dimen/qb_px_18" />

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/qb_px_1"
        android:background="@color/colorE4" />
</LinearLayout>
最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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