android中弹出的动画

时间:2015-12-03 05:46:26

标签: android animation android-intent popup android-animation

我在20秒后在主要活动中创建一个弹出窗口。我在UI的右上角有一个小的imageview。我希望我的弹出窗口来自该图像(如动画或从图像缩放闪光并显示弹出窗口)。我怎样才能做到这一点?希望你明白我想问的是什么。这是我在MainActivity类中完成的弹出代码。

Handler handler = new Handler();

        handler.postDelayed(new Runnable() {

            @Override

            public void run() {

                new AlertDialog.Builder(context)
                        .setTitle("Win Free Recharge")
                        .setMessage("Do you want to earn free recharge?")
                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // continue with delete
                                Intent intent = new Intent(MainActivity.this, WebActivity.class);
                                startActivity(intent);
                            }
                        })
                        .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // do nothing
                            }
                        })
                        .setIcon(R.drawable.inr1)
                        .show();
            }
        }, 20000);

1 个答案:

答案 0 :(得分:1)

在你的风格中添加这个样式属性

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:windowEnterAnimation">@anim/anim_scale_in</item>
        <item name="android:windowExitAnimation">@anim/anim_scale_out</item>
        <item name="colorAccent">@color/colorPrimary</item>
</style>
<\ n>在anim文件夹中,创建anim_scale_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="@android:integer/config_shortAnimTime"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromXScale="0.7"
        android:toXScale="1.0"
        android:fromYScale="0.7"
        android:toYScale="1.0"/>

    <alpha
        android:duration="@android:integer/config_shortAnimTime"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:fromAlpha="0"
        android:toAlpha="1.0"/>
</set>
<\ n>在anim文件夹中,创建anim_scale_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="@android:integer/config_shortAnimTime"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromXScale="1.0"
        android:toXScale="0.7"
        android:fromYScale="1.0"
        android:toYScale="0.7"/>

    <alpha
        android:duration="@android:integer/config_shortAnimTime"
        android:interpolator="@android:interpolator/accelerate_decelerate"
        android:fromAlpha="1.0"
        android:toAlpha="0.0"/>

</set>

使用以下代码在对话框中应用动画:

AlertDialog.Builder alert = new AlertDialog.Builder(mContext, R.style.MyAlertDialogStyle)
                        .setTitle("Title")
                        .setMessage("Message")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                Dialog mDialog = alert.create();
                mDialog.getWindow().getAttributes().windowAnimations = R.style.MyAlertDialogStyle;
                mDialog.show();