Android PopUpWindow實現卡片式彈窗
首先自定義一個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); }
到此這篇關於Android PopUpWindow實現卡片式彈窗的文章就介紹到這瞭,更多相關Android PopUpWindow彈窗內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Android Studio實現補間動畫
- android實現簡單的活動轉盤
- Android Studio實現幀動畫
- android跑馬燈出現重復跳動以及不滾動問題的解決方法
- Android實現控件拖動效果