常用頁面跳轉動畫
阿新 • • 發佈:2018-11-27
1、res目錄下新建anim資料夾,在anim資料夾下新建xml檔案
slide_right_in.xml(右邊進入動畫)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="100.0%p"
android:toXDelta="0.0" />
</set >
slide_right_out.xml(右邊退出動畫)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0.0"
android:toXDelta="100.0%p" />
</set>
2、程式碼:
//進入動畫
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//進入動畫
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeCustomAnimation(MainActivity.this,R.anim.slide_right_in,0);
ActivityCompat.startActivity(MainActivity.this, new Intent(MainActivity.this, TestActivity.class), optionsCompat.toBundle());
}
});
}
}
//退出動畫
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
@Override
public void onBackPressed() {
super.onBackPressed();
//退出動畫
overridePendingTransition(0, R.anim.slide_right_out);
}
}
也可以把進入、退出動畫寫在一起
public static void rightEnterAndRightExit(Context context, Intent intent) {
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeCustomAnimation(context,
R.anim.slide_right_in, R.anim.slide_right_out);
ActivityCompat.startActivity(context, intent, optionsCompat.toBundle());
}
====================================================
從左邊進入動畫slide_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="-100.0%p"
android:toXDelta="0.0" />
</set>
從左邊退出動畫slide_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0.0"
android:toXDelta="-100.0%p" />
</set>
====================================================
從底下進入動畫slide_bottom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:shareInterpolator="true">
<translate
android:duration="300"
android:fromYDelta="100%p"
android:toYDelta="0.0" />
</set>
從底下退出動畫slide_bottom_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:shareInterpolator="true">
<translate
android:duration="300"
android:fromYDelta="0.0"
android:toYDelta="100%p" />
</set>
====================================================
若要對所有Activity的頁面跳轉設定統一的動畫,只需使用MyTheme主題即可:
<!--統一頁面跳轉動畫-->
<style name="ActivitySwitchAnimation">
<item name="android:activityOpenEnterAnimation">@anim/slide_bottom_in</item>
<item name="android:activityOpenExitAnimation">@anim/slide_bottom_out</item>
<item name="android:activityCloseEnterAnimation">@anim/slide_bottom_in</item>
<item name="android:activityCloseExitAnimation">@anim/slide_bottom_out</item>
</style>
<style name="MyTheme" parent="AppTheme">
<item name="android:windowAnimationStyle">@style/ActivitySwitchAnimation</item>
</style>