android-自定义dialog&进入退出动画

本文介绍了如何在Android中创建一个自定义Dialog,该Dialog从顶部下落进入屏幕中央,并在退出时从中部下落消失。详细步骤包括自定义Dialog的实现、进入动画drop_enter.xml和退出动画pop_exit.xml的设置,以及相应的布局文件设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


dropDialg: 从顶部下落到屏幕中间进入,退出从屏幕中间下落消失

效果如图

代码实现

① 自定义drop dialog

注意自定义dialog继承自AppCompatDialog

public class MyDialog extends AppCompatDialog {
    public MyDialog(Context context) {
        super(context, R.style.mydialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_mydialog);
        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        int widthPixels = metrics.widthPixels;
        int heightPixels = metrics.heightPixels;
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.width = (int) (widthPixels / 2.0f);
        layoutParams.height = (int) (heightPixels / 4.0f);
        getWindow().setAttributes(layoutParams);
        initView();
    }

    private void initView() {
        Button btnDis = (Button) findViewById(R.id.id_btn_dis);
        btnDis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}

② R.style.myDialog

 <style name="mydialog" parent="AlertDialog.AppCompat">
        <item name="android:windowAnimationStyle">@style/dialog_enter_exit</item>
    </style>

    <style name="dialog_enter_exit" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/pop_enter</item>
        <item name="android:windowExitAnimation">@anim/pop_exit</item>
    </style>

③ 进入动画 pop_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fillAfter="true"
        android:fromYDelta="-50%p"
        android:toYDelta="0%p" />
</set>


④ 退出动画 pop_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <translate
        android:duration="500"
        android:fillAfter="true"
        android:fromYDelta="0%p"
        android:toYDelta="100%p" />
</set>

⑤ R.layout.layout_mydialog

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#33ffff"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="dialog" />

    <Button
        android:id="@+id/id_btn_dis"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dismiss" />
</LinearLayout>

bottomDialog: 从屏幕底部弹出,从屏幕底部消失

效果图:

代码实现:

和dropDialog相比,改变的是

layoutParams.gravity = Gravity.BOTTOM;

pop_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fillAfter="true"
        android:fromYDelta="100%p"
        android:toYDelta="0%p" />
</set>

还有pop_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <translate
        android:duration="500"
        android:fillAfter="true"
        android:fromYDelta="0%p"
        android:toYDelta="100%p" />
</set>

① 自定义dailog,继承自AppCompatDialog

public class MyDialog extends AppCompatDialog {
    public MyDialog(Context context) {
        super(context, R.style.mydialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_mydialog);
        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        int widthPixels = metrics.widthPixels;
        int heightPixels = metrics.heightPixels;
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = widthPixels;
        layoutParams.height = (int) (heightPixels / 3.0f);
        getWindow().setAttributes(layoutParams);
        initView();
    }

    private void initView() {
        Button btnDis = (Button) findViewById(R.id.id_btn_dis);
        btnDis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}

② R.style.mydialog

 <style name="mydialog" parent="AlertDialog.AppCompat">
        <item name="android:windowAnimationStyle">@style/dialog_enter_exit</item>
    </style>

    <style name="dialog_enter_exit" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/pop_enter</item>
        <item name="android:windowExitAnimation">@anim/pop_exit</item>
    </style>

③ anim: pop_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fillAfter="true"
        android:fromYDelta="100%p"
        android:toYDelta="0%p" />
</set>

④ anim: pop_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <translate
        android:duration="500"
        android:fillAfter="true"
        android:fromYDelta="0%p"
        android:toYDelta="100%p" />
</set>


⑤ layout布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#33ffff"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="dialog" />

    <Button
        android:id="@+id/id_btn_dis"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dismiss" />
</LinearLayout>








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值