Android笔记(2)popwindow

1.建立anim动画文件
(1)建立popshow_anim.xml文件(popwindow显示动画)

<?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>

(2)建立pophidden_anim.xml文件(popwindow退出动画)

<?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="50%p" />

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

2.布局文件pop.xml(此处写弹出的内容)

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

3.Activity文件

public class Activity extends AppCompatActivity {
    private View contentView;
    private PopupWindow popupWindow;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //初始化
            showPopwindow();
            //将背景透明化
            WindowManager.LayoutParams params=GoodsDetail.this.getWindow().getAttributes();
                params.alpha=0.5f;
              GoodsDetail.this.getWindow().setAttributes(params);
              //弹出
                popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
                //退出
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    WindowManager.LayoutParams params=getActivity().getWindow().getAttributes();
                    params.alpha=1f;
                    getActivity().getWindow().setAttributes(params);
                }
        }
        private void showPopwindow() {
        contentView = LayoutInflater.from(GoodsDetail.this).inflate(
                R.layout.pop, null);
        //此处使用contentView.findViewById找pop.xml中控件
        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);// 取得焦点
        //注意  要是点击外部空白处弹框消息  那么必须给弹框设置一个背景色  不然是不起作用的
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //点击外部消失
        popupWindow.setOutsideTouchable(true);
        //设置可以点击
        popupWindow.setTouchable(true);
        //进入退出的动画
        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        //popupWindow隐藏时恢复屏幕正常透明度
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params=GoodsDetail.this.getWindow().getAttributes();
                params.alpha=1f;
                GoodsDetail.this.getWindow().setAttributes(params);
            }
        });
    }
}

4.在style中

<style name="mypopwindow_anim_style">
        <item name="android:windowEnterAnimation">@anim/popshow_anim</item>
        <!-- 指定显示的动画xml -->

        <item name="android:windowExitAnimation">@anim/pophidden_anim</item>
        <!-- 指定消失的动画xml -->
    </style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值