網站首頁 編程語言 正文
首先自定義一個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); }
原文鏈接:https://blog.csdn.net/Y810614/article/details/122609148
相關推薦
- 2023-02-14 C#實現ComboBox變色的示例代碼_C#教程
- 2022-07-07 圖解AVL樹數據結構輸入與輸出及實現示例_C 語言
- 2022-08-26 詳解Python中元組的三個不常用特性_python
- 2022-07-18 Liunx下使用SSH登錄遠程服務器
- 2022-10-16 python?math模塊使用方法介紹_python
- 2022-05-27 C++?算法精講之貪心算法_C 語言
- 2023-07-16 callBack: function(res){} 與 callBack: res =>{}
- 2022-08-26 如何利用python在剪貼板上讀取/寫入數據_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支