不同Activity之间的动画切换
一个页面对应一个Activity,页面之间的跳转最常见的是从上下左右四个方向,但是这是如何实现的呢? 实际上很简单。
创建两个Activity, MainActivity和SecondActivity:
MainActivity:
public class MainActivity extends Activity implements OnClickListener { private Button btn_next; private long mExitMillTime = 0L; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_next = (Button) findViewById(R.id.btn_next_page); btn_next.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_next_page: Intent it = new Intent(MainActivity.this, SecondActivity.class); startActivity(it); overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out); break; default: break; } } /*@Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK) { if (System.currentTimeMillis() - mExitMillTime > 2000) { Toast.makeText(this, "再点击一次退出程序", Toast.LENGTH_SHORT).show(); mExitMillTime = System.currentTimeMillis(); } else { MainActivity.this.finish(); } return true; } return super.onKeyDown(keyCode, event); }*/ }SecondActivity代码如下:
public class SecondActivity extends Activity implements OnClickListener{ private Button btnReturn; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second); btnReturn=(Button) findViewById(R.id.btn_return_homepage); btnReturn.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_return_homepage: Intent it = new Intent(SecondActivity.this, MainActivity.class); startActivity(it); overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out); break; default: break; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub // 点击返回键时,同样以动画返回 Intent it = new Intent(SecondActivity.this, MainActivity.class); startActivity(it); overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out); return super.onKeyDown(keyCode, event); } }
要想在Activity之间进行动画切换,主要是方法overridePendingTransition(R.anim.push_right_in,R.anim.push_right_out);
至于anim文件夹下的push_left_in.xml等四个文件就不再贴出来了,有兴趣的可以看一下简单的demo
下载地址:点击打开链接
posted on 2014-06-10 00:10 jinfenglee 阅读(167) 评论(0) 编辑 收藏 举报