PopupWindow实现底部滑出菜单

1、xml资源文件

1.主布局activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/main"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" >  

    <Button  
        android:id="@+id/open"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerHorizontal="true"  
        android:text="打开泡泡窗口" />  

</RelativeLayout>

2.popupwindow.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="@android:color/white"
    android:padding="20dp"
    android:orientation="vertical">
    <RadioGroup
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:text="全部拦截"
            android:id="@+id/rb_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:text="电话拦截"
            android:id="@+id/rb_call"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:text="电话拦截"
            android:id="@+id/rb_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>
    <LinearLayout
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:text="修改"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:text="取消"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>

3.样式文件:style.xml

<style name="popWindow_animation">  
       <item name="android:windowEnterAnimation">@anim/popupwindow_enter</item>  
       <item name="android:windowExitAnimation">@anim/popupwindow_exit</item>  
   </style> 

4.泡泡窗体进入动画:popupwindow_enter.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.5"  
        android:toAlpha="1.0" />  

</set> 

5.泡泡窗体进入动画:popupwindow_exit.xml

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android" >  

    <alpha  
        android:duration="500"  
        android:fromAlpha="1.0"  
        android:toAlpha="0.5" />   
    <translate  
        android:fromYDelta="0"  
        android:toYDelta="100%p"  
        android:duration="500" />  

</set

2、java代码

1.初始化泡泡窗体设置

    private void initPopupWindow() {
        View popView = getLayoutInflater().inflate(R.layout.popupwindow, null);
        popupWindow = new PopupWindow(popView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
    }

2.显示泡泡窗体

    private void showPoPupWindow(View parent) {
        popupWindow.setAnimationStyle(R.style.popWindow_animation);
        popupWindow.showAtLocation(parent, Gravity.BOTTOM,0,0);//parent为泡泡窗体所在的父容器view
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用TranslateAnimation来实现PopupWindow底部入和的动画效果。 以下是一个示例代码: ```java // PopupWindow底部入动画 public void showPopupWindow(View anchorView, View popupView) { PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setOutsideTouchable(true); int[] location = new int[2]; anchorView.getLocationOnScreen(location); TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, popupView.getHeight(), 0); translateAnimation.setDuration(300); popupView.startAnimation(translateAnimation); popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, location[0], location[1] + anchorView.getHeight()); } // PopupWindow底部动画 public void dismissPopupWindow(View popupView) { TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, popupView.getHeight()); translateAnimation.setDuration(300); popupView.startAnimation(translateAnimation); translateAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) {} @Override public void onAnimationEnd(Animation animation) { popupView.postDelayed(() -> popupWindow.dismiss(), 50); } @Override public void onAnimationRepeat(Animation animation) {} }); } ``` 在showPopupWindow方法中,我们创建了一个PopupWindow,并使用TranslateAnimation实现底部入的动画效果。 在dismissPopupWindow方法中,我们同样使用了TranslateAnimation,不过这次是实现底部的动画效果。注意在动画结束后,我们使用postDelayed方法来延迟50毫秒关闭PopupWindow,以确保动画可以完全结束再关闭。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值