popupwindow你需要的都在这里,看完这篇,你想要什么效果都可以,附送动画解析

效果图如下
<img src="http://img.blog.csdn.net/20160509115354707?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
 public void ok(View view) {
        final TextView textView = new TextView(this);
        textView.setText("马马达");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"xixi",Toast.LENGTH_SHORT).show();
                textView.setText("点击了");
                Log.d("点击:",DisplayUtil.getDisplay(MapsActivity.this.getWindowManager()).getWidth()+":"+DisplayUtil.getDisplay(MapsActivity.this.getWindowManager()).getHeight());

            }
        });
        //创建pop,内容是一个textview,大小是:屏幕宽度*100
        PopupWindow popupWindow = new PopupWindow(textView,DisplayUtil.getDisplay(MapsActivity.this.getWindowManager()).getWidth(), 100);
        //必须可以获取焦点,然后设置一个背景,才可以点击其他地方消失
        popupWindow.setFocusable(true);
        //设置可点击
        popupWindow.setTouchable(true);
        //设置出入动画样式
        popupWindow.setAnimationStyle(R.style.dialog_anim);
        //设置背景,为了可以点击其他地方消失
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.BLUE));
        // popupWindow.showAsDropDown(bt);//这个方法可以让他显示在bt控件的左下方
        popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0,50);//任意位置可设置显示,现在是显示在屏幕的0,50的地方,因为上面有系统通知栏
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.3f; //0.0-1.0,主界面设置透明度,实现背景变灰
        getWindow().setAttributes(lp);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {//消失的时候让主界面恢复变白
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1.0f; //0.0-1.0,主界面设置透明度,实现背景变灰
                getWindow().setAttributes(lp);
            }
        });

    }

附送:
<style name="dialog_anim" parent="@android:style/Animation">
    <item name="android:windowEnterAnimation">@anim/dialog_appear</item>
    <item name="android:windowExitAnimation">@anim/dialog_disappear</item>
</style>
进入动画如下:translate的fromYDelta要注意,此处表示相对于自身高度,-100%就是从上面划出到设置的位置
补充如果是-100%p表示相对于父布局的-100%
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate  android:duration="500"  android:fromYDelta="-100%"  />
    <alpha  android:duration="500"  android:fromAlpha="0.3"  android:interpolator="@android:anim/accelerate_interpolator"  android:toAlpha="1.0" />
</set>
退出动画:同样道理是从现在位置开始,到达-100%,就是向上缩回去了
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate  android:duration="500"  android:toYDelta="-100%"/>

    <alpha  android:duration="500"  android:fromAlpha="1.0"  android:interpolator="@android:anim/accelerate_interpolator"  android:toAlpha="0.5" />

</set>

你可能感兴趣的:(android,动画,PopupWindow,弹出框)