先看效果
dialog.gif
使用方法:
AndroidStudio引入(https://jitpack.io/)
step1:Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
step2:Add the dependency
dependencies {
compile 'com.github.ithedan:BaseDialog:1.0'
}
Activity中使用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bottom = (Button) findViewById(R.id.bottom_top);
Button top = (Button) findViewById(R.id.top_bottom);
Button left = (Button) findViewById(R.id.left_right);
Button right = (Button) findViewById(R.id.right_left);
Button center = (Button) findViewById(R.id.center);
Button center1 = (Button) findViewById(R.id.center1);
bottom.setOnClickListener(this);
top.setOnClickListener(this);
left.setOnClickListener(this);
right.setOnClickListener(this);
center.setOnClickListener(this);
center1.setOnClickListener(this);
}
private void showDialog(int grary, int animationStyle) {
BaseDialog.Builder builder = new BaseDialog.Builder(this);
final BaseDialog dialog = builder.setViewId(R.layout.photo_choose_dialog)
.setPaddingdp(10, 0, 10, 0)//設(shè)置dialogpadding
.setGravity(grary)//設(shè)置顯示位置
.setAnimation(animationStyle)//設(shè)置動畫
.setWidthHeightpx(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)//設(shè)置dialog的寬高
.isOnTouchCanceled(true)//設(shè)置觸摸dialog外圍是否關(guān)閉dialog
.addViewOnClickListener(R.id.but_choose_one, new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"相冊", Toast.LENGTH_SHORT).show();
}
})//設(shè)置監(jiān)聽事件
.builder();
dialog.show();
Button button = dialog.getView(R.id.but_choose_three);//根據(jù)id獲取dialog中的恐懼
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.close();
}
});//關(guān)閉dialog
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bottom_top:
showDialog(Gravity.BOTTOM, R.style.Bottom_Top_aniamtion);
break;
case R.id.top_bottom:
showDialog(Gravity.TOP, R.style.Top_Bottom_aniamtion);
break;
case R.id.left_right:
showDialog(Gravity.CENTER, R.style.Left_Right_aniamtion);
break;
case R.id.right_left:
showDialog(Gravity.CENTER, R.style.Right_Left_aniamtion);
break;
case R.id.center:
showDialog(Gravity.CENTER, R.style.Alpah_aniamtion);
break;
case R.id.center1:
showDialog(Gravity.CENTER, R.style.Scale_aniamtion);
break;
}
}
在BaseDialog:1.0中默認定義了以上六種動畫效果,如果達不到項目要求可以根據(jù)自己的需求在style中自己定義,調(diào)用builder.setAnimation(int styleAnimation) 即可床蜘,dialog中默認的styles是:
<!--dialog style屬性-->
<style name="dialog_style" parent="@android:style/Theme.Dialog">
<!--//Dialog的windowFrame框為無-->
<item name="android:windowFrame">@null</item>
<!--//是否浮現(xiàn)在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--//是否半透明-->
<item name="android:windowIsTranslucent">true</item>
<!--//是否顯示title-->
<item name="android:windowNoTitle">true</item>
<!--//設(shè)置dialog的背景-->
<item name="android:background">@android:color/transparent</item>
<!--//顯示區(qū)域背景是否透明-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--//就是用來控制灰度的值泄私,當為1時抒抬,界面除了我們的dialog內(nèi)容是高亮顯示的恐似,dialog以外的區(qū)域是黑色的木缝,完全看不到其他內(nèi)容秃励,系統(tǒng)的默認值是0.5-->
<item name="android:backgroundDimAmount">0.5</item>
<!--//顯示區(qū)域以外是否使用黑色半透明背景-->
<item name="android:backgroundDimEnabled">true</item>
</style>