android自定义popwindow,并且有从底部弹出的动画

首先自定义popwindow类
pop_bottom.xml为自定义布局

public class CustomPopWindow extends PopupWindow {

        private View mMenuView;
        private ListView listView;
        private TextView tv_ok;
        private TextView tv_pop_title;
        private LinearLayout liner_ok;
        private TextView redpacket_tip;
        private RelativeLayout liner_hasuse_redpacket;
        public CustomPopWindow(Context context) {
                super(context);

                LayoutInflater inflater = (LayoutInflater) context
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                //pop_bottom为自定义布局
                mMenuView = inflater.inflate(R.layout.pop_bottom, null);
                listView=(ListView) mMenuView.findViewById(R.id.listview);
                tv_ok= (TextView) mMenuView.findViewById(R.id.tv_ok);
                tv_pop_title=(TextView) mMenuView.findViewById(R.id.tv_pop_title);
                liner_ok= (LinearLayout) mMenuView.findViewById(R.id.liner_ok);
                redpacket_tip= (TextView) mMenuView.findViewById(R.id.redpacket_tip);
                liner_hasuse_redpacket= (RelativeLayout) mMenuView.findViewById(R.id.liner_hasuse_redpacket);
                //设置SelectPicPopupWindow的View
                this.setContentView(mMenuView);
                //设置SelectPicPopupWindow弹出窗体的宽
                this.setWidth(LayoutParams.MATCH_PARENT);
                //设置SelectPicPopupWindow弹出窗体的高
                this.setHeight(LayoutParams.WRAP_CONTENT);
                //设置SelectPicPopupWindow弹出窗体可点击
                this.setFocusable(true);
                //设置SelectPicPopupWindow弹出窗体动画效果
                this.setAnimationStyle(R.style.popupAnimation);

                //实例化一个ColorDrawable颜色为半透明
                //ColorDrawable dw = new ColorDrawable(0xb0000000);
                //设置SelectPicPopupWindow弹出窗体的背景
                //this.setBackgroundDrawable(dw);
                this.getBackground().setAlpha(0);
                final Activity activity=(Activity) context;
                WindowManager.LayoutParams params=activity.getWindow().getAttributes();  
                params.alpha=0.7f;  
                activity.getWindow().setAttributes(params);  
                //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
                mMenuView.setOnTouchListener(new OnTouchListener() {

                        public boolean onTouch(View v, MotionEvent event) {
                                if(event.getAction()==MotionEvent.ACTION_UP){
                                        dismiss();
                                        WindowManager.LayoutParams params=activity.getWindow().getAttributes();  
                                        params.alpha=1f;  
                                        activity.getWindow().setAttributes(params);  
                                }                                
                                return true;
                        }
                });
                this.setOnDismissListener(new OnDismissListener() {

                    @Override
                    public void onDismiss() {
                         WindowManager.LayoutParams params=activity.getWindow().getAttributes();  
                         params.alpha=1f;  
                         activity.getWindow().setAttributes(params);  
                    }
                });

        }
        public View getView(){
            return mMenuView;
        }

        public ListView getListView() {
                return listView;
        }


        public TextView getTv_pop_title() {
                return tv_pop_title;
        }

        public LinearLayout getLiner_ok() {
                return liner_ok;
        }
        public TextView getRedpacket_tip() {
                return redpacket_tip;
        }

        public RelativeLayout getLiner_hasuse_redpacket() {
                return liner_hasuse_redpacket;
        }
}

R.style.popupAnimation为窗体弹出动画
在res/values文件下建立以下代码

  <style name="popupAnimation" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/in</item>
        <item name="android:windowExitAnimation">@anim/out</item>
    </style>

在res/anim文件下建立动画文件
in.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%p"
        android:toYDelta="0" />
    <alpha
        android:duration="500"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

out.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="50%p" />
    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

在使用CustompopWindow时,直接创建对象获取对应的控件就可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值