简介
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();