AlertDialog 基本使用與動畫處理

基本使用

  • 創建builder
    AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.myAlertDialog);
  • 創建並注入view

View view = LayoutInflater.from(this).inflate(R.layout.dialog_note_color_tips, null);
builder.setView(view);

對view進行操作

  • 展示

AlertDialog dialog= builder.create();
dialog .show

升級用法

  • 添加彈框位置設置
    在show之後調用

Window dialogWindow = mNotesColorTipsDialog.getWindow();
dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.gravity = Gravity.CENTER;
lp.width = 400;
dialogWindow.setAttributes(lp);

-增加動畫

dialogWindow.setWindowAnimations(R.style.dialogWindowAnimUp);

整體代碼如下:

if (mNotesColorTipsDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.myAlertDialog);
            View view = LayoutInflater.from(this).inflate(R.layout.dialog_note_color_tips, null);
            builder.setView(view);
            builder.setCancelable(false);
            mNotesColorTipsDialog = builder.create();
            Button button = view.findViewById(R.id.btn);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mNotesColorTipsDialog.dismiss();
                }
            });
            checkBox = view.findViewById(R.id.cb);
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    SharedPreferencesHelper.getSpInstance(mContext).put(Constants.NOTE_COLOR_TIP_SHOW, !isChecked);
                    if (isChecked) {
                        checkBox.setTextColor(getResources().getColor(R.color.purple));
                    } else {
                        checkBox.setTextColor(getResources().getColor(R.color.gray));
                    }
                }
            });
        }
        checkBox.setChecked(!show);

        mNotesColorTipsDialog.show();
        Window dialogWindow = mNotesColorTipsDialog.getWindow();
        dialogWindow.setWindowAnimations(R.style.dialogWindowAnimUp);
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = 400;
        dialogWindow.setAttributes(lp);

動畫增加插值器

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/anticipate_interpolator">
<translate
    android:duration="1000"
    android:fromYDelta="0"
    android:toYDelta="-100%" />
</set>

幾個插值器的介紹:

java類 xml資源id 說明
AccelerateDecelerateInterpolator @android:anim/accelerate_decelerate_interpolator 其變化開始和結束速率較慢,中間加速
AccelerateInterpolator @android:anim/accelerate_interpolator 其變化開始速率較慢,後面加速
DecelerateInterpolator @android:anim/decelerate_interpolator 其變化開始速率較快,後面減速
LinearInterpolator @android:anim/linear_interpolator 其變化速率恆定
AnticipateInterpolator @android:anim/anticipate_interpolator 其變化開始向後甩,然後向前
AnticipateOvershootInterpolator @android:anim/anticipate_overshoot_interpolator 其變化開始向後甩,然後向前甩,過沖到目標值,最後又回到了終值
OvershootInterpolator @android:anim/overshoot_interpolator 其變化開始向前甩,過沖到目標值,最後又回到了終值
BounceInterpolator @android:anim/bounce_interpolator 其變化在結束時反彈
CycleInterpolator @android:anim/cycle_interpolator 循環播放,其速率爲正弦曲線
TimeInterpolator 一個接口,可以自定義插值器

兩步實現彈框圓角透明背景

  • 設置透明樣式,創建時傳入即可實現透明背景
<style name="myAlertDialog" parent="Theme.MaterialComponents.Dialog.Alert">
        <item name="android:background">@android:color/transparent</item>
    </style>
  • 然後在佈局文件裏添加圓角背景
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@drawable/bg_corner"//設置圓角背景
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="wrap_content">
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章