欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Android PopUpWindow实现卡片式弹窗

程序员文章站 2023-12-29 14:00:34
首先自定义一个viewgroup,继承自linerlayout,为了实现上下滑动//用于实现顶部弹窗动画 以及向上滑动动画public class toptipslinearlayout exten...

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弹窗内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

上一篇:

下一篇: