本文实例为大家分享了PopWindow实现带动画向下弹出效果的具体代码,供大家参考,具体内容如下
首先建一个popwin的实体类
包dmpte.mytest; 进口android.content.Context; 进口android.view.LayoutInflater; 进口android.view.MotionEvent; 进口android.view.View; 进口android.widget.PopupWindow; 进口android.widget.RelativeLayout; 公开课PopWin延伸PopupWindow { 私人上下文mContext; 私人视图视图; 公共PopWin(最终上下文mContext视图。OnClickListener itemsOnClick int国旗){ 这一点。mContext=mContext; 这一点。视图=LayoutInflater.from (mContext) .inflate (R.layout.view_popwin, null);//设置外部可点击 this.setOutsideTouchable(真正的);/*设置弹出窗口特征*///设置视图 this.setContentView (this.view);//设置弹出窗体的宽和高 this.setHeight (RelativeLayout.LayoutParams.WRAP_CONTENT);//高 this.setWidth (RelativeLayout.LayoutParams.MATCH_PARENT);//宽//设置弹出窗体可点击 this.setFocusable(真正的);//设置弹出窗体显示时的动画,从底部向上弹出 this.setAnimationStyle (R.style.take_photo_anim);//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框 this.view。setOnTouchListener(新View.OnTouchListener () { @Override 公共布尔> & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android " android: id=癅 + id/pop_layout” android: layout_width=" match_parent " android: layout_height=" match_parent " android:背景=" @null " 面向android:=按怪薄北? LinearLayout & lt; android: layout_width=" match_parent " android: layout_height=" 170 dp” android:背景=" #飞行符” 面向android:=按怪薄北? & lt; TextView android: id=癅 + id/tv_jingtai” android: layout_width=" match_parent " android: layout_height=" match_parent " android: layout_gravity="中心" android: layout_marginTop=" 2 dp " android:重力="中心" android:文本="移动静态” android:输入textColor=" # f123 "/比; & lt;/LinearLayout> & lt;/LinearLayout>
接下来是这个类里涉及的动画popwin_anim,在res/价值/风格下
& lt;样式名=" popwin_anim "父=癮ndroid:动画”比; & lt;项目名称=" android: windowEnterAnimation "祝辞@anim/pop_enter_anim & lt;项目名称=" android: windowExitAnimation "祝辞@anim/pop_exit_anim & lt;/style>
然后是进场动画pop_enter_anim和出场动画pop_exit_anim,在res下建一个文件夹似的,分别新建上面两个xml
pop_enter_anim.xml
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt;设置xmlns: android=" http://schemas.android.com/apk/res/android " android: shareInterpolator=" false "比; & lt; !——平移动画——比; & lt;翻译 android:持续时间=" 500 " android: fromYDelta=?100% p” android: toYDelta=" 0 "/比; & lt;/set>
pop_exit_anim.xml
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt;设置xmlns: android=" http://schemas.android.com/apk/res/android " android: shareInterpolator=" false "比; & lt; !——平移动画——比; & lt;翻译 android:持续时间=" 1000 " android: fromYDelta=" 0 " android: toYDelta=" p " -100%/比; & lt;/set>
最后是使用
//让背景变暗 WindowManager。.getAttributes LayoutParams lp=getWindow () (); lp。α=0.7度; getWindow () .setAttributes (lp);//弹出窗体 PopWin popWin_=new PopWin (null, 0); popWin_.showAsDropDown (findViewById (R.id.relativeLayout));//监听popwin是否关闭,关闭的话让背景恢复 popWin_。setOnDismissListener(新PopupWindow.OnDismissListener () { @Override 公共空间onDismiss () { WindowManager。.getAttributes LayoutParams lp=getWindow () (); lp。α=1 f; getWindow () .setAttributes (lp); } });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。