Android自定义PopWindow带动画向下弹出效果

发布时间:2019-08-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android自定义PopWindow带动画向下弹出效果脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例为大家分享了PopWindow实现带动画向下弹出效果的具体代码,供大家参考,具体内容如下

首先建一个popwin的实体类

 package dmpte.mytest;  import andROId.content.Context; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.widget.PopupWindow; import android.widget.RelativeLayout;  public class PopWin extends PopupWindow {  PRivate Context mContext;  private View view;    public PopWin(final Context mContext, View.OnClickListener ITemsOnClick, int flag) {   this.mContext = mContext;   this.view = LayoutInflater.From(mContext).inflate(R.layout.view_popwin, null);   // 设置外部可点击   this.setOutsideTouchable(true);   /* 设置弹出窗口特征 */   // 设置视图   this.setContentView(this.view);   // 设置弹出窗体的和高   this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);//高   this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);//宽    // 设置弹出窗体可点击   this.setFocusable(true);    // 设置弹出窗体显示时的动画,从底部向上弹出   this.setAnimationStyle(R.style.take_photo_anim); //  mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框   this.view.setOnTouchListener(new View.OnTouchListener() {    @override    public boolean onTouch(View v, MotionEvent event) {     int height = view.findViewById(R.id.pop_layout).getHeight();     int y = (int) event.getY();     if (event.getAction() == MotionEvent.ACTION_DOWN) {     //Y表示手指点击的位置,屏幕顶端为0,往下一次递增。height是popwin的高度。y > height就表示手指点在Popwin的外面,然后关闭popwin      if (y > height) {       dismiss();      }     }     return true;    }    });   }  }

然后是这个类的布局 view_popwin.XMl

 <&#63;xML version="1.0" encoding="utf-8"?> <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:background="@null"  android:orientation="vertical">   <LinearLayout   android:layout_width="match_parent"   android:layout_height="170dp"   android:background="#ffff"   android:orientation="vertical">    <TextView    android:id="@+id/tv_jingtai"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_gravity="center"    android:layout_marginTop="2dp"    android:gravity="center"    android:text="移动静态"    android:textColor="#F123" />   </LinearLayout> </LinearLayout>

接下来是这个类里涉及的动画 popwin_anim,在res/values/styles下

 <style name="popwin_anim" parent="android:Animation">     <item name="android:windowEnterAnimation">@anim/pop_enter_anim</item>     <item name="android:windowExitAnimation">@anim/pop_exit_anim</item> </style>

然后是进场动画 pop_enter_anim和出场动画 pop_exit_anim,在res下建一个文件夹anim,分别新建上面两个xml

pop_enter_anim.xml

 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"  android:shareInterpolator="false">  <!-- 平移动画 -->  <translate   android:duration="500"   android:fromYDelta="-100%p"   android:toYDelta="0" /> </set>

pop_exit_anim.xml

 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"  android:shareInterpolator="false">  <!-- 平移动画 -->  <translate   android:duration="1000"   android:fromYDelta="0"   android:toYDelta="-100%p" />  </set>

最后是使用

 //让背景变暗  WindowManager.LayoutParams lp = getWindow().getAttributes();     lp.alpha = 0.7f;     getWindow().setattributes(lp);     //弹出窗体     PopWin popWin_ = new PopWin(this, null, 0);     popWin_.showAsDropDown(findViewById(R.id.relativeLayout));     //监听popwin是否关闭,关闭的话让背景恢复     popWin_.setOnDismissListener(new PopupWindow.OnDismissListener() {      @Override      public void onDismiss() {       WindowManager.LayoutParams lp = getWindow().getAttributes();       lp.alpha = 1f;       getWindow().setAttributes(lp);   } });
android教程
脚本网站
android studio

脚本宝典总结

以上是脚本宝典为你收集整理的Android自定义PopWindow带动画向下弹出效果全部内容,希望文章能够帮你解决Android自定义PopWindow带动画向下弹出效果所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。