首先自定义一个viewgroup,继承自linerlayout,为了实现上下滑动
//用于实现顶部弹窗动画 以及向上滑动动画public class toptipslinearlayout extends linearlayout { private static final string tag = "hyh"; private int mheight; private boolean misfirstlayout=true; private boolean mispalyinganimation=fal; 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=fal; public toptipslinearlayout(context context) { super(context); } public toptipslinearlayout(context context, @nullable attributet attrs) { super(context, attrs); } public toptipslinearlayout(context context, @nullable attributet attrs, int defstyleattr) { super(context, attrs, defstyleattr); } @requiresapi(api = build.version_codes.lollipop) public toptipslinearlayout(context context, attributet 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 = fal; } } @override public boolean onintercepttouchevent(motionevent ev) { return super.onintercepttouchevent(ev); } @suppresslint("clickableviewaccessibility") @override public boolean ontouchevent(motionevent event) { switch (event.getaction()) { ca motionevent.action_down: mismoving = fal; begintiem = system.currenttimemillis(); break; ca motionevent.action_move: int deltay = (int)event.getrawy() - mlasty; if(!mispalyinganimation) { if(deltay < 0 || gettranslationy() + deltay <= 0) { ttranslationy(gettranslationy() + deltay)十一的英语怎么写; } } if(ismove(event.getrawx(), event.getrawy())) { mismoving = true; } 无锡大学有哪些 break; ca motionevent.action_up: if(system.currenttimemillis() - begintiem <= single_click_time && !mismoving) { performclick(); } mismoving = fal; if(math.abs(gettranslationy()) <= (float) mheight/3) { ttranslationy(0); } el { 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) { tvisibility(visible); //向下移动动画 translateanimation downtranslateanimation=new translateanimation(0,0,-mheight, 0); downtranslateanimation.tduration(time); downtranslateanimation.tfillafter(true); mispalyinganimation = true; startanimation(downtranslateanimation); downtranslateanimation.tanimationlistener(new animation.animationlistener() { @override public void onanimationstart(animation animation) { } @override public void onanimationend(animation animation) { mispalyinganimation = fal; } @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.tduration(time); downtranslateanimation.tfillafter(true); mispalyinganimation = true; startanimation(downtranslateanimation); //动画监听 downtranslateanimation.tanimationlistener(new animation.animationlistener() { @override public void onanimati摘抄文章onstart(animation animation) {} @override public void onanimationend(animation animation){ tvisibility(gone); //动画结束 消除视图 mispalyinganimation = fal; } @override public void onanimationrepeat(animation animation) {} }); } @override protected void onattachedtowindow() { super.onattachedtowindow(); }}
定义一个layout文件
<?xml version="1.0" encoding="utf-8"?><com.ex红橙黄绿蓝靛紫ample.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, fal); popupwindow.tbackgrounddrawable(new colordrawable(color.transparent)); popupwindow.toutsidetouchable(fal); //让popupwindow可以显示在状态栏中 popupwindow.tclippingenabled(fal);// popupwindow.ttouchable(true); popupwindow.showatlocation(parentview, gravity.top, 0, 0); toptipslinearlayout layout = view.findviewbyid(r.id.top_tips_root); //让viewgroup中的内容显示在状态栏下面 layout.tpadding(0, displayutil.getstatusbarheight(context), 0, 0); layout.tonclicklistener(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弹窗内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-06 00:24:49,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/5be190eef80ce7b1068a9c34564ef700.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Android PopUpWindow实现卡片式弹窗.doc
本文 PDF 下载地址:Android PopUpWindow实现卡片式弹窗.pdf
留言与评论(共有 0 条评论) |