亲宝软件园·资讯

展开

Android PopUpWindow弹窗

PizazzForeverㅤ 人气:0

在这里插入图片描述

首先自定义一个ViewGroup,继承自LinerLayout,为了实现上下滑动

//用于实现顶部弹窗动画  以及向上滑动动画
public class TopTipsLinearLayout extends LinearLayout {

    private static final String TAG = "Hyh";

    private int mHeight;
    private boolean mIsFirstLayout=true;
    private boolean mIsPalyingAnimation=false;
    private int mLastY=0;
    private int mLastX=0;
    private final int mTouchSlop = 4;
    private final long SINGLE_CLICK_TIME = 300;
    private long beginTiem=0;
    private boolean mIsMoving=false;

    public TopTipsLinearLayout(Context context) {
        super(context);
    }

    public TopTipsLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TopTipsLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public TopTipsLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if(mIsFirstLayout) {
            mHeight = getHeight();
            mIsFirstLayout = false;
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mIsMoving = false;
                beginTiem = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_MOVE:
                int deltay = (int)event.getRawY() - mLastY;
                if(!mIsPalyingAnimation) {
                    if(deltay < 0 || getTranslationY() + deltay <= 0) {
                        setTranslationY(getTranslationY() + deltay);
                    }
                }
                if(isMove(event.getRawX(), event.getRawY())) {
                    mIsMoving = true;
                }
                break;
            case MotionEvent.ACTION_UP:
                if(System.currentTimeMillis() - beginTiem <= SINGLE_CLICK_TIME && !mIsMoving) {
                    performClick();
                }
                mIsMoving = false;
                if(Math.abs(getTranslationY()) <= (float) mHeight/3) {
                    setTranslationY(0);
                } else {
                    fadeOutAnimator(200);
                }
                break;
        }
        mLastY = (int)event.getRawY();
        mLastX = (int)event.getRawX();
        return true;
    }

    private boolean isMove(float curX,float curY) {
        return Math.abs(curX - mLastX) >= mTouchSlop || Math.abs(curY - mLastY) >= mTouchSlop;
    }

    public void showEnterAnimator(long time) {
        setVisibility(VISIBLE);
        //向下移动动画
        TranslateAnimation downTranslateAnimation=new TranslateAnimation(0,0,-mHeight, 0);
        downTranslateAnimation.setDuration(time);
        downTranslateAnimation.setFillAfter(true);

        mIsPalyingAnimation = true;
        startAnimation(downTranslateAnimation);

        downTranslateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mIsPalyingAnimation = false;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    public void fadeOutAnimator(long time) {
        if(mIsMoving) {
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    fadeOutAnimator(300);
                }
            }, 1500);
            return ;
        }
        //向上移动动画
        TranslateAnimation downTranslateAnimation=new TranslateAnimation(0,0,0, -mHeight - getTranslationY());
        downTranslateAnimation.setDuration(time);
        downTranslateAnimation.setFillAfter(true);

        mIsPalyingAnimation = true;
        startAnimation(downTranslateAnimation);

        //动画监听
        downTranslateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}
            @Override
            public void onAnimationEnd(Animation animation){
                setVisibility(GONE); //动画结束 消除视图
                mIsPalyingAnimation = false;
            }
            @Override
            public void onAnimationRepeat(Animation animation) {}
        });
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }
}

定义一个layout文件

<?xml version="1.0" encoding="utf-8"?>
<com.example.randfood.customview.TopTipsLinearLayout
    android:id="@+id/top_tips_root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:visibility="invisible">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是一个消息弹窗"
        android:layout_gravity="center_horizontal"
        android:textSize="30sp"
        android:padding="20dp"
        android:background="@drawable/messageview_top_pop_bg"/>

</com.example.randfood.customview.TopTipsLinearLayout>

调用下面的方法即可

private static void initPopUpWindow(Context context, View parentView) {
        View view = View.inflate(context, R.layout.messageview_top_pop, null);
        PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, false);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(false);
        //让popupwindow可以显示在状态栏中
        popupWindow.setClippingEnabled(false);
//        popupWindow.setTouchable(true);
        popupWindow.showAtLocation(parentView, Gravity.TOP, 0, 0);
        TopTipsLinearLayout layout = view.findViewById(R.id.top_tips_root);
        //让viewgroup中的内容显示在状态栏下面
        layout.setPadding(0, DisplayUtil.getStatusBarHeight(context), 0, 0);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "你点击了顶部消息", Toast.LENGTH_SHORT).show();
            }
        });
        layout.post(new Runnable() {
            @Override
            public void run() {
                layout.showEnterAnimator(300);
            }
        });
        layout.postDelayed(new Runnable() {
            @Override
            public void run() {
                layout.fadeOutAnimator(300);
            }
        }, 5000);
    }

加载全部内容

相关教程
猜你喜欢
用户评论