滑出animation不工作后按button?

我在进入或退出活动时使用animation,进入活动animation效果良好,但退出animation效果不佳。 我开始退出animation,当我按下返回button。发生什么事是它首先开始input当前活动的animation,然后显示最后一个活动,我想要一个简单的退出animation上后退button按下。

Slide_out.xml

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_longAnimTime" android:fromXDelta="0%p" android:toXDelta="-100%p" > </translate> 

Slide_in.xml

  <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_longAnimTime" android:fromXDelta="100%p" android:toXDelta="0%p" > </translate> 

按下动作栏上的后退button

 public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.home: finish(); overridePendingTransition(R.anim.slide_out, R.anim.slide_in); return true; default: break; } return super.onOptionsItemSelected(item); } 

Solutions Collecting From Web of "滑出animation不工作后按button?"

首先在animation文件夹中创build一个更多的animation即nothing.xml

nothing.xml

 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_longAnimTime" android:fromXDelta="0%" android:toXDelta="0%" > </translate> 

这里是你的slide_in.xml

 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_longAnimTime" android:fromXDelta="-100%" android:toXDelta="0%" > </translate> 

slide_out.xml

 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_longAnimTime" android:fromXDelta="0%" android:toXDelta="100%" > </translate> 

现在就像这样调用你的NewActivity

 startActivity(new Intent(CurrentActivity.this, NewActivity.class)); overridePendingTransition(R.anim.slide_in, R.anim.nothing); 

然后在你的后退button上点击做到这一点

 finish(); overridePendingTransition(R.anim.nothing, R.anim.slide_out); 

我用slide_in.xml

  <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_longAnimTime" android:fromXDelta="100%" android:toXDelta="0%" > </translate> 

slide_out.xml

  <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_longAnimTime" android:fromXDelta="0%" android:toXDelta="-100%" > </translate> 

slide_enter.xml

  <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_longAnimTime" android:fromXDelta="-100%" android:toXDelta="0%" > </translate> 

slide_exit.xml

  <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_longAnimTime" android:fromXDelta="0%" android:toXDelta="100%" > </translate> 

  Intent i=new Intent(Authentication.this,Login.class); startActivity(i); overridePendingTransition(R.anim.slide_in, R.anim.slide_out); 

到以前

  finish(); overridePendingTransition(R.anim.slide_enter, R.anim.slide_exit); 

onBackPressed添加animation,点击后退button将显示animation。

 @Override public void onBackPressed() { super.onBackPressed(); overridePendingTransition(R.anim.slide_in, R.anim.slide_out); } 

我已经做了类似的事情,它可以正常工作,你可以用startAnimation(your_xml)改变animate(),你也必须使活动背景透明:

 <item name="android:windowBackground">@android:color/transparent</item> 

此活动将滑下,并且由于透明背景,滑动时MainActivity将可见。 编辑 – 带有工具栏后退button:

 toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mainlinear.animate() .translationY(ScUtils.getScreenHeight(getApplicationContext())) .setDuration(210) .setInterpolator(new AccelerateInterpolator()) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { ThemeActivity.super.onBackPressed(); overridePendingTransition(0, 0); } }).start(); } });