1、开发工具与关键技术:Eclipse、AndroidStudio
2、撰写时间:2020年04月28日
Android的Dialog的使用,相信大家用得不少吧!那么如何使Dialog添加一些动画效果或者自定义样式的呢!今天,带你学习一个简单又生动的Dialog。学不会?你找我。
第一、Dialog工具类
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.example.hote.R;
import com.example.hote.bean.RoomTypeVo;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import java.util.Locale;
//继承Dialog
public class RoomTypeInfoDialog extends Dialog {
private Context context;
private RoomTypeVo roomType;
//这里我传递里RoomTypeVo 类
public RoomTypeInfoDialog(@NonNull Context context,RoomTypeVo roomType) {
super(context);
this.context=context;
this.roomType=roomType;
}
public RoomTypeInfoDialog(@NonNull Context context, int themeResId,RoomTypeVo roomType) {
super(context, themeResId);
this.context=context;
this.roomType=roomType;
}
//在onCreate方法中和一般的Activity使用是一样,这里呢?根据自己的需求,我就简单获取几个参数。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置布局
setContentView(R.layout.dialog_room_type_info);
//获取控件
TextView tvRoomTypeName = findViewById(R.id.tv_room_type_name);
//人数
TextView tvPeopleNum = findViewById(R.id.tv_room_type_people_num);
//客房类型
tvRoomTypeName.setText(roomType.getRoomTypeName());
//人数
tvPeopleNum.setText(String.valueOf(roomType.getPeopleNumber()));
}
第二、在style中设置样式
<resources>
<style name="DialogTheme" parent="@android:style/Theme.Dialog">
<!-- 边框 -->
<item name="android:windowFrame">@null</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 半透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 无标题 -->
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<!-- 背景透明 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 模糊 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 遮罩层 -->
<item name="android:backgroundDimAmount">0.5</item>
</style>
<!-- dialog的动画 -->
<style name="dialog_menu_animStyle">
//弹出效果
<item name="android:windowEnterAnimation">@anim/dialog_in_anim</item>
// 隐藏效果
<item name="android:windowExitAnimation">@anim/dialog_out_anim</item>
</style>
</resources>
第三、在res文件夹中创建Animation类型的文件
1、弹出效果,注:这里我使用的是从下往上出来的效果,可以根据需求修改
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--有四种动画:alpha (透明变化) translate(位置移动) scale(缩放) rotate(旋转),
android:duration: 动画运行时间,定义在多次时间(ms)内完成动画
android:startOffset: 延迟一定时间后运行动画
fromXDelta: X轴方向开始位置,可以是%,也可以是具体的像素 具体见图
toXDelta: X轴方向结束位置,可以是%,也可以是具体的像素
fromYDelta: Y轴方向开始位置,可以是%,也可以是具体的像素
toYDelta: Y轴方向结束位置,可以是%,也可以是具体的像素-->
<translate
android:duration="500"
android:fromXDelta="0"
android:fromYDelta="100%p"
android:toXDelta="0"
android:toYDelta="0%p" />
</set>
2、隐藏效果
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromXDelta="0"
android:fromYDelta="0%p"
android:toXDelta="0"
android:toYDelta="100%p" />
</set>
第四、点击事件
@Override
public void onImageClick(View view, RoomTypeVo data, int position) {
// Toast.makeText(mActivityContext, data.getPicture(), Toast.LENGTH_SHORT).show();
//1、初始化Dialog
RoomTypeInfoDialog dialog=new RoomTypeInfoDialog(mActivityContext,R.style.DialogTheme,data);
//获取Dialogwindow对象
Window window=dialog.getWindow();
//设置弹出位置
window.setGravity(Gravity.BOTTOM);
//设置动画
window.setWindowAnimations(R.style.dialog_menu_animStyle);
//设置对话框大小
window.getDecorView().setPadding(0,0,0,0);
WindowManager.LayoutParams layoutParams=window.getAttributes();
//设置宽度和高度
layoutParams.width=WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height=WindowManager.LayoutParams.WRAP_CONTENT;
//显示Dialog
dialog.show();
}
});