PopupWindow半透明背影
android應用需求要實現底部彈窗,而且需要半透明背影,使用PopupWindow實現的,PopupWindow背影網上有好多種實現,需要的自行搜尋。下面給出我的解決方法,直接操作PopupWindow內的自定義view,做自定義view的動畫,直接上程式碼:
public class TranslucentPopupWindow {
private static final int ANIM_TIME = 300;
private FrameLayout mRootView;
private View mContentView;
private PopupWindow mPopupWindow;
public TranslucentPopupWindow(Context context) {
mRootView = new FrameLayout(context);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
mRootView.setLayoutParams(layoutParams);
mRootView.setBackgroundColor(context.getResources().getColor(R.color.color_translucent));
mRootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
public void setCustomContentView(View view) {
mContentView = view;
addCustomContentView();
}
private void addCustomContentView() {
if (null != mContentView) {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM;
layoutParams.leftMargin = mContentView.getResources().getDimensionPixelOffset(R.dimen.dialog_padding);//彈框距離螢幕左間距
layoutParams.rightMargin = mContentView.getResources().getDimensionPixelOffset(R.dimen.dialog_padding);
mRootView.addView(mContentView, layoutParams);
mRootView.setVisibility(View.INVISIBLE);
}
}
private void createWindow() {
if (null == mPopupWindow) {
mPopupWindow = new PopupWindow(mRootView);
mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
mPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
mEnterAnimatorSet = null;
mExitAnimatorSet = null;
}
});
}
}
public void dismiss() {
if (null != mPopupWindow && mPopupWindow.isShowing()) {
startExitAnim();
}
}
public boolean isShowing() {
return null != mPopupWindow && mPopupWindow.isShowing();
}
public void showAtLocation(View parentView, int gravity, int x, int y) {
if (null == mPopupWindow) {
createWindow();
}
if (!mPopupWindow.isShowing()) {
mRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mRootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
mRootView.setVisibility(View.VISIBLE);
startEnterAnim();
}
});
mPopupWindow.showAtLocation(parentView, gravity, x, y);
}
}
private void startEnterAnim() {
if (null != mContentView && (null == mEnterAnimatorSet || !mEnterAnimatorSet.isRunning())) {
initEnterAnimatorSet();
mEnterAnimatorSet.start();
}
}
private AnimatorSet mEnterAnimatorSet;
private void initEnterAnimatorSet() {
if (null == mEnterAnimatorSet) {
mContentView.setTranslationY(mContentView.getMeasuredHeight());
mContentView.setAlpha(0);
mEnterAnimatorSet = new AnimatorSet();
ObjectAnimator animatorY = ObjectAnimator.ofFloat(mContentView, "translationY",
0);
ObjectAnimator animatorAlpha = ObjectAnimator.ofFloat(mContentView, "alpha", 1);
mRootView.setAlpha(0f);
ObjectAnimator rootAnimatorAlpha = ObjectAnimator.ofFloat(mRootView, "alpha", 1);
mEnterAnimatorSet.playTogether(animatorAlpha, animatorY, rootAnimatorAlpha);
mEnterAnimatorSet.setDuration(ANIM_TIME);
}
}
private void startExitAnim() {
if (null != mContentView && (null == mExitAnimatorSet || !mExitAnimatorSet.isRunning())) {
initExitAnimatorSet();
mExitAnimatorSet.start();
}
}
private AnimatorSet mExitAnimatorSet;
private void initExitAnimatorSet() {
if (null == mExitAnimatorSet) {
mContentView.setTranslationY(0);
mContentView.setAlpha(1);
mExitAnimatorSet = new AnimatorSet();
ObjectAnimator animatorY = ObjectAnimator.ofFloat(mContentView, "translationY",
mContentView.getMeasuredHeight());
ObjectAnimator animatorAlpha = ObjectAnimator.ofFloat(mContentView, "alpha", 0);
mRootView.setAlpha(1);
ObjectAnimator rootAnimatorAlpha = ObjectAnimator.ofFloat(mRootView, "alpha", 0);
mExitAnimatorSet.playTogether(animatorAlpha, animatorY, rootAnimatorAlpha);
mExitAnimatorSet.setDuration(ANIM_TIME);
mExitAnimatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
if (null != mPopupWindow) {
mPopupWindow.dismiss();
}
}
@Override
public void onAnimationCancel(Animator animation) {
if (null != mPopupWindow) {
mPopupWindow.dismiss();
}
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}
}