最近要改客户端,需要实现一个从底部弹出的popuwindow,像我这种渣渣android技术,能整出popuwindow但是整不出动画,百度之,记录一下。
从下面这个地址转的 http://blog.csdn.net/yxhuang2008/article/details/42617805
最近因为要用到PopupWindow,所以,就在网上搜索了一下,发现挺多关于这样的文章,现在我把它们整理了一下。
1、Android PopupWindow 的使用技巧,http://www.cnblogs.com/sw926/p/3230659.html
2、Android PopupWindow 的使用和分析,http://www.cnblogs.com/mengdd/p/3569127.html
3、Android PopupWindow 实现从底部弹出或滑出选择菜单或窗口,http://104zz.iteye.com/blog/1685389
本文是整理上面三篇文章而成,绝大部分代码是使用第三篇的,如有不妥,请联系。
一、第一步就是创建布局文件
res/layout/finish_project_popupwindow.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/popupwindow_Button_saveProject"
- android:layout_marginLeft="20dip"
- android:layout_marginRight="20dip"
- android:layout_marginTop="10dip"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/popupwindow_Button_saveProject" />
- <Button
- android:id="@+id/popupwindow_Button_abandonProject"
- android:layout_marginLeft="20dip"
- android:layout_marginRight="20dip"
- android:layout_marginTop="10dip"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/popupwindow_Button_abandonProject" />
- <Button
- android:id="@+id/popupwindow_cancelButton"
- android:layout_marginLeft="20dip"
- android:layout_marginRight="20dip"
- android:layout_marginTop="10dip"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/popupwindow_cancelButton" />
- </LinearLayout>
二、第二步,设置PopupWindow弹出和淡出的动画效果
1、在res目录下建立一个文件夹anim,专门用于存储PopupWindow弹出和淡出的动画效果
(1)PopupWindow 弹出时的动画效果
res/anim/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:fromYDelta="100%p"
- android:toYDelta="0"
- android:duration="500" />
- <alpha
- android:fromAlpha="0"
- android:toAlpha="1.0"
- android:duration="300"/>
- </set>
(2)PopupWindow 淡出时的动画效果
res/anim/out_anim.xml
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:shareInterpolator="false">
- <translate
- android:fromYDelta="0"
- android:toYDelta="100%p"
- android:duration="3000"/>
- <alpha
- android:fromAlpha="1.0"
- android:toAlpha="0"
- android:duration="2000"/>
- </set>
在<translate>标签中,fromYDelta, toYDelta 表示在y轴方法的移动,因为我是要实现从底部弹出PopupWindow的,所以,是y轴,如果想要实现从屏幕侧边的话,可以选择x轴fromXDelta, toXDelta。
duration 表示弹出或者淡出的动画时间。在<alpha>标签中表示PopupWindow透明度的变化,这样是为了比较好看。duration 同样表示时间。
(3)增加这样的Style
在res/valus/styles.xml 的<resources>标签内添加
- <!-- popupwindow 动画 -->
- <style name="Animation">
- <item name="android:windowEnterAnimation">@anim/enter_anim</item>
- <item name="android:windowExitAnimation">@anim/out_anim</item>
- </style>
其中,
- <item name="android:windowEnterAnimation">@anim/enter_anim</item>
表示窗口弹出时的动画文件,@anim/enter_anim 中enter_anim一定要和我们前面定义弹出动画的文件名一致,淡出的动画文件类似。
三、创建自定义类,继承PopupWindow
src/FinishProjectPopupWindow.java
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.drawable.ColorDrawable;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.PopupWindow;
- public class FinishProjectPopupWindows extends PopupWindow{
- private static final String TAG = "FinishProjectPopupWindows";
- private View mView;
- public Button btnSaveProject, btnAbandonProject, btnCancelProject;
- public FinishProjectPopupWindows(Activity context,
- OnClickListener itemsOnClick) {
- super(context);
- Log.i(TAG, "FinishProjectPopupWindow 方法已被调用");
- LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- mView = inflater.inflate(R.layout.finish_project_popuwindow, null);
- btnSaveProject = (Button) mView.findViewById(R.id.popupwindow_Button_saveProject);
- btnAbandonProject = (Button) mView.findViewById(R.id.popupwindow_Button_abandonProject);
- btnCancelProject = (Button) mView.findViewById(R.id.popupwindow_cancelButton);
- // 设置按钮监听
- btnCancelProject.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- Log.i(TAG, "取消项目");
- dismiss();
- }
- });
- btnSaveProject.setOnClickListener(itemsOnClick);
- btnAbandonProject.setOnClickListener(itemsOnClick);
- //设置PopupWindow的View
- this.setContentView(mView);
- //设置PopupWindow弹出窗体的宽
- this.setWidth(LayoutParams.MATCH_PARENT);
- //设置PopupWindow弹出窗体的高
- this.setHeight(LayoutParams.WRAP_CONTENT);
- //设置PopupWindow弹出窗体可点击
- this.setFocusable(true);
- //设置SelectPicPopupWindow弹出窗体动画效果
- this.setAnimationStyle(R.style.Animation);
- //实例化一个ColorDrawable颜色为半透明
- ColorDrawable dw = new ColorDrawable(0xb0000000);
- //设置SelectPicPopupWindow弹出窗体的背景
- this.setBackgroundDrawable(dw);
- }
- }
四、在Activity中设置弹出PopupWindow
- private FinishProjectPopupWindows mFinishProjectPopupWindow
- mFinishProjectPopupWindow = new FinishProjectPopupWindows(MapFragmentActivity.this, itemsOnClick);
- // 显示PopupWindow
- mFinishProjectPopupWindow.showAtLocation(MapFragmentActivity.this.findViewById(R.id.main),
- Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
五、实现PopupWindow中按钮的监听
- private OnClickListener itemsOnClick = new OnClickListener(){
- @Override
- public void onClick(View v) {
- mFinishProjectPopupWindow.dismiss();
- switch(v.getId()){
- case R.id.popupwindow_Button_saveProject:
- Log.i(TAG, "保存线路");
- break;
- case R.id.popupwindow_Button_abandonProject:
- break;
- case R.id.popupwindow_cancelButton:
- mFinishProjectPopupWindow.dismiss();
- Log.i(TAG, "取消");
- break;
- }
- }
- };
运行的效果