一个底部弹出Popwindow的效果
效果图:
显示的时候会从屏幕底部滑出来,消失的时候就是滑出去,实现方式用的PopWindow,下面贴代码:
- public class BottomPopupOption {
- //上下文对象
- private Context mContext;
- //Title文字
- private String mTitle;
- //PopupWindow对象
- private PopupWindow mPopupWindow;
- //选项的文字
- private String[] options;
- //选项的颜色
- private int[] Colors;
- //点击事件
- private onPopupWindowItemClickListener itemClickListener;
- /**
- * 一个参数的构造方法,用于弹出无标题的PopupWindow
- *
- * @param context
- */
- public BottomPopupOption(Context context) {
- this.mContext = context;
- }
- /**
- * 2个参数的构造方法,用于弹出有标题的PopupWindow
- *
- * @param context
- * @param title 标题
- */
- public BottomPopupOption(Context context, String title) {
- this.mContext = context;
- this.mTitle = title;
- }
- /**
- * 设置选项的点击事件
- *
- * @param itemClickListener
- */
- public void setItemClickListener(onPopupWindowItemClickListener itemClickListener) {
- this.itemClickListener = itemClickListener;
- }
- /**
- * 设置选项文字
- */
- public void setItemText(String... items) {
- options = items;
- }
- /**
- * 设置选项文字颜色,必须要和选项的文字对应
- */
- public void setColors(int... color) {
- Colors = color;
- }
- /**
- * 添加子View
- */
- private void addView(View v) {
- LinearLayout lin_layout = (LinearLayout) v.findViewById(R.id.layout_popup_add);
- //Title
- TextView tv_pop_title = (TextView) v.findViewById(R.id.tv_popup_title);
- //取消按钮
- Button btn_cancel = (Button) v.findViewById(R.id.btn_cancel);
- btn_cancel.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- dismiss();
- }
- });
- if (mTitle != null) {
- tv_pop_title.setText(mTitle);
- } else {
- tv_pop_title.setVisibility(View.GONE);
- }
- if (options != null && options.length > 0) {
- for (int i = 0; i < options.length; i++) {
- View item = LayoutInflater.from(mContext).inflate(R.layout.basetools_popup_item, null);
- Button btn_txt = (Button) item.findViewById(R.id.btn_popup_option);
- btn_txt.setText(options[i]);
- if (Colors != null && Colors.length == options.length) {
- btn_txt.setTextColor(Colors[i]);
- }
- final int finalI = i;
- btn_txt.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (itemClickListener != null) {
- itemClickListener.onItemClick(finalI);
- }
- }
- });
- lin_layout.addView(item);
- }
- }
- }
- /**
- * 弹出Popupwindow
- */
- public void showPopupWindow() {
- View popupWindow_view = LayoutInflater.from(mContext).inflate(R.layout.basetools_popup_bottom, null);
- //添加子View
- addView(popupWindow_view);
- mPopupWindow = new PopupWindow(popupWindow_view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
- mPopupWindow.setAnimationStyle(R.style.timepopwindow_anim_style);
- mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
- mPopupWindow.setFocusable(true);
- mPopupWindow.setOutsideTouchable(true);
- mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
- @Override
- public void onDismiss() {
- setWindowAlpa(false);
- }
- });
- show(popupWindow_view);
- }
- /**
- * 显示PopupWindow
- */
- private void show(View v) {
- if (mPopupWindow != null && !mPopupWindow.isShowing()) {
- mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
- }
- setWindowAlpa(true);
- }
- /**
- * 消失PopupWindow
- */
- public void dismiss() {
- if (mPopupWindow != null && mPopupWindow.isShowing()) {
- mPopupWindow.dismiss();
- }
- }
- /**
- * 动态设置Activity背景透明度
- *
- * @param isopen
- */
- public void setWindowAlpa(boolean isopen) {
- if (android.os.Build.VERSION.SDK_INT < 11) {
- return;
- }
- final Window window = ((Activity) mContext).getWindow();
- final WindowManager.LayoutParams lp = window.getAttributes();
- window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
- ValueAnimator animator;
- if (isopen) {
- animator = ValueAnimator.ofFloat(1.0f, 0.5f);
- } else {
- animator = ValueAnimator.ofFloat(0.5f, 1.0f);
- }
- animator.setDuration(400);
- animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
- @TargetApi(Build.VERSION_CODES.HONEYCOMB)
- @Override
- public void onAnimationUpdate(ValueAnimator animation) {
- float alpha = (float) animation.getAnimatedValue();
- lp.alpha = alpha;
- window.setAttributes(lp);
- }
- });
- animator.start();
- }
- /**
- * 点击事件选择回调
- */
- public interface onPopupWindowItemClickListener {
- void onItemClick(int position);
- }
- }
代码量不多,而且很容易看懂~ 布局:
basetools_popup_bottom:
- <?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="match_parent"
- android:gravity="bottom"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:padding="10dp">
- <LinearLayout
- android:id="@+id/layout_popup_add"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:background="@drawable/pop_white_btn_select"
- android:divider="@color/list_divider_line"
- android:orientation="vertical"
- android:showDividers="middle">
- <TextView
- android:id="@+id/tv_popup_title"
- android:layout_width="match_parent"
- android:layout_height="42dp"
- android:gravity="center"
- android:text="请选择照片的获取方式"
- android:textColor="#666666"
- android:textSize="15sp" />
- </LinearLayout>
- <Button
- android:id="@+id/btn_cancel"
- android:layout_width="match_parent"
- android:layout_height="42dp"
- android:layout_marginTop="15dp"
- android:background="@drawable/pop_white_btn_select"
- android:text="取消"
- android:textColor="@color/gray009"
- android:textSize="18sp" />
- </LinearLayout>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <solid android:color="@color/white"></solid>
- <corners android:radius="5dp"></corners>
- </shape>
Color:
- <color name="list_divider_line">#e7e7e7</color>
布局:basetools_popup_item
- <?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="42dp"
- android:orientation="vertical">
- <Button
- android:id="@+id/btn_popup_option"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@null"
- android:text="选项一"
- android:textColor="@color/blue001"
- android:textSize="18sp" />
- </LinearLayout>
PopWoindw 显示和消失动画:
- <style name="popwindow_anim_style">
- <item name="android:windowEnterAnimation">@anim/anim_enter_bottom</item>
- <!-- 指定显示的动画xml -->
- <item name="android:windowExitAnimation">@anim/anim_exit_bottom</item>
- <!-- 指定消失的动画xml -->
- </style>
anim_enter_bottom:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate android:fromYDelta="100%" android:toYDelta="0" android:duration="300" />
- </set>
anim_exit_bottom:
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate android:fromYDelta="0" android:toYDelta="100%" android:duration="300" />
- </set>
使用方法:
- BottomPopupOption bottomPopupOption = new BottomPopupOption(TradeManageActivity.this);
- bottomPopupOption.setItemText("拍照", "选择相册");
- // bottomPopupOption.setColors();//设置颜色
- bottomPopupOption.showPopupWindow();
注意:setColor需要和setItemText 值相对于
如果popWindow需要Title就使用带有二个参数的构造方法
有点击事件,采用接口回调的方式
- bottomPopupOption.setItemClickListener(new BottomPopupOption.onPopupWindowItemClickListener() {
- @Override
- public void onItemClick(int position) {
- }
- });
相关推荐
- 一个底部弹出Popwindow的效果
- 10. 创建一个带有灯光且通过鼠标左键可以触发效果的密室案例(带火焰音效)
- 一个具有动态切换,自然过渡效果的Segment,常见于今日头条,网易新闻类,Write By Swift3.0
- 类似app常见效果,弹出一个提示语句(黑色背景+白色文字),2s后消失(来自改编alert,在h5里比较实用的),下面附上效果图
- 仿照微信的效果,实现了一个支持多选、选原图和视频的图片选择器,适配了iOS6-10系统,3行代码即可集成....
- WebGL实现的一个Depth Blur(深度模糊/或景深模糊)的动态效果
- html中写一个简单的JavaScript动画效果+css 让一个小方框小方块从左上角到右下角 或者 右上角到左下角不断运动的效果 定时器 setInterval 函数function
- 如何制作一个效果比较好的全景图
- Android开发实战之底部Dialog弹出效果
- vue写一个弹出层以及遇上的知识点
- 转载: 10年老程序员谈Dotnet程序员职业规划(图文)
- 增强EditText--TextInputLayout