AlertDialog

简介

xxx

封装

package widget.dialog.four;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.xalikai.bnmdstudentend.R;

import util.ScreenUtils;

/**
 * Created on 2019/2/27.
 *
 * @author 郑少鹏
 * @desc 上/下课对话框
 */
public class GoToOrGetOutOfClassDialog extends AlertDialog {
    private Context context;
    private boolean goToClass;
    private IMMessage imMessage;

    public GoToOrGetOutOfClassDialog(@NonNull Context context, int themeResId, boolean goToClass, IMMessage imMessage) {
        super(context, themeResId);
        this.context = context;
        this.goToClass = goToClass;
        this.imMessage = imMessage;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        @SuppressLint("InflateParams") View contentView = layoutInflater.inflate(R.layout.go_to_or_get_out_of_class, null);
        setContentView(contentView);
        // 内容
        TextView goToOrGetOutOfClassTvContent = contentView.findViewById(R.id.goToOrGetOutOfClassTvContent);
        goToOrGetOutOfClassTvContent.setText(goToClass ? String.format(context.getString(R.string.goToClassHint), imMessage.getFromNick()) :
                String.format(context.getString(R.string.getOutOfClassHint), imMessage.getFromNick()));
        // 点监听
        TextView goToOrGetOutOfClassTvOkay = contentView.findViewById(R.id.goToOrGetOutOfClassTvOkay);
        goToOrGetOutOfClassTvOkay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        // window对象
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.width = ScreenUtils.screenWidth(context) / 3;
            attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(attributes);
            // 动画
            window.setWindowAnimations(R.style.SelfDialogAnimation);
            // 位置
            window.setGravity(Gravity.CENTER);
        }
    }
}

styles

<style name="BaseDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowSoftInputMode">adjustPan</item>
    <item name="android:windowIsFloating">true</item>
</style>

<!--SelfDialogAnimation-->
<style name="SelfDialogAnimation">
    <item name="android:windowEnterAnimation">@anim/sweet_alert_dialog_in</item>
    <item name="android:windowExitAnimation">@anim/sweet_alert_dialog_out</item>
</style>

anim(sweet_alert_dialog_in)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:shareInterpolator="true">
    <alpha
        android:duration="90"
        android:fromAlpha="0.2"
        android:toAlpha="1" />

    <scale
        android:duration="135"
        android:fromXScale="0.7"
        android:fromYScale="0.7"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.05"
        android:toYScale="1.05" />

    <scale
        android:duration="105"
        android:fromXScale="1.05"
        android:fromYScale="1.05"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="135"
        android:toXScale="0.95"
        android:toYScale="0.95" />

    <scale
        android:duration="60"
        android:fromXScale="0.95"
        android:fromYScale="0.95"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="240"
        android:toXScale="1"
        android:toYScale="1" />
</set>

anim(sweet_alert_dialog_out)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:shareInterpolator="true">
    <scale
        android:duration="150"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.6"
        android:toYScale="0.6" />

    <alpha
        android:duration="150"
        android:fromAlpha="1"
        android:toAlpha="0" />
</set>

主代码

new GoToOrGetOutOfClassDialog(activity, R.style.BaseDialog, true, imMessage).show();

资料

AlertDialog的六种创建方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snpmyn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值