android开发分享Android自定义PopWindow带动画向下弹出效果

本文实例为大家分享了popwindow实现带动画向下弹出效果的具体代码,供大家参考,具体内容如下 首先建一个popwin的实体类 package dmpte.m

android开发分享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

  <?xml version="1.0" encoding="utf-8"?>  <linearlayout xmlns:android="https://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="https://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="https://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自定义PopWindow带动画向下弹出效果的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/901632.html

(0)
上一篇 2021年10月24日
下一篇 2021年10月24日

精彩推荐