Android-自定义popupwindow详解(包含动画简单讲解)
来源:互联网 发布:淘宝交易关闭欺诈风险 编辑:程序博客网 时间:2024/05/21 07:14
我们知道在很多需求的时候都要求有对话框的,我们也知道dialog和popupwindow都可以实现。但是,popupwindow是弹框的万能之法,当然dialog的集成度,所继承的相关方法也给予一定便利。关于dialog的自定义上篇文章有详细介绍,本篇文章将详解popupwindow的自定义(糅合动画)。
先上图,就是做成这个带有弹出的动画的popupwindow
下面将进行代码的详解
大体思路:就是继承popupwindow,获得构造器,接入自定义布局,设置弹框属性
1.popupwindow的java文件:
import android.app.Activity;import android.content.Context;import android.content.Intent;import android.graphics.drawable.ColorDrawable;import android.view.LayoutInflater;import android.view.View;import android.widget.LinearLayout;import android.widget.PopupWindow;import com.android.administrator.childrensittingposture.R;import com.android.administrator.childrensittingposture.activity.HistoryActivity;import com.android.administrator.childrensittingposture.activity.SettingActivity;/** * Created by 符柱成 on 2016/7/24. */public class MyPopWindow extends PopupWindow { private View conentView; private Activity context; public MyPopWindow(final Activity context) { super(context); this.context = context; this.initPopupWindow(); } private void initPopupWindow() { //使用view来引入布局 LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); conentView = inflater.inflate(R.layout.popuo_dialog, null); //获取popupwindow的高度与宽度 int h = context.getWindowManager().getDefaultDisplay().getHeight(); int w = context.getWindowManager().getDefaultDisplay().getWidth(); // 设置SelectPicPopupWindow的View this.setContentView(conentView); // 设置SelectPicPopupWindow弹出窗体的宽 this.setWidth(w / 2 + 50); // 设置SelectPicPopupWindow弹出窗体的高 this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT); // 设置SelectPicPopupWindow弹出窗体可点击 this.setFocusable(true); this.setOutsideTouchable(true); // 刷新状态 this.update(); // 实例化一个ColorDrawable颜色为半透明 ColorDrawable dw = new ColorDrawable(0000000000); // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作 this.setBackgroundDrawable(dw); // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog); // 设置SelectPicPopupWindow弹出窗体动画效果,设置动画,一会会讲解 this.setAnimationStyle(R.style.AnimationPreview); //布局控件初始化与监听设置 LinearLayout llayout_remind = (LinearLayout) conentView .findViewById(R.id.llayout_remind); LinearLayout llayout_history = (LinearLayout) conentView .findViewById(R.id.llayout_history); llayout_remind.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { } }); llayout_history.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } /** * 显示popupWindow的方式设置,当然可以有别的方式。 *一会会列出其他方法 * @param parent */ public void showPopupWindow(View parent) { if (!this.isShowing()) { // 以下拉方式显示popupwindow this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18); } else { this.dismiss(); } }}
列出其他的popupwindow显示方法:都是封装好了的方法,尽情使用吧
/** * 第一种 * 显示在控件的下右方 * * @param parent parent */ public void showAtDropDownRight(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { // x y int[] location = new int[2]; //获取在整个屏幕内的绝对坐标 parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0] + parent.getWidth() - this.width, location[1] + parent.getHeight()); } } /** 第二种 * 显示在控件的下中方 * * @param parent parent */ public void showAtDropDownCenter(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { // x y int[] location = new int[2]; //获取在整个屏幕内的绝对坐标 parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0] / 2 + parent.getWidth() / 2 - this.width / 6, location[1] + parent.getHeight()); } } /** 第三种 * 显示在控件的下左方 * * @param parent parent */ public void showAtDropDownLeft(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { // x y int[] location = new int[2]; //获取在整个屏幕内的绝对坐标 parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0], location[1] + parent.getHeight()); } }
popupwindow的布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" android:background="@color/blue"> <LinearLayout android:id="@+id/pop_layout2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="@color/white" android:gravity="center_horizontal" android:orientation="vertical" > <LinearLayout android:id="@+id/llayout_remind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp" > <ImageView android:layout_width="35dp" android:layout_height="35dp" android:scaleType="fitCenter" android:src="@drawable/remind" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginLeft="10dp" android:gravity="center" android:text="自动提醒设置" android:textColor="@color/white" android:textSize="15dip" /> </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="0.2dp" android:background="@color/white" /> <LinearLayout android:id="@+id/llayout_history" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp" > <ImageView android:layout_width="35dp" android:layout_height="35dp" android:scaleType="fitCenter" android:src="@drawable/history" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginLeft="10dp" android:gravity="center" android:text="历史记录统计" android:textColor="@color/white" android:textSize="15dip" /> </LinearLayout> </LinearLayout></RelativeLayout>
此外我们不要忘了,我们是给这个popupwindow设置了动画的喔:style文件:这里作为资源来自定义样式
<style name="AnimationPreview"> <item name="android:windowEnterAnimation">@anim/in</item> <item name="android:windowExitAnimation">@anim/out</item> </style>
保存动画的文件在另一个地方
用图说明
下面就给出动画文件的代码啦:
<?xml version="1.0" encoding="utf-8"?><!-- 对话框出来时的动画--><scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="0.001" android:toXScale="1.0" android:fromYScale="0.001" android:toYScale="1.0" android:pivotX="100%" android:pivotY="10%" android:duration="200" /><!--fromXScale与toXScale是指一开始的时候在x方向上的缩放比例和在结束时候的比例,1.0就是原本的样子。同理fromYScale和 toYScale--><!--pivotX和pivotY是指动画开始的位置 起始动画的x和y位置-->
<?xml version="1.0" encoding="utf-8"?><!-- 对话框消失时的动画--><scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="1.0" android:toXScale="0.001" android:fromYScale="1.0" android:toYScale="0.001" android:pivotX="100%" android:pivotY="10%" android:duration="200" />
好了,popupwindow的自定义思路详解完成,并且在期间列出一些扩展性的可能和讲解了这个动画的实现。
本文参考了:老妖的博客http://blog.csdn.net/wwj_748/article/details/25653409
欢迎在下方指出错误,共同学习!
转载请注明:【JackFrost的博客】
1 0
- Android-自定义popupwindow详解(包含动画简单讲解)
- android自定义popupwindow动画
- Android自定义PopupWindow 动画
- Android 自定义PopupWindow动画效果
- 自定义PopupWindow动画效果
- 自定义PopupWindow动画效果
- android PopupWindow 动画效果
- Android之动画PopupWindow
- Android之动画PopupWindow
- Android中PopupWindow的简单操作和动画的操作
- android简单的自定义动画
- Android自定义View之popupwindow进阶封装:高仿ios “item动画弹出”效果的popupwindow。
- Android动画最新最全详解包含Material Design动画
- Android动画最新最全详解包含Material Design动画
- Android之简单自定义PopupWindow弹框实现
- android 自定义PopupWindow
- android自定义一个PopupWindow
- Android-自定义PopupWindow
- AngularJS入门笔记(2)
- Python入门学习笔记(1):Python、PyCharm 的安装和介绍
- Kylin build cube step 2 报错(Kylin 安装在slave 节点上)
- 【杭电oj2028】Lowest Common Multiple Plus
- 英语学习——纤手香凝
- Android-自定义popupwindow详解(包含动画简单讲解)
- Java技术----多态的实现原理
- 字符串空格替换或删除
- 纯html、css3、js的时钟
- Q40:数组中只出现一次的数字
- Laravel队列的心得笔记
- 查看当前mysql使用频繁的sql语句
- Java 8系列之重新认识HashMap
- leetcode:295. Find Median from Data Stream