当前位置: 移动技术网 > 移动技术>移动开发>Android > Android自定义PopWindow带动画向下弹出效果

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

2019年07月31日  | 移动技术网移动技术  | 我要评论

本文实例为大家分享了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="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);
  }
});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网