自定义popupWindow弹出并附带底部弹出位移和渐变动画

一:首先自定义的popupWindow代码如下:

 public class SelectPingLunPopupWindow extends PopupWindow {

    private TextView tv_cancel, tv_send;
    private View mMenuView;
    private EditText et_content;//输入的内容

    public SelectPingLunPopupWindow(Activity context, OnClickListener itemsOnClick) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_pinglun, null);
        tv_cancel = (TextView) mMenuView
                .findViewById(R.id.tv_cancel);
        tv_send = (TextView) mMenuView.findViewById(R.id.tv_send);
        et_content = (EditText) mMenuView.findViewById(R.id.et_content);
        /*cancelButton = (Button) mMenuView
                .findViewById(R.id.btn_help);
         //取消按钮  
        cancelButton.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {  
                //销毁弹出框   
                dismiss();  
            }  
        });  */
      //设置按钮监听  
        tv_send.setOnClickListener(itemsOnClick);  
        tv_cancel.setOnClickListener(itemsOnClick);
      //设置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.mypopwindow_anim_style);  
        //实例化一个ColorDrawable颜色为半透明  
        ColorDrawable dw = new ColorDrawable(0xb0000000);  
        //设置SelectPicPopupWindow弹出窗体的背景   
        this.setBackgroundDrawable(dw);
      //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框  
        mMenuView.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.ll_dialog).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        //证明此时点击的是框的外部
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
    
    //获取输入的内容
    public String getEditContent(){
        return et_content.getEditableText().toString().trim();
    }
}


二:dialog_pinglun.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#636799"
    android:gravity="center"
    android:id="@+id/ll_dialog"
    android:orientation="vertical" >
    
    <LinearLayout
        android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:gravity="center_vertical"
        >
        <TextView
            android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="取消"
    android:textSize="15sp"
    android:textColor="@color/text_login_xingzhi_hui"
    android:layout_marginLeft="10dp"
    android:id="@+id/tv_cancel"
            />
        <TextView
            android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="写评论"
    android:textSize="18sp"
    android:textColor="@color/text_login_xingzhi_hui"
    android:gravity="center_horizontal"
            />
        <TextView
            android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="发送"
    android:textSize="15sp"
    android:textColor="@color/text_login_xingzhi_hui"
    android:gravity="right"
    android:layout_marginRight="10dp"
    android:id="@+id/tv_send"
            />
    </LinearLayout>

    <EditText
        android:layout_width="match_parent"
    android:layout_height="106dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="20dp"
    android:background="#404E75"
    android:id="@+id/et_content"
    android:gravity="top"
    android:textColor="@color/text_login_xingzhi_hui_liang"
    android:textSize="16sp"
        />
</LinearLayout>

三:在activity中某个点击事件下,添加如下代码:

private void getSelectPic() {
        myWindow = new SelectPingLunPopupWindow(this, itemsOnClick);
        myWindow.showAtLocation(ll_parent, Gravity.CENTER, 0, 0);//只显示在相对父控件ll_parent中间位置,如果是底部,那就加上Gravity.BOTTOM
        WindowBackgroundAlphaUtils.backgroundAlpha((FindPingLunActivity)mContext, 0.2f);
        myWindow.setOnDismissListener(new OnDismissListener() {
            
            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub

                 //设置popupWindow监听,当消失的时候,记得把背景变亮。

                WindowBackgroundAlphaUtils.backgroundAlpha((FindPingLunActivity)mContext, 1.0f);
            }
        });
    }
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        public void onClick(View v) {
            myWindow.dismiss();
            switch (v.getId()) {
            case R.id.tv_cancel://popupWindow布局文件中控件id
                
                break;
            case R.id.tv_send://popupWindow布局文件中控件id
                String content = myWindow.getEditContent();
              
                break;
            }
        }
    };

其中WindowBackgroundAlphaUtils.backgroundAlpha((FindPingLunActivity)mContext, 1.0f);是设置弹出popupWindow后背景变亮变暗的效果,也贴出代码来:

public class WindowBackgroundAlphaUtils {
    
    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     */  
    public static void backgroundAlpha(Activity activity,float bgAlpha)  
    {  
        WindowManager.LayoutParams lp =activity.getWindow().getAttributes();  
        lp.alpha = bgAlpha; //0.0-1.0  
        activity.getWindow().setAttributes(lp);  
    }  

}

四:最后一步,添加动画效果

    1 细心的哥们可能已经看到了,在文章最前面第一步创建popupWindow的代码中有这样一句话: this.setAnimationStyle(R.style.mypopwindow_anim_style); 
            对,我们在values中style.xml中加入下面的代码:

             <!--  这个是加入的发现模块弹出评论的动画样式 代码 -->
  <style name="mypopwindow_anim_style">
    <item name="android:windowEnterAnimation">@anim/popshow_anim</item>
 <!-- 指定显示的动画xml -->

    <item name="android:windowExitAnimation">@anim/pophidden_anim</item>
 <!-- 指定消失的动画xml -->
  </style>

    2  在res目录下创建文件夹anim,并在anim文件夹下分别创建popshow_anim.xml和pophidden_anim.xml,代码如下

     popshow_anim.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>

pophidden_anim.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>

ok,这样就完成了附带底部弹出位移动画和渐变动画的popupWindow了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值