1.在res下建立anim資料夾
2.在anim建立left_1.xml, left_2.xml, right_1.xml, right_2.xml, up_1.xml, down_1.xml檔案
分別為向左移動,向右移動,向上移動與向下移動
3.left_1.xml
left_2.xml
right_1.xml
right_2.xml
up_1.xml
down_1.xml
4.Dialog需要在value/styles.xml加入
5.3個layout頁面
res/layout/activity_main.xml
res/layout/activity_next.xml
res/layout/dialog_layout.xml
6.MainActivity.java
package com.terryyamg.activityanimationtest; import android.app.Dialog; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnNext = (Button) findViewById(R.id.btnNext); Button btnDialog = (Button) findViewById(R.id.btnDialog); btnNext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, NextActivity.class); startActivity(intent); overridePendingTransition(R.anim.right_1, R.anim.right_2); } }); btnDialog.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final Dialog dialog = new Dialog(MainActivity.this); dialog.setContentView(R.layout.dialog_layout); dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; Button btnClose = (Button) dialog.findViewById(R.id.btnClose); btnClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); dialog.show(); } }); } }NextActivity.java
package com.terryyamg.activityanimationtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; public class NextActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_next); } @Override public void onBackPressed() { super.onBackPressed(); overridePendingTransition(R.anim.left_1, R.anim.left_2); } }
檔案下載:
https://github.com/terryyamg/ActivityAnimationTest
參考來源:
http://stackoverflow.com/questions/26431017/how-to-achieve-right-to-left-animation-to-start-the-activity
http://android-er.blogspot.tw/2012/07/dialog-animation-using-windowanimations.html
thank you so much for writing this great blog here for us.
回覆刪除vegus168