Android安卓用Dialog对话框简单并且完美实现popupWindow底部弹出效果,有动画效果
来源:互联网 发布:单片机软件工程师苏州 编辑:程序博客网 时间:2024/05/16 11:28
1背景
项目中经常用到popupwindow从底部弹出的样式,而且存在有动画,最初实现这种效果用是的popupwindow实现起来十分复杂(指的是动画要实现,效果要好),代码基类书写的代码量大。后来发现对话框可以实现从底部弹出效果,实验后发现,代码量奇迹般变少了,特分享一下思路和代码
2看效果
3 要点
主要用到了自定义对话框的进出动画,对话框样式,设置对话框的屏幕宽度和位置等相关的方法
4实现过程
A 在attr.xml文件里面定义对话框的样式
<!-- 对话框风格 --> <style name="dialogThemeBase" parent="@android:style/Theme.Dialog"> <!-- 窗口边框 --> <item name="android:windowFrame">@null</item> <!-- 窗口是否浮动在Activity上 --> <item name="android:windowIsFloating">true</item> <!-- 半透明 --> <item name="android:windowIsTranslucent">false</item> <!-- 没有标题栏 --> <item name="android:windowNoTitle">true</item> <!-- 背景 --> <item name="android:background">@null</item> <!-- 背景模糊 --> <item name="android:backgroundDimEnabled">true</item> <item name="android:windowBackground">@android:color/transparent</item> </style>
B 在style.xml文件里面定义对话框菜单的进出样式
<!-- 底部弹出popupwindow的动画样式 --> <style name="popupAnimBottomIn" parent="android:Animation"> <item name="android:windowEnterAnimation">@anim/slide_in_from_bottom</item> <item name="android:windowExitAnimation">@anim/slide_out_from_bottom</item> </style>
C 在anin目录下创建两个动画文件
slide_in_from_bottom.xml
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="0.0%" android:fromYDelta="100.0%" android:toXDelta="0.0%" android:toYDelta="0.0%" />
slide_out_from_bottom.xml
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="0.0%" android:fromYDelta="0.0%" android:toXDelta="0.0%" android:toYDelta="100.0%" />
D 创建普通对话框基类
BaseDialog.java
package com.example.mytest20170113;import android.app.Dialog;import android.content.Context;import android.view.View;/** * Created by liugd on 2016/4/29. */public abstract class BaseDialog extends Dialog implements View.OnClickListener { protected Context context; public BaseDialog(Context context) { this(context, R.style.dialogThemeBase); } public BaseDialog(Context context, int theme) { super(context, theme); this.context = context; if (setWindowAnimation() != 0) { getWindow().setWindowAnimations(setWindowAnimation()); } setContentView(setView()); initListener(); finView(); } /*** * 设置VIEW * * @return */ protected abstract int setView(); protected void finView() { } //动画样式 protected int setWindowAnimation() { return 0; } protected int[] setClickIDs() { return null; } /*** * 泛型查找ID,无需强制转换 * @param id * @return */ @SuppressWarnings("unchecked") protected <T extends View> T findViewByID(int id) { return (T) super.findViewById(id); } /** * 初始化点击事件 */ void initListener() { int ids[] = setClickIDs();// 设置点击ID if (ids != null && ids.length > 0) { for (int id : ids) { findViewById(id).setOnClickListener(this); } } }}
E 底部弹出对话框基类
BaseBottomDialog.java
package com.example.mytest20170113;import android.content.Context;import android.util.DisplayMetrics;import android.view.Gravity;import android.view.WindowManager;/** * 从底部弹出的对话框,实现popwindow弹出的效�? Created by liugd on 2017/1/12. */public abstract class BaseBottomDialog extends BaseDialog {public BaseBottomDialog(Context context) {super(context);configScreenSize(context);WindowManager.LayoutParams lp = getWindow().getAttributes();lp.width = mScreenWidth;getWindow().setAttributes(lp);getWindow().setGravity(Gravity.BOTTOM);// 底部展示}// 动画样式protected int setWindowAnimation() {return R.style.popupAnimBottomIn;}public static void configScreenSize(Context context) {if (mScreenWidth == 0) {DisplayMetrics display = new DisplayMetrics();WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);manager.getDefaultDisplay().getMetrics(display);mScreenHeight = Math.max(display.heightPixels, display.widthPixels);mScreenWidth = Math.min(display.heightPixels, display.widthPixels);}}public static int mScreenHeight;public static int mScreenWidth;}
F 底部对话框的实现类
MyTestBottomDialog.java
package com.example.mytest20170113;import android.content.Context;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class MyTestBottomDialog extends BaseBottomDialog {public MyTestBottomDialog(Context context) {super(context);}@Overridepublic void onClick(View arg0) {Toast.makeText(context, ((TextView) arg0).getText().toString(), 0).show();switch (arg0.getId()) {case R.id.button1:break;case R.id.button2:break;case R.id.button3:break;}dismiss();}@Overrideprotected int[] setClickIDs() {return new int[] { R.id.button1, R.id.button2, R.id.button3 };}@Overrideprotected int setView() {return R.layout.dialog_bottom_test;}}
G 测试对话框的布局文件
dialog_bottom_test.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选项1" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选项2" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消" /></LinearLayout>
H 在Activity里面测试
findViewById(R.id.textView1).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {MyTestBottomDialog dialog = new MyTestBottomDialog(MainActivity.this);dialog.show();}});
2017-1-13 12:16
附CSDN下载地址
http://download.csdn.NET/detail/u012990509/9736702
1 0
- Android安卓用Dialog对话框简单并且完美实现popupWindow底部弹出效果,有动画效果
- 自定义Dialog对话框并实现从底部弹出的动画效果
- Android 实现底部弹出对话框效果
- PopupWindow底部弹出效果
- Android实现底部弹出PopupWindow背景逐渐变暗效果
- android dialog实现底部弹出和手指滑动隐藏效果
- 底部弹出PopupWindow并且背景变为半透明效果
- 底部弹出PopupWindow并且背景变为半透明效果
- 底部弹出PopupWindow并且背景变为半透明效果
- popupWindow 弹出动画效果
- Android之PopupWindow-底部弹出,以及中间弹出有变暗效果
- Android之PopupWindow-底部弹出,以及中间弹出有变暗效果
- Android之PopupWindow-底部弹出,以及中间弹出有变暗效果
- Android PopupWindow弹出窗口的完美实现(实现弹出背景变暗效果)
- Android Dialog(对话框)增加动画效果
- [Android实例] popupwindow实现弹出菜单效果
- Android高效率实现弹出带动画效果的对话框,仿照微信对话框效果
- android PopupWindow 动画效果
- 统计相关系数(2)——Spearman Rank(斯皮尔曼等级)相关系数及MATLAB实现
- java 编程中如何避免bug 的出现
- 安卓成长记(八)
- 80老翁谈人生(344):著作权法是否适用于网络空间?
- 微信小程序之环境搭建(二)
- Android安卓用Dialog对话框简单并且完美实现popupWindow底部弹出效果,有动画效果
- 前端之Sass/Scss实战笔记
- TV开发中ListView的那些坑
- service
- [乐意黎转载]云南公务员招录计划中将增加残疾人指标!
- 【c++】hello word
- 深入理解HashMap(精华必看)
- JavaWeb学习总结二十一、JavaWeb的两种开发模式
- [笔记]《Linux内核设计与实现》第十一章定时器和时间管理