Dialog的弹出动画(上下左右的弹出)
class CustomDialog(context: Context) : Dialog(context, R.style.user_default_dialog) {
private var mList = ArrayList<String>()
init {
init()
}
fun init() {
setContentView(R.layout.custom_dialog)
for (index in 0..100) {
mList.add("动画上下左右都有自己设置,就是玩$index")
}
recyclerView.layoutManager = LinearLayoutManager(context)
val mAdapter = CustomDialogAdapter(R.layout.custom_dialog_item)
mAdapter.setNewInstance(mList)
recyclerView.adapter = mAdapter
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="自定义dialog动画弹出"
android:textColor="@color/black"
android:textSize="18sp"
tools:ignore="HardcodedText,MissingConstraints" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tv" />
</androidx.constraintlayout.widget.ConstraintLayout>
class CustomDialogAdapter(layoutRestId: Int) : BaseQuickAdapter<String, BaseViewHolder>(layoutRestId) {
override fun convert(holder: BaseViewHolder, item: String) {
item.run {
holder.setText(R.id.tv, item)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="动画快出来"
android:textColor="@color/black"
android:textSize="16sp" />
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#ECEBE8" />
</LinearLayout>
<resources>
<style name="user_default_dialog" parent="@style/Theme.AppCompat.Dialog">
<item name="android:windowFrame">@null</item>
<!-- 边框 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 半透明 -->
<item name="android:windowNoTitle">true</item>
<!-- 无标题 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 背景透明 -->
<item name="android:backgroundDimEnabled">false</item>
<!-- 模糊 -->
<item name="android:backgroundDimAmount">0.4</item>
<!-- 设置窗口的透明度, 0.0~1.0,1表示不透明-->
</style>
<!-- 从上往下弹出-->
<style name="AnimTop" parent="@android:style/Animation">
<!--进入动画-->
<item name="android:windowEnterAnimation">@anim/dialog_top_in</item>
<!--退出动画-->
<item name="android:windowExitAnimation">@anim/dialog_top_out</item>
</style>
<!-- .从下往上弹出:-->
<style name="AnimBottom" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/dialog_bottom_in</item>
<item name="android:windowExitAnimation">@anim/dialog_bottom_out</item>
</style>
<!-- .从左往右弹出:-->
<style name="AnimLeft" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/dialog_left_in</item>
<item name="android:windowExitAnimation">@anim/dialog_left_out</item>
</style>
<!-- 从右往左弹出:-->
<style name="AnimRight" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/dialog_right_in</item>
<item name="android:windowExitAnimation">@anim/dialog_right_out</item>
</style>
<!-- 从中间弹出:-->
<style name="AnimCenter" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/dialog_center_in</item>
<item name="android:windowExitAnimation">@anim/dialog_center_out</item>
</style>
</resources>
<!-- 从上往下弹出-->
<style name="AnimTop" parent="@android:style/Animation">
<!--进入动画-->
<item name="android:windowEnterAnimation">@anim/dialog_top_in</item>
<!--退出动画-->
<item name="android:windowExitAnimation">@anim/dialog_top_out</item>
</style>
//dialog_top_in
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fillAfter="true">
<scale
android:fromXScale="1"
android:toXScale="1"
android:fromYScale="0"
android:toYScale="1"
android:pivotX="0"
android:pivotY="0"/>
</set>
//dialog_top_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fillAfter="false">
<scale
android:fromXScale="1"
android:toXScale="1"
android:fromYScale="1"
android:toYScale="0"
android:pivotX="0"
android:pivotY="0" />
</set>
<!-- .从下往上弹出:-->
<style name="AnimBottom" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/dialog_bottom_in</item>
<item name="android:windowExitAnimation">@anim/dialog_bottom_out</item>
</style>
//dialog_bottom_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200">
<scale
android:fromXScale="1"
android:fromYScale="0"
android:pivotX="0"
android:pivotY="100%"
android:toXScale="1"
android:toYScale="1" />
</set>
//dialog_bottom_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200">
<scale
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="0"
android:pivotY="100%"
android:toXScale="1"
android:toYScale="0" />
</set>
<!-- .从左往右弹出:-->
<style name="AnimLeft" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/dialog_left_in</item>
<item name="android:windowExitAnimation">@anim/dialog_left_out</item>
</style>
//dialog_left_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="300"/>
</set>
//dialog_left_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
<!-- 从右往左弹出:-->
<style name="AnimRight" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/dialog_right_in</item>
<item name="android:windowExitAnimation">@anim/dialog_right_out</item>
</style>
//dialog_right_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="300"/>
</set>
/dialog_right_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="300"/>
</set>
<!-- 从中间弹出:-->
<style name="AnimCenter" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/dialog_center_in</item>
<item name="android:windowExitAnimation">@anim/dialog_center_out</item>
</style>
//dialog_center_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200">
<scale
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1" />
</set>
//dialog_center_out
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200">
<scale
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0"
android:toYScale="0" />
</set>
//调用
public class ScreenUtil {
//px转dp
public static int dp2px(Context context, float dp) {
return (int) (dp * context.getResources().getDisplayMetrics().density + 0.5f);
}
//获取屏幕的宽度
public static int getScreenWidth(Context context) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
return display.getWidth();
}
}
class MainActivity : AppCompatActivity() {
private val mUrl: String = "https://productimg.xbiao.com/25/600_900/4366916153456432.jpg"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Dialog全屏,去掉状态栏的方式 https://www.jb51.net/article/183394.htm
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN)//全屏
val dialog = CustomDialog(this)
dialog.window?.setGravity(Gravity.END)
// dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) //去掉dialog蒙层
dialog.window?.setDimAmount(0f)//核心代码 解决了无法去除遮罩问题
dialog.window?.setDimAmount(0.4f)
dialog.window?.setLayout(
ScreenUtil.getScreenWidth(this) - ScreenUtil.dp2px(
this,
100F
), ViewGroup.LayoutParams.MATCH_PARENT
)
bt_top_dialog.setOnClickListener {
dialog.window?.setWindowAnimations(R.style.AnimTop)
dialog.show()
}
bt_bottom_dialog.setOnClickListener {
dialog.window?.setWindowAnimations(R.style.AnimBottom)
dialog.show()
}
bt_center_dialog.setOnClickListener {
dialog.window?.setWindowAnimations(R.style.AnimCenter)
dialog.show()
}
bt_left_dialog.setOnClickListener {
dialog.window?.setWindowAnimations(R.style.AnimLeft)
dialog.show()
}
bt_right_dialog.setOnClickListener {
dialog.window?.setWindowAnimations(R.style.AnimRight)
dialog.show()
}
dialog.setOnCancelListener {
Toast.makeText(this, "点外面,干哈呀", Toast.LENGTH_SHORT).show()
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/const_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<Button
android:id="@+id/bt_right_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dialog右侧弹出"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/bt_left_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dialog左侧弹出"
app:layout_constraintStart_toEndOf="@+id/bt_right_dialog"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/bt_top_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dialog上面弹出"
app:layout_constraintStart_toEndOf="@+id/bt_left_dialog"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/bt_bottom_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:text="Dialog下面弹出"
app:layout_constraintTop_toBottomOf="@+id/bt_right_dialog"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/bt_center_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:text="Dialog中间弹出"
app:layout_constraintTop_toBottomOf="@+id/bt_right_dialog"
app:layout_constraintStart_toEndOf="@+id/bt_bottom_dialog"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>