popupwindow

使用

构建popupwindow

int width = LayoutParams.WRAP_CONTENT;
int height = LayoutParams.WRAP_CONTENT;
//获取要popupwindow对象—并设置参数
// 参1 要弹出的view 参2 宽 参3高
PopupWindow popupWindow = new PopupWindow(convertview, width, height);
// 点击popupwindow外的地方不消失,点击返回键应用退出,说明焦点在activity上.
// 设置背景--点击外面会消失,点击返回键也会消失(如果不设置,不消失)
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.RED));
// 点击popupwindow外的地方不消失,点击返回键应用退出,说明焦点在activity上.

让其按返回键不退出也不消失

//让其有焦点--按返回键不退出了,但也不消失
// 是因为你没有设置popupwindow的背景
popupWindow.setFocusable(true);

三种show的方式

//第一种 参1 锚点 :view 即:显示到这个view的下方
popupWindow.showAsDropDown(tv);
//第二种 参1view 参2x轴的偏移量 参3Y轴的偏移量
// 默认显示在参一的下方
popupWindow.showAsDropDown(convertview, 100, 100);
//第三种 参1输入当前activity里的任意一个view--为了找到当前activity ,window的一个标识
// 参2 x轴的偏移量
// 参3 Y轴的偏移量
// 参4 重心位置显示的大概位置,可以加或|来再更加确定其位置
// 默认是上下居中的,且是全屏的
popupWindow.showAsDropDown(tv, 0, 0, Gravity.RIGHT | Gravity.TOP);

设置动画样式

popupWindow.setAnimationStyle(R.style.PopAnimation);

案例

    protected void ShowPop(View view) {
        //----------------构造pop--------------
        //将布局转成view
        View contentView = View.inflate(getApplicationContext(), R.layout.pop_view, null);

        //宽高是WRAP_CONTENT
        int width = LayoutParams.WRAP_CONTENT;
        int height = LayoutParams.WRAP_CONTENT;
        //获取popupwindow对象
        // 参1 要现实的弹出框的view 参2 宽 参3高
        PopupWindow popupWindow = new PopupWindow(contentView, width, height);

        //----------------一般性设置--------------
        //按返回键不退出了,但也不消失
        popupWindow.setFocusable(true);// 让有焦点
        //设置背景 如果不设置 点击其它区域不能消失 如果不需要 就设置为透明
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        //----------------设置动画样式--------------
        popupWindow.setAnimationStyle(R.style.PopAnimation);

        //----------------显示--------------
        // 锚点 显示在描点的下面
        // popupWindow.showAsDropDown(view);
        // 参2 x轴偏移量 参3 y轴偏移量
        //要向右和向上挪动
        popupWindow.showAsDropDown(view, 60, -60);
        // 参1 输入当前activity里的任意一个view 参2重心位置 参3x轴偏移量参4y轴偏移量
        // popupWindow.showAtLocation(tv, Gravity.RIGHT | Gravity.TOP, 0, 100);

    }

进入和退出动画
enter

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="1000"
        android:fromXDelta="100%"
        android:interpolator="@anim/overshoot"
        android:toXDelta="0" />
    <alpha
        android:duration="1000"
        android:fromAlpha="0.5"
        android:interpolator="@anim/decelerate_cubic"
        android:toAlpha="1.0" />
</set>

exit

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="1000"
        android:fromXDelta="0"
        android:interpolator="@anim/anticipate"
        android:toXDelta="100%" />
    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:interpolator="@anim/accelerate_cubic"
        android:toAlpha="0.0" />
</set>

插补器
到sdk下面拷贝
使用Androidstudio--将其放在res/anim目录下
eclipse放在res/interpolator目录下
android-sdk-windows\platforms\android-20\data\res\interpolator
Anticipate.xml和Overshoot.xml
anticipate.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
/*
**
** Copyright 2009, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
-->

<anticipateInterpolator />

accelerate_cubic.xml

<?xml version="1.0" encoding="utf-8"?><!--
/*
** Copyright 2010, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
-->

<accelerateInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:factor="1.5" />

添加动画样式--然后在代码中添加

    <style name="PopAnimation">
        <item name="android:windowEnterAnimation">@anim/pop_enter</item>
        <item name="android:windowExitAnimation">@anim/pop_exit</item>
    </style>

修改弹出pop的背景

    //弹出pop前后修改背景色
    private void setBackgroundAlpha(float from, float to, int duration) {
        final WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        ValueAnimator animator = ValueAnimator.ofFloat(from, to);
        animator.setDuration(duration);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                lp.alpha = (float) animation.getAnimatedValue();
                mActivity.getWindow().setAttributes(lp);
            }
        });
        animator.start();
    }
//或者
    public void backgroundAlpaha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow()
                .addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

消失后将背景透明度还原

        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(alpha, 1f, 300);
            }
        });

修改pop的宽高

和View(锚点)一样宽

       int width = view.getWidth();
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        mPopupWindow = new PopupWindow(mTopPop, width, height);

全屏宽

        int width = LinearLayout.LayoutParams.MATCH_PARENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        mPopupWindow = new PopupWindow(mTopPop, width, height);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容