这篇文章主要讲解了“怎么用Android PopUpWindow实现卡片式弹窗”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用Android PopUpWindow实
这篇文章主要讲解了“怎么用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实现卡片式弹窗这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
--结束END--
本文标题: 怎么用Android PopUpWindow实现卡片式弹窗
本文链接: https://lsjlt.com/news/321094.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0