您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用Android PopUpWindow實現卡片式彈窗”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用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實現卡片式彈窗這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。