您的位置:首页 > 其它

仿QQ实现从底部滑出选择框

2016-03-05 17:44 483 查看
效果图如下:



实现方法:封装了一个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;
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: