一、创建一个 DialogFromBotton
package com.example.myapplication;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import androidx.annotation.NonNull;
public class DialogFromBottom extends Dialog {
private final static int mAnimationDuration = 100; // 动画持续时间
// 持有 ContentView,为了做动画
private View mContentView;
private boolean mIsAnimating = false; // 是否正在进行动画
private OnBottomSheetShowListener mOnBottomSheetShowListener;
public DialogFromBottom(@NonNull Context context) {
super(context, R.style.AppTheme_BottomSheet);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getDecorView().setPadding(0, 0, 0, 0);
// 在底部,宽度撑满
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.BOTTOM | Gravity.CENTER; // dialog从哪里弹出
// 弹出窗口的宽高
int screenWidth = DisplayHelper.getScreenWidth(getContext());
int screenHeight = DisplayHelper.getScreenHeight(getContext());
params.width = screenWidth < screenHeight ? screenWidth : screenHeight;
getWindow().setAttributes(params);
setCanceledOnTouchOutside(true);
}
@Override
public void setContentView(int layoutResID) {
mContentView = LayoutInflater.from(getContext()).inflate(layoutResID, null);
super.setContentView(mContentView);
}
@Override
public void setContentView(@NonNull View view) {
mContentView = view;
super.setContentView(view);
}
@Override
public void setContentView(@NonNull View view, ViewGroup.LayoutParams params) {
mContentView = view;
super.setContentView(view, params);
}
/**
* BottomSheet升起动画
*/
private void animateUp() {
if (mContentView == null) {
return;
}
// 平移动画
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f
);
// 透明度动画
AlphaAnimation alpha = new AlphaAnimation(0, 1);
// 动画集
AnimationSet set = new AnimationSet(true);
set.addAnimation(translate);
set.addAnimation(alpha);
set.setInterpolator(new DecelerateInterpolator());
set.setDuration(mAnimationDuration);
set.setFillAfter(true);
mContentView.startAnimation(set);
}
/**
* BottomSheet降下动画
*/
private void animateDown() {
if (mContentView == null) {
return;
}
// 平移动画
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f
);
// 透明度动画
AlphaAnimation alpha = new AlphaAnimation(1, 0);
// 动画集
AnimationSet set = new AnimationSet(true);
set.addAnimation(translate);
set.addAnimation(alpha);
set.setInterpolator(new DecelerateInterpolator());
set.setDuration(mAnimationDuration);
set.setFillAfter(true);
// 设置动画监听
set.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
mIsAnimating = true;
}
@Override
public void onAnimationEnd(Animation animation) {
mIsAnimating = false;
/**
* Bugfix: Attempting to destroy the window while drawing!
*/
mContentView.post(new Runnable() {
@Override
public void run() {
// java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView{22dbf5b V.E...... R......D 0,0-1080,1083} not attached to window manager
// 在dismiss的时候可能已经detach了,简单try-catch一下
try {
DialogFromBottom.super.dismiss();
} catch (Exception e) {
// QMUILog.w(TAG, "dismiss error\n" + Log.getStackTraceString(e));
}
}
});
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mContentView.startAnimation(set);
}
@Override
public void show() {
super.show();
animateUp(); // 上升动画
if (mOnBottomSheetShowListener != null) {
mOnBottomSheetShowListener.onShow();
}
}
@Override
public void dismiss() {
if (mIsAnimating) {
return;
}
animateDown(); // 下降动画
}
// 底部抽屉弹出的监听接口
public interface OnBottomSheetShowListener {
void onShow();
}
}
二、创建DisplayHelper
package com.example.myapplication;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;
public class DisplayHelper {
/**
* DisplayMetrics
* @return
*/
public static DisplayMetrics getDisplayMetrics(Context context){
DisplayMetrics displayMetrics = new DisplayMetrics();
((WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics;
}
/**
* 屏幕宽度
* @return
*/
public static int getScreenWidth(Context context){
return getDisplayMetrics(context).widthPixels;
}
/**
* 屏幕高度
* @return
*/
public static int getScreenHeight(Context context){
return getDisplayMetrics(context).heightPixels;
}
}
三、Activity如下:
package com.example.myapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button mDialog; // 弹出对话框的按钮
private DialogFromBottom dialogFromBottom;
private View dialogContent; // 对话框的内容布局
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
// 初始化视图
private void initView() {
// 获得LayoutInflater对象
LayoutInflater from = LayoutInflater.from(this);
// 通过LayoutInflater实例化dilogue的布局
View dialogContent = from.inflate(R.layout.dialog_layout, null, false);
// 弹出按钮的实例化,绑定视图和点击事件
mDialog = (Button) findViewById(R.id.dialog);
mDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFromBottom();
}
});
// 在对话框布局中找到TextView,设置点击事件
TextView aa = (TextView) dialogContent.findViewById(R.id.textViewaaaa);
aa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 点击关闭对话框
dialogFromBottom.dismiss();
}
});
//创建一个自定义对话框实例并设置对话框的内容布局
dialogFromBottom = new DialogFromBottom(this);
dialogFromBottom.setContentView(dialogContent);
}
// 显示从底部弹出的对话框
private void DialogFromBottom() {
dialogFromBottom.show();
}
}
dialog_layout布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eee"
android:orientation="vertical">
<!--<TextView-->
<!--android:id="@+id/open_from_camera"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="40dp"-->
<!--android:background="@android:color/white"-->
<!--android:gravity="center"-->
<!--android:padding="8dp"-->
<!--android:text="相机"-->
<!--android:textColor="#333"-->
<!--android:textSize="15sp" />-->
<!--<TextView-->
<!--android:id="@+id/open_album"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="40dp"-->
<!--android:layout_marginTop="1dp"-->
<!--android:background="@android:color/white"-->
<!--android:gravity="center"-->
<!--android:padding="8dp"-->
<!--android:text="打开图库"-->
<!--android:textColor="#333"-->
<!--android:textSize="15sp" />-->
<!--<TextView-->
<!--android:id="@+id/cancel"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="40dp"-->
<!--android:layout_marginTop="1dp"-->
<!--android:background="@android:color/white"-->
<!--android:gravity="center"-->
<!--android:padding="8dp"-->
<!--android:text="取消"-->
<!--android:textColor="#333"-->
<!--android:textSize="15sp" />-->
<TextView
android:layout_width="match_parent"
android:layout_height="460dp"
android:background="#000"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xxx"
android:textSize="24sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textViewaaaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭"
android:textColor="@android:color/white"
android:layout_alignParentEnd="true"
android:layout_marginEnd="24dp"
android:layout_alignParentTop="true" />
</RelativeLayout>
这里activity_main就不写了,里面就一个Button