自定义dialog和弹出dialog的动画

时间:2022-11-01 13:11:29

自定义dialog

  final Dialog dialog = new Dialog(ChartVisit.this);
LayoutInflater inflater=getLayoutInflater();
final View dialogview=inflater.inflate(R.layout.layout_fragment_chart_visit_screen_dialog, null);
Window dialogWindow = dialog.getWindow();
dialogWindow.setGravity(Gravity.CENTER | Gravity.BOTTOM);//设置dialog的位置
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//取消dialog的默认标题
//必须填写上面找到的View ,不然会再找一遍view,导致里面的点击事件不反应
//dialog.setContentView(R.layout.layout_fragment_chart_visit_screen_dialog);
dialog.setContentView(dialogview);
dialog.setCanceledOnTouchOutside(false);//点击其他位置可以取消dialog

TextView textView= (TextView) dialogview.findViewById(R.id.textview_back);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
LinearLayout linearLayout= (LinearLayout) dialogview.findViewById(R.id.linearlayout);
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MyDialog myDialog=new MyDialog(ChartVisit.this,R.style.NoDialogTitle);
myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
myDialog.showDialog(R.layout.layout_fragment_chart_visit_screen2,0,0);

// Toast.makeText(ChartVisit.this, "我是dialog", Toast.LENGTH_SHORT).show();
// Dialog dialog1=new Dialog(ChartVisit.this);
// LayoutInflater inflater=getLayoutInflater();
// final View dialogview1=inflater.inflate(R.layout.layout_fragment_chart_visit_screen2, null);
// Window dialogWindow = dialog1.getWindow();
// dialogWindow.setGravity(Gravity.CENTER | Gravity.BOTTOM);//设置dialog的位置
// /**
// * 对dialog的设置必须放在setContenView之前
// */

// dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);//取消dialog的默认标题
// dialog1.setCanceledOnTouchOutside(false);//点击其他位置可以取消dialog
// dialog1.setContentView(dialogview1);
// //添加动画
// TranslateAnimation animation=new TranslateAnimation(dialogview.getWidth(),0,0,0);
// animation.setDuration(2000);
// dialogview1.setAnimation(animation);
// animation.start();
//
// dialog1.show();
// WindowManager windowManager = getWindowManager();
// Display display = windowManager.getDefaultDisplay();
// WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
// lp.width = (int)(display.getWidth()); //设置宽度
// (lp).height=(int)dialogview.getHeight();//设置高度
// dialog1.getWindow().setAttributes(lp);
}
});
dialog.show();
/**
* 设置dialog的大小必须在show()的后面,不然不起作用
*/

WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = (int)(display.getWidth());
//(lp).height=(int)dialogview.getHeight();//设置宽度
dialog.getWindow().setAttributes(lp);

弹出dialog动画

MyDialog

public class MyDialog extends Dialog {
private Window window = null;

public MyDialog(Context context)
{
super(context);
}
//这个构造函数可以设置dialog的属性
public MyDialog(Context context, int themeResId) {
super(context, themeResId);
}

public void showDialog(int layoutResID, int x, int y){
setContentView(layoutResID);

windowDeploy(x, y);

//设置触摸对话框意外的地方取消对话框
setCanceledOnTouchOutside(true);
show();
}

//设置窗口显示
public void windowDeploy(int x, int y){
window = getWindow(); //得到对话框
window.setGravity(Gravity.BOTTOM|Gravity.CENTER);
window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
//window.setBackgroundDrawableResource(R.color.vifrification); //设置对话框背景为透明
WindowManager.LayoutParams wl = window.getAttributes();
//根据x,y坐标设置窗口需要显示的位置
wl.x = x; //x小于0左移,大于0右移
wl.y = y; //y小于0上移,大于0下移
wl.width=window.getWindowManager().getDefaultDisplay().getWidth();//设置dialog的款
// wl.width= AppApplication.getApplication().getWallpaperDesiredMinimumWidth();
wl.height=350;//设置dialog的高
// wl.alpha = 0.6f; //设置透明度
// wl.gravity = Gravity.BOTTOM; //设置重力
window.setAttributes(wl);
}
}

先在res中创建anim在中创建animator

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--<scale-->
<!--android:interpolator="@android:anim/accelerate_interpolator"-->
<!--android:fromXScale="1.0"-->
<!--android:toXScale="1.0"-->
<!--android:fromYScale="0.0"-->
<!--android:toYScale="1.0"-->
<!--android:pivotX="0%"-->
<!--android:pivotY="100%"-->
<!--android:fillAfter="false"-->
<!--android:duration="400"/>-->
<translate
android:fromXDelta="800"
android:toXDelta="0"
android:duration="400"/>

</set>

然后在styles中添加动画

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
//dialog的属性
<style parent="@android:Theme.Dialog" name="NoDialogTitle">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
//设置dialog的背景颜色
<item name="android:background">#ffffff</item>
</style>
//dialog的动画
<style name="dialogWindowAnim" parent="android:Animation" mce_bogus="1">
<item name="android:windowEnterAnimation">@anim/animator</item>
</style>

</resources>

自定义dialog和弹出dialog的动画
如果在new Dialog();时,不加R.style….dialog不会全屏即使你在设置dialog的宽的时候为全屏,因为dialog本身就有一定的属性
如图:
自定义dialog和弹出dialog的动画