PopupWindow不同方向弹出

作者: 洪生鹏 | 来源:发表于2016-08-24 15:30 被阅读1369次

之前写过五分钟教你学会PopupWindow
有人问要设置从顶部弹出的,现在归纳从不同方向弹出的PopupWindow
效果:

left.gif
先看部分关键地方
/**
     * 初始化PopupWindow
     * @param type
     */
    protected void initPopupWindow(int type) {
        View popupWindowView = getLayoutInflater().inflate(R.layout.popupwindow, null);
        ColorDrawable dw = new ColorDrawable(0xffffffff);
        if (Direction.BOTTOM.ordinal() == type) {
            popupWindow = new PopupWindow(popupWindowView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
            popupWindow.setAnimationStyle(R.style.BottomFade);
            popupWindow.setBackgroundDrawable(dw);
            popupWindow.showAtLocation(btnOpen, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        } else if (Direction.TOP.ordinal() == type) {
            popupWindow = new PopupWindow(popupWindowView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
            popupWindow.setAnimationStyle(R.style.TopFade);
            popupWindow.setBackgroundDrawable(dw);
            popupWindow.showAtLocation(btnOpen, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
        } else if (Direction.LEFT.ordinal() == type) {
            popupWindow = new PopupWindow(popupWindowView, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, true);
            popupWindow.setAnimationStyle(R.style.LeftFade);
            popupWindow.setBackgroundDrawable(dw);
            popupWindow.showAtLocation(btnOpen, Gravity.LEFT, 0, 500);
        } else {
            popupWindow = new PopupWindow(popupWindowView, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, true);
            popupWindow.setAnimationStyle(R.style.RightFade);
            popupWindow.setBackgroundDrawable(dw);
            popupWindow.showAtLocation(btnOpen, Gravity.RIGHT, 0, 500);
        }
        backgroundAlpha(0.5f);
        popupWindow.setOnDismissListener(new popupDismissListener());
        popupWindowView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        TextView tv_close = (TextView) popupWindowView.findViewById(R.id.tv_close);
        tv_close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }
 /**
     * 设置透明度
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }
    /**
     * 弹出方向
     */
    public enum Direction {
        LEFT,
        RIGHT,
        TOP,
        BOTTOM
    }

不同方向的弹出动画#

顶部弹出和消失
顶部弹出 in_toptobottom.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%"
        android:toYDelta="0" />

</set>

顶部弹出消失 out_bottomtotop.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%" />
  
</set>

其他方向弹出的主要是改变 duration fromYDelta toYDelta的值
Style

 <style name="LeftFade">
        <item name="android:windowEnterAnimation">@anim/in_lefttoright</item>
        <item name="android:windowExitAnimation">@anim/out_righttoleft</item>
    </style>

    <style name="RightFade">
        <item name="android:windowEnterAnimation">@anim/in_righttoleft</item>
        <item name="android:windowExitAnimation">@anim/out_lefttoright</item>
    </style>

    <style name="BottomFade">
        <item name="android:windowEnterAnimation">@anim/in_bottomtotop</item>
        <item name="android:windowExitAnimation">@anim/out_toptobottom</item>
    </style>
    <style name="TopFade">
        <item name="android:windowEnterAnimation">@anim/in_toptobottom</item>
        <item name="android:windowExitAnimation">@anim/out_bottomtotop</item>
    </style>

完整的Activity

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 * 微信公众号:aikaifa
 * 开发小助手,长期不断推送优选博文、优秀开源项目。
 * 励志鸡汤源源不断,专治孤独者,找回自我。
 * qq交流群:154950206
 */
public class MainActivity extends Activity implements View.OnClickListener {
    private Context context = null;
    private PopupWindow popupWindow;
    private Button btnOpen;
    private int type = 0;
    private RadioGroup rg_type;

    /**
     * 弹出方向
     */
    public enum Direction {
        LEFT,
        RIGHT,
        TOP,
        BOTTOM
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main);
        btnOpen = (Button) findViewById(R.id.btn_open);
        btnOpen.setOnClickListener(this);
        rg_type = (RadioGroup) findViewById(R.id.rg_type);
        rg_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.left:
                        type = Direction.LEFT.ordinal();
                        break;
                    case R.id.right:
                        type = Direction.RIGHT.ordinal();
                        break;
                    case R.id.bottom:
                        type = Direction.BOTTOM.ordinal();
                        break;
                    case R.id.top:
                        type = Direction.TOP.ordinal();
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_open: {
                initPopupWindow(type);
                break;
            }
        }
    }

    class popupDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }
    }

    /**
     * 初始化PopupWindow
     * @param type
     */
    protected void initPopupWindow(int type) {
        View popupWindowView = getLayoutInflater().inflate(R.layout.popupwindow, null);
        ColorDrawable dw = new ColorDrawable(0xffffffff);
        if (Direction.BOTTOM.ordinal() == type) {
            popupWindow = new PopupWindow(popupWindowView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
            popupWindow.setAnimationStyle(R.style.BottomFade);
            popupWindow.setBackgroundDrawable(dw);
            popupWindow.showAtLocation(btnOpen, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        } else if (Direction.TOP.ordinal() == type) {
            popupWindow = new PopupWindow(popupWindowView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
            popupWindow.setAnimationStyle(R.style.TopFade);
            popupWindow.setBackgroundDrawable(dw);
            popupWindow.showAtLocation(btnOpen, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
        } else if (Direction.LEFT.ordinal() == type) {
            popupWindow = new PopupWindow(popupWindowView, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, true);
            popupWindow.setAnimationStyle(R.style.LeftFade);
            popupWindow.setBackgroundDrawable(dw);
            popupWindow.showAtLocation(btnOpen, Gravity.LEFT, 0, 500);
        } else {
            popupWindow = new PopupWindow(popupWindowView, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, true);
            popupWindow.setAnimationStyle(R.style.RightFade);
            popupWindow.setBackgroundDrawable(dw);
            popupWindow.showAtLocation(btnOpen, Gravity.RIGHT, 0, 500);
        }
        backgroundAlpha(0.5f);
        popupWindow.setOnDismissListener(new popupDismissListener());
        popupWindowView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        TextView tv_close = (TextView) popupWindowView.findViewById(R.id.tv_close);
        tv_close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }

    /**
     * 设置透明度
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }

}

activity_main.xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:background="#eeeeee"
    android:padding="10dp"
    android:orientation="vertical">

    <RadioGroup
        android:id="@+id/rg_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="左侧"
            android:checked="true"/>

        <RadioButton
            android:id="@+id/right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="右侧" />

        <RadioButton
            android:id="@+id/bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="底部" />

        <RadioButton
            android:id="@+id/top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="顶部" />
    </RadioGroup>

    <Button
        android:id="@+id/btn_open"
        style="@style/btn_style"
        android:text="弹出PopupWindow" />
</LinearLayout>

----END---

转载请与本人联系 如果觉得喜欢或者对你有帮助,请点个赞吧~

相关文章

网友评论

    本文标题:PopupWindow不同方向弹出

    本文链接:https://www.haomeiwen.com/subject/pfcosttx.html