I want an effect that pops up from the bottom. As shown below:

1. When you click the Show button, a Dialog dialog pops up slowly from the bottom.

2. When dialog is closed, the dialog slowly moves towards the bottom and disappears. Very smooth effect.

 

   

 

Implementation method:

Write the following code in the activity

public class MainActivity extends Activity {
	Button button1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button1 = (Button)findViewById(R.id.button1);
		button1.setOnClickListener(mOnClickListener);
	}

	OnClickListener mOnClickListener = new View.OnClickListener(){

		@Override
		public void onClick(View v) {
			//TODO Auto-generated method stub   
			AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)  
			        .setTitle("title").setMessage("message").create();  
			Window window = dialog.getWindow();  
			window.setGravity(Gravity.BOTTOM);  // The position of the dialog display can be set here
			window.setWindowAnimations(R.style.mystyle);  // Add animationdialog.show(); }}; }Copy the code

Prepare a style resource file and create myStyle

  <style name="mystyle" parent="android:Animation">
        <item name="@android:windowEnterAnimation">@anim/dialog_enter</item>
        <item name="@android:windowExitAnimation">@anim/dialog_exit</item>
</style>
Copy the code

There are two animations used in this MyStyle, as follows:

dialog_enter.xml

<? The XML version = "1.0" encoding = "utf-8"? > <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="600" android:fromYDelta="100%p" /> </set>Copy the code

dialog_exit.xml

<? The XML version = "1.0" encoding = "utf-8"? > <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="600" android:toYDelta="100%p" /> </set>Copy the code

This is done.

 

Code explanation:

1. Set an animation style style for the Windows on which the Dialog resides

2. This style specifies the animation for the form (Windows) to enter (dialog_enter) and the animation for the form to leave (dialog_exit)

3. In the dialog_Enter animation on entry, a translate change is written, specifying that it starts at 100% of its parent. 100% of the parent container location is off-screen location, is not visible, this is a starting point. No end point is specified, and the end point defaults to where it should be displayed (to be displayed).

4. Similarly, dialog_exit defines the exit animation. The animation does not specify the start position, but specifies that the end position is 100% of its parent container, which slowly fades down the screen.

Android :toYDelta="100%p" in 100% indicates the 100% location of its parent containerCopy the code

And if I write

Android :toYDelta="100%", which indicates 100% of its location.Copy the code

Reference:

www.linuxidc.com/Linux/2012-…

Blog.csdn.net/ztp800201/a…