怎么用Android PopUpWindow实现卡片式弹窗

怎么用Android PopUpWindow实现卡片式弹窗

这篇文章主要讲解了“怎么用AndroidPopUpWindow实现卡片式弹窗”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用AndroidPopUpWindow实现卡片式弹窗”吧!

效果图:

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

//用于实现顶部弹窗动画以及向上滑动动画publicclassTopTipsLinearLayoutextendsLinearLayout{privatestaticfinalStringTAG="Hyh";privateintmHeight;privatebooleanmIsFirstLayout=true;privatebooleanmIsPalyingAnimation=false;privateintmLastY=0;privateintmLastX=0;privatefinalintmTouchSlop=4;privatefinallongSINGLE_CLICK_TIME=300;privatelongbeginTiem=0;privatebooleanmIsMoving=false;publicTopTipsLinearLayout(Contextcontext){super(context);}publicTopTipsLinearLayout(Contextcontext,@NullableAttributeSetattrs){super(context,attrs);}publicTopTipsLinearLayout(Contextcontext,@NullableAttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);}@RequiresApi(api=Build.VERSION_CODES.LOLLIPOP)publicTopTipsLinearLayout(Contextcontext,AttributeSetattrs,intdefStyleAttr,intdefStyleRes){super(context,attrs,defStyleAttr,defStyleRes);}@OverrideprotectedvoidonLayout(booleanchanged,intl,intt,intr,intb){super.onLayout(changed,l,t,r,b);if(mIsFirstLayout){mHeight=getHeight();mIsFirstLayout=false;}}@OverridepublicbooleanonInterceptTouchEvent(MotionEventev){returnsuper.onInterceptTouchEvent(ev);}@SuppressLint("ClickableViewAccessibility")@OverridepublicbooleanonTouchEvent(MotionEventevent){switch(event.getAction()){caseMotionEvent.ACTION_DOWN:mIsMoving=false;beginTiem=System.currentTimeMillis();break;caseMotionEvent.ACTION_MOVE:intdeltay=(int)event.getRawY()-mLastY;if(!mIsPalyingAnimation){if(deltay<0||getTranslationY()+deltay<=0){setTranslationY(getTranslationY()+deltay);}}if(isMove(event.getRawX(),event.getRawY())){mIsMoving=true;}break;caseMotionEvent.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();returntrue;}privatebooleanisMove(floatcurX,floatcurY){returnMath.abs(curX-mLastX)>=mTouchSlop||Math.abs(curY-mLastY)>=mTouchSlop;}publicvoidshowEnterAnimator(longtime){setVisibility(VISIBLE);//向下移动动画TranslateAnimationdownTranslateAnimation=newTranslateAnimation(0,0,-mHeight,0);downTranslateAnimation.setDuration(time);downTranslateAnimation.setFillAfter(true);mIsPalyingAnimation=true;startAnimation(downTranslateAnimation);downTranslateAnimation.setAnimationListener(newAnimation.AnimationListener(){@OverridepublicvoidonAnimationStart(Animationanimation){}@OverridepublicvoidonAnimationEnd(Animationanimation){mIsPalyingAnimation=false;}@OverridepublicvoidonAnimationRepeat(Animationanimation){}});}publicvoidfadeOutAnimator(longtime){if(mIsMoving){postDelayed(newRunnable(){@Overridepublicvoidrun(){fadeOutAnimator(300);}},1500);return;}//向上移动动画TranslateAnimationdownTranslateAnimation=newTranslateAnimation(0,0,0,-mHeight-getTranslationY());downTranslateAnimation.setDuration(time);downTranslateAnimation.setFillAfter(true);mIsPalyingAnimation=true;startAnimation(downTranslateAnimation);//动画监听downTranslateAnimation.setAnimationListener(newAnimation.AnimationListener(){@OverridepublicvoidonAnimationStart(Animationanimation){}@OverridepublicvoidonAnimationEnd(Animationanimation){setVisibility(GONE);//动画结束消除视图mIsPalyingAnimation=false;}@OverridepublicvoidonAnimationRepeat(Animationanimation){}});}@OverrideprotectedvoidonAttachedToWindow(){super.onAttachedToWindow();}}

定义一个layout文件

<?xmlversion="1.0"encoding="utf-8"?><com.example.randfood.customview.TopTipsLinearLayoutandroid: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"><TextViewandroid: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>

调用下面的方法即可

privatestaticvoidinitPopUpWindow(Contextcontext,ViewparentView){Viewview=View.inflate(context,R.layout.messageview_top_pop,null);PopupWindowpopupWindow=newPopupWindow(view,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,false);popupWindow.setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));popupWindow.setOutsideTouchable(false);//让popupwindow可以显示在状态栏中popupWindow.setClippingEnabled(false);//popupWindow.setTouchable(true);popupWindow.showAtLocation(parentView,Gravity.TOP,0,0);TopTipsLinearLayoutlayout=view.findViewById(R.id.top_tips_root);//让viewgroup中的内容显示在状态栏下面layout.setPadding(0,DisplayUtil.getStatusBarHeight(context),0,0);layout.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){Toast.makeText(context,"你点击了顶部消息",Toast.LENGTH_SHORT).show();}});layout.post(newRunnable(){@Overridepublicvoidrun(){layout.showEnterAnimator(300);}});layout.postDelayed(newRunnable(){@Overridepublicvoidrun(){layout.fadeOutAnimator(300);}},5000);}

感谢各位的阅读,以上就是“怎么用AndroidPopUpWindow实现卡片式弹窗”的内容了,经过本文的学习后,相信大家对怎么用AndroidPopUpWindow实现卡片式弹窗这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是恰卡编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

发布于 2022-01-21 23:15:42
收藏
分享
海报
0 条评论
50
上一篇:Java哈希法代码怎么写 下一篇:怎么用C++ OpenCV实现像素画
目录

    推荐阅读

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码