服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android自定义PopWindow带动画向下弹出效果

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

2022-08-28 17:18狱火苍穹 Android

这篇文章主要为大家详细介绍了Android自定义PopWindow带动画向下弹出效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

首先建一个popwin的实体类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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下

?
1
2
3
4
<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

?
1
2
3
4
5
6
7
8
9
<?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

?
1
2
3
4
5
6
7
8
9
10
<?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>

最后是使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//让背景变暗
 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);
  }
});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_33919497/article/details/82414911

延伸 · 阅读

精彩推荐