一、先看看效果图
实现半屏弹窗,背景半透明,可以使用Dialog或Activity来实现。
二、动画效果
实现从下往上弹出,从上往下关闭。
从下往上弹出:anim_slide_in_bottom.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="300"
android:fromXDelta="0%"
android:toXDelta="0%"
android:fromYDelta="100%"
android:toYDelta="0%"/>
set>
从上往下弹出:anim_slide_out_bottom.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="300"
android:fromXDelta="0%"
android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="100%"/>
set>
无动画效果no_anim.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:interpolator="@android:anim/decelerate_interpolator"
android:fromXDelta="0%"
android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="0%"/>
set>
三、Dialog实现半屏弹窗
<resources>
<style name="bottom_dialog_bg_style" parent="@android:style/Theme.Dialog">
- "android:windowNoTitle"
>true
- "android:background">@android:color/transparent
- "android:windowBackground">@android:color/transparent
- "android:windowFrame">@null
- "android:windowIsFloating">true
- "android:windowIsTranslucent">true
- "android:backgroundDimEnabled">true
- "android:backgroundDimAmount">0.5
style>
<style name="show_dialog_animStyle">
- "android:windowEnterAnimation"
>@anim/anim_slide_in_bottom
- "android:windowExitAnimation">@anim/anim_slide_out_bottom
style>
resources>
bottom_dialog_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@drawable/corner_background">
LinearLayout>
RelativeLayout>
corner_background.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<padding android:top="1dp" />
<corners android:topLeftRadius="6dp"
android:topRightRadius="6dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"/>
<gradient android:angle="90" android:startColor="@android:color/white" android:endColor="@android:color/white" />
shape>
item>
layer-list>
CustomBottomDialog
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
public class CustomBottomDialog extends Dialog {
public CustomBottomDialog(@NonNull Context context) {
super(context, R.style.bottom_dialog_bg_style);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_dialog_layout);
setWindowTheme();
setCancelable(true);
setCanceledOnTouchOutside(true);
}
private void setWindowTheme() {
Window window = this.getWindow();
// 设置弹出位置
window.setGravity(Gravity.BOTTOM);
// 设置弹出动画
window.setWindowAnimations(R.style.show_dialog_animStyle);
// 设置对话框大小
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
三、Activity半屏弹窗
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_dialog_layout);
overridePendingTransition(R.anim.anim_slide_in_bottom, R.anim.no_anim);
}
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.no_anim, R.anim.anim_slide_out_bottom);
}
}
<style name="bottom_activity_bg_style" parent="Theme.AppCompat.Light.NoActionBar">
- "android:windowNoTitle"
>true
- "android:background">@android:color/transparent
- "android:windowBackground">@android:color/transparent
- "android:windowFrame">@null
- "android:windowIsFloating">false
- "android:windowIsTranslucent">true
- "android:backgroundDimEnabled">true
- "android:backgroundDimAmount">0.5
style>
Activity的方式和Dialog的方式类似,只是需要修改一下动画添加方式和style。另外效果也有不同,如果需要做成dialog类型,设置android:windowIsFloating是关键。
两种添加进入和退出动画的方式:
1、style方式
- @anim/anim_slide_in_bottom
- @anim/anim_slide_out_bottom
2、在activity当中添加overridePendingTransition
3、有些情况下,把module做成aar插件时,通过overridePendingTransition,使用R.anim.anim_slide_in_bottom方式读取不到anim资源。
try {
int anim_slide_in_bottom= getApplication().getResources().getIdentifier("anim_slide_in_bottom", "anim", getApplication().getPackageName());
int no_anim= getApplication().getResources().getIdentifier("no_anim", "anim", getApplication().getPackageName());
overridePendingTransition(anim_slide_in_bottom, no_anim);
} catch (Exception e) {
e.printStackTrace();
}
作者:沧水巫云
博客:http://blog.csdn.NET/amir_zt/
以上原创,转载请注明出处,谢谢。
https://blog.csdn.net/amir_zt/article/details/114922218