Android底部弹出的popupWindow


import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.kaola.agent.R;

/**
 * @Author: qqyang
 * @Date: 2019/12/25
 * @Description:
 */
public class BasePopupWindow extends PopupWindow {
    /**
     * 显示模式.
     */
    private int showModel;
    /**
     * 先出现 蒙板 列表后弹出.
     */
    public static final int PART = 1;
    /**
     * 列表 和 蒙板 从底部弹出.
     */
    public static final int SIMULTANEOUSLY = 2;

    public static final int SHOW_MODE_DEFAULT = PART;

    private static final float ALPHA_WINDOW = 0.5f; // 蒙板透明度

    private FrameLayout contentView;
    private final TextView tvCancel;

    private Activity activity;

    public BasePopupWindow(Activity activity) {
        this(activity, SHOW_MODE_DEFAULT);
    }

    public BasePopupWindow(Activity activity, int showModel) {
        super(activity);
        this.activity = activity;
        this.showModel = showModel;

        View view = View.inflate(activity, R.layout.popu_base_layout, null);
        tvCancel = view.findViewById(R.id.tv_cancel_base_popup);
        tvCancel.setOnClickListener((v) -> dismiss()); // 销毁popupWindow
        contentView = view.findViewById(R.id.fl_container_base_popup);

        this.setBackgroundDrawable(new BitmapDrawable());
        setContentView(view); //设置填充视图

        setHeight(LinearLayoutCompat.LayoutParams.MATCH_PARENT); // 设置高度
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); //虚拟键适配
        setWidth(LinearLayoutCompat.LayoutParams.MATCH_PARENT); // 设置宽度
        setFocusable(true); // 设置获取焦点,防止软键盘无法弹出。
        setOutsideTouchable(false);
        setAnimationStyle(R.style.popup_base_style); // 设置进入退出动画
        setClippingEnabled(false);
    }


    public void setView(View view) {
        contentView.addView(view);
    }

    /**
     * 是否显示 底部取消按钮。
     */
    public void setCancelViewVisible(boolean isShowCancelView) {
        if (isShowCancelView) {
            tvCancel.setVisibility(View.VISIBLE);
        } else {
            tvCancel.setVisibility(View.GONE);
        }
    }

    /**
     * 显示 popupWindow.
     */
    public void show() {
        switch (showModel) {
            case PART:
                setBackgroundAlpha(1.0f, ALPHA_WINDOW); //设置屏幕透明度
                break;
            case SIMULTANEOUSLY:

                break;
            default:
                setBackgroundAlpha(1.0f, ALPHA_WINDOW);//设置屏幕透明度
                break;
        }
        this.showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }

    public void showWithNotAlpha() {
        this.showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        setBackgroundAlpha(ALPHA_WINDOW, 1.0f);
    }

    public void dismissNoAnim() {
        super.dismiss();
    }

    /**
     * 设置添加屏幕的背景透明度
     */
    private void setBackgroundAlpha(float startAlpha, float endAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(startAlpha, endAlpha);
        valueAnimator.setDuration(300L);
        valueAnimator.addUpdateListener(animation -> {
            lp.alpha = (float) animation.getAnimatedValue();
            activity.getWindow().setAttributes(lp);
        });
        valueAnimator.start();
    }
}

View 的 布局:

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

    <FrameLayout
        android:id="@+id/fl_container_base_popup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:background="#ffffff"
        tools:minHeight="300dp" />

    <TextView
        android:id="@+id/tv_cancel_base_popup"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_marginTop="10dp"
        android:background="#ffffff"
        android:gravity="center"
        android:text="@string/cancel"
        android:textColor="@color/gray_4"
        android:textSize="@dimen/font_16" />

</LinearLayout>


style :

    <style name="dialog_bottom_default" parent="Animation.AppCompat.Dialog">
        <item name="android:windowEnterAnimation">@anim/dialog_bottom_enter</item>
        <item name="android:windowExitAnimation">@anim/dialog_bottom_exit</item>
    </style>

dialog_botton_enter:

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

dialog_bottom_exit:

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值