Android 底部滑上滑下动画的Dialog及它的WindowManager.LayoutParams设置

1430人阅读 评论(0) 收藏 举报
分类:
/**   
 * @Description: 
 * @author stone
 * @email  aa86799@163.com
 */
public class  MyDialog extends Dialog {
	
	private Animation mShowAnim;
	private Animation mHideAnim;
	private View mMainLayout;

	/**
	 * @param context
	 */
	public  MyDialog(Context context) {
		super(context);

		createShowAnim();
		createHideAnim();
		
		//mMainLayout = ...
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		setContentView(mMainLayout);
	}
	
	private void createShowAnim() {
		mShowAnim = new TranslateAnimation(1, 0, 1, 0, 1, 1, 1, 0);
		mShowAnim.setDuration(500);
	}
	
	private void createHideAnim() {
		mHideAnim = new TranslateAnimation(1, 0, 1, 0, 1, 0, 1, 1);
		mHideAnim.setDuration(500);
	}

	@Override
	public void show() {
		super.show();
		mMainLayout.startAnimation(mShowAnim);
	}
	
	private void dismissDialog() {
		super.dismiss();
	}
	
	@Override
	public void dismiss() {
		mHideAnim.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				dismissDialog();
			}
		});
		mMainLayout.startAnimation(mHideAnim);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			dismiss();
		}
		return super.onKeyDown(keyCode, event);
	}
}

MyDialog myDialog = new MyDialog(context);
WindowManager.LayoutParams params = myDialog.getWindow().getAttributes();
params.alpha = 0.8f; //窗口透明度
params.width =  mScreenWidth - 80;
params.height =  mScreenHeight / 4;
params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
myDialog.getWindow().setAttributes(params);
//必须设置一个背景,否则会有系统的Dialog样式:外部白框
myDialog.getWindow().setBackgroundDrawableResource(R.drawable.translate);

设置的背景必须是Drawable对象或Drawable资源的id。

res/drawable/translate.xml

<?xml version="1.0" encoding="utf-8"?>
<color xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="#00000000">
</color>

----------------------------------------------------------------------------------------------------------------------------------------------------------------

刚一哥们跟我说,网上流行的是这样的实现方法:

mWindow.setWindowAnimations(R.style.dialog);  //添加动画  

res/values/styles.xml

<style name="dialog" parent="android:Animation">    
    <item name="@android:windowEnterAnimation">@anim/dialog_enter</item>  
    <item name="@android:windowExitAnimation">@anim/dialog_exit</item>  
</style> 

dialog_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="500"
        android:fromYDelta="100%p" />
</set>


dialog_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="500"
        android:toYDelta="100%p" />
</set>




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1286142次
    • 积分:13190
    • 等级:
    • 排名:第1119名
    • 原创:341篇
    • 转载:6篇
    • 译文:10篇
    • 评论:234条
    博客专栏
    最新评论
      QQ交流群: 473876194
      QQ交流群