Android_自定义Dialog底部弹出动画

一、前言:

效果图如下:

AAAA.gif

二、代码:

1. 点击Button调用代码

 private void show() {
        Dialog dialog = new Dialog(this);
        //去掉标题线
        dialog.requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog);
        //背景透明
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.show();

        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER; // 居中位置
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        window.setWindowAnimations(R.style.mystyle);  //添加动画
    }

2. Dialog的自定义布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="用户名" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="密码" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定" />
    </LinearLayout>
</LinearLayout>

3. Dialog的自定义布局Share样式,drawable里面新建

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--圆角-->
    <corners android:radius="30dp" />

    <!--填充色-->
    <solid android:color="#ffffff" />
</shape>

4. 弹出动画样式,在styles里面添加

 <!--弹窗动画-->
    <style name="mystyle" parent="android:Animation">
        <!--//进入时的动画-->
        <item name="@android:windowEnterAnimation">@anim/dialog_enter</item>
        <!--//退出时的动画-->
        <item name="@android:windowExitAnimation">@anim/dialog_exit</item>
    </style>

5. 弹出动画,新建anim文件目录,一个进入 一个退出的,

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromYDelta="100%"
        android:toYDelta="0" />
</set>

6. 退出的

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromYDelta="0"
        android:toYDelta="100%" />
</set>

参考链接:https://www.jianshu.com/p/ccf6a3c23c72

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 效果图如下 点击Button调用代码 Dialog的自定义布局 Dialog的自定义布局Share样式,drawa...
    Lrxc阅读 17,159评论 0 9
  • 【Android 动画】 动画分类补间动画(Tween动画)帧动画(Frame 动画)属性动画(Property ...
    Rtia阅读 11,382评论 1 38
  • 动画基础概念 动画分类 Android 中动画分为两种,一种是 Tween 动画、还有一种是 Frame 动画。 ...
    Rtia阅读 5,027评论 0 6
  • 这里主要做的就是想让你可以进行各种Dialog的显示功能,如果想要各种不同样式的就可以进行配置不同的文件设置...
    Charon_Pluto阅读 7,162评论 0 7
  • 1 背景 不能只分析源码呀,分析的同时也要整理归纳基础知识,刚好有人微博私信让全面说说Android的动画,所以今...
    未聞椛洺阅读 7,664评论 0 10

友情链接更多精彩内容