仿QQ实现从底部滑出选择框
2016-03-05 17:44
483 查看
效果图如下:
实现方法:封装了一个CommonPopupWinodow继承PopupWindow,里面实现了当CommonPopupWinodow出现时,背景变暗;消失时,背景变亮;以及出现消失时的动画。用法类似于AlertDialog.
源码:
“`
public class CommonPopupWindow extends PopupWindow {
}
实现方法:封装了一个CommonPopupWinodow继承PopupWindow,里面实现了当CommonPopupWinodow出现时,背景变暗;消失时,背景变亮;以及出现消失时的动画。用法类似于AlertDialog.
源码:
“`
public class CommonPopupWindow extends PopupWindow {
private Context mContext; public CommonPopupWindow(Context mContext, View contentView, int width, int height, boolean focusable) { super(contentView, width, height, focusable); this.mContext = mContext; } public static class Builder { //从上往下数,第一项 public OnClickListener firstButtonClickListener; //从上往下数,第二项 public OnClickListener secondButtonClickListener; //从上往下数,第三项;一般为取消 public OnClickListener thirdButtonClickListener; //从上往下数,第一项文字 public CharSequence firstButtonText; //第二项文字 public CharSequence secondButtonText; //第三项文字 public CharSequence thirdButtonText; public Context context; public CommonPopupWindow popupWindow; public Builder(Context context) { this.context = context; } /* * 从上往下 第一项 * * @param textId * @param listener * @return */ public Builder setFirstButton(int textId, final OnClickListener listener) { firstButtonText = context.getText(textId); firstButtonClickListener = listener; return this; } /* * 从上往下 第二项 * * @param textId * @param listener * @return */ public Builder setSecondButton(int textId, final OnClickListener listener) { secondButtonText = context.getText(textId); secondButtonClickListener = listener; return this; } /* * 从上往下 第三项 * * @param textId * @param listener * @return */ public Builder setThirdButton(int textId, final OnClickListener listener) { thirdButtonText = context.getText(textId); thirdButtonClickListener = listener; return this; } //创建CommonPopupWindow public CommonPopupWindow create() { View popView = LayoutInflater.from(context).inflate(R.layout.puw_common_content, null); TextView mTvFirst = (TextView) popView.findViewById(R.id.tv_popup_first); TextView mTvSecond = (TextView) popView.findViewById(R.id.tv_popup_second); TextView mTvThird = (TextView) popView.findViewById(R.id.tv_popup_third); mTvFirst.setText(firstButtonText); mTvSecond.setText(secondButtonText); mTvThird.setText(thirdButtonText); mTvFirst.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { firstButtonClickListener.onClick(); popupWindow.dismiss(); } }); mTvSecond.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (null != secondButtonClickListener) { secondButtonClickListener.onClick(); } popupWindow.dismiss(); } }); mTvThird.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (null != thirdButtonClickListener) { thirdButtonClickListener.onClick(); } popupWindow.dismiss(); } }); popupWindow = new CommonPopupWindow(context, popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow.setAnimationStyle(R.style.sty_common_popwin_anim); //当popupwindow退出时,背景逐渐变亮 popupWindow.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss() { new Handler().post(new Runnable() { float tempAlpha = 0.5f; @Override public void run() { while (tempAlpha < 1.0f) { tempAlpha += 0.1f; setBackgroundAlpha(tempAlpha); try { Thread.sleep(40); } catch (InterruptedException e) { e.printStackTrace(); } } } }); } }); return popupWindow; } private void setBackgroundAlpha(float bgAlpha) { WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes(); lp.alpha = bgAlpha; //0.0-1.0 ((Activity) context).getWindow().setAttributes(lp); } } public interface OnClickListener { void onClick(); } //当popupwindow开始出现时,逐渐降低背景的透明度 public CommonPopupWindow showAtBottom(View parent, int gravity, int x, int y) { this.showAtLocation(parent, gravity, x, 0); new Handler().post(new Runnable() { float tempAlpha = 1.0f; @Override public void run() { while (tempAlpha > 0.5f) { try { tempAlpha -= 0.1f; WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes(); lp.alpha = tempAlpha; //0.0-1.0 ((Activity) mContext).getWindow().setAttributes(lp); Thread.sleep(40); } catch (InterruptedException e) { e.printStackTrace(); } } } }); return this; }
}
相关文章推荐
- alarm and pause Functions
- DomainModel接收参数
- LeetCode : Regular Expression Matching [java]
- python 小练习之山寨版markdown格式txt文件转html文件
- Android多点触控基础
- 连连看完善
- leetcode:122. Best Time to Buy and Sell Stock II(java)解答
- CodeForces 158 B. Taxi(模拟)
- GUI相关学习资料
- 第24章 SEH结构化异常处理_异常处理及软件异常
- linux dmesg命令参数及用法详解(linux显示开机信息命令)
- 【EA教程】——ER图生成SQL脚本
- 神奇的位运算——进制转换问题(16转8)
- LeetCode--9. Palindrome Number
- MVVM wpf listitem中绑定list外的命令的方法。
- Volley网络请求的简单封装使用
- Python爬虫爬取一篇韩寒新浪博客
- 平台:VS2010+object2015(sdk)+zwCAD2015(开发版) 配置
- Android多点触控手势基础
- Node.Js