Activity切换动画
动画执行优先级,系统动画<AppTheme<(overridePendingTransition\ActivityOptionsCompat))
1.overridePendingTransition()
下面放code:
R.anim.in_activity
R.anim.out_activity
R.anim.in_activity_close
R.anim.out_activity_close
资源文件放anim
in_activity
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="100%"
android:toYDelta="0%"
android:duration="300"
/>
</set>
out_activity
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="0%"
android:toYDelta="0%"
/>
</set>
in_activity_close
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0%"
android:toYDelta="0%"
android:duration="300"
/>
</set>
out_activity_close
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="0%"
android:toYDelta="100%"
/>
</set>
2.Theme方式
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowAnimationStyle">@style/myWindowAnim</item>
</style>
<style name="myWindowAnim">
<item name="android:activityOpenEnterAnimation">@anim/in_activity</item>
<item name="android:activityOpenExitAnimation">@anim/out_activity</item>
<item name="android:activityCloseEnterAnimation">@anim/in_activity_close</item>
<item name="android:activityCloseExitAnimation">@anim/out_activity_close</item>
</style>
主题添加 <item name="android:windowAnimationStyle">@style/myWindowAnim</item>
这样全局都有动画了,不用每个Activity单独设置了。
资源文件同上
ActivityOptionsCompat( )
有兼容包,当然5.0以下没有动画效果。
看API名字应该就能知道了
- ActivityOptionsCompat.makeCustomAnimation(Context context, int enterResId, int exitResId)【相当于overridePendingTransition】
- ActivityOptionsCompat.makeScaleUpAnimation(View source,int startX, int startY, int startWidth, int startHeight)
- ActivityOptionsCompat.makeThumbnailScaleUpAnimation(View source,Bitmap thumbnail, int startX, int startY)
共享属性
- ActivityOptionsCompat.makeSceneTransitionAnimation(Activity activity, View sharedElement, String sharedElementName)
- ActivityOptionsCompat.makeSceneTransitionAnimation(Activity activity,Pair<View, String>… sharedElements)
// startActivity(new Intent(this, Main2Activity.class));
// overridePendingTransition(R.anim.in_activity, R.anim.out_activity);
// ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeCustomAnimation(this, R.anim
// .in_activity, R.anim.out_activity);
// ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeScaleUpAnimation(v,
// v.getWidth() , v.getHeight() , 0, 0);
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
// ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeThumbnailScaleUpAnimation(v,
// bitmap, v.getWidth(), v.getHeight());
ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(
this, v, "sharedElement");
ActivityCompat.startActivity(this, new Intent(this, B.class),
activityOptionsCompat.toBundle());
这里注意:ActivityCompat.startActivity()。
Android 5.0(API 级别 21)支持这些共享元素转换:
changeBounds - 为目标视图的布局边界的变化添加动画。
changeClipBounds - 为目标视图的裁剪边界的变化添加动画。
changeTransform - 为目标视图的缩放与旋转变化添加动画。
changeImageTransform - 为目标图像的大小与缩放变化添加动画。
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeBounds android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="500"/>
</transitionSet>
<style name="BaseAppTheme" parent="android:Theme.Material">
<!-- enable window content transitions -->
<item name="android:windowContentTransitions">true</item>
<!-- specify enter and exit transitions -->
<item name="android:windowEnterTransition">@transition/explode</item>
<item name="android:windowExitTransition">@transition/explode</item>
<!-- specify shared element transitions -->
<item name="android:windowSharedElementEnterTransition">
@transition/change_image_transform</item>
<item name="android:windowSharedElementExitTransition">
@transition/change_image_transform</item>
</style>
Fragment切换动画
资源文件放在animator
Fragment转场动画和Activity转场有点不同的是,Fragment只要在跳转页设置就行。
fragment_slide_left_enter
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:interpolator="@android:interpolator/accelerate_cubic"
android:valueFrom="1000" android:valueTo="0"
android:valueType="floatType"
android:propertyName="translationX"
android:duration="@android:integer/config_mediumAnimTime" />
<!--<objectAnimator-->
<!--android:interpolator="@android:interpolator/decelerate_quint"-->
<!--android:valueFrom="0.0" android:valueTo="1.0"-->
<!--android:valueType="floatType"-->
<!--android:propertyName="alpha"-->
<!--android:duration="@android:integer/config_mediumAnimTime" />-->
</set>
fragment_slide_left_exit
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:interpolator="@android:interpolator/accelerate_cubic"
android:valueFrom="0" android:valueTo="-1000"
android:valueType="floatType"
android:propertyName="translationX"
android:duration="@android:integer/config_mediumAnimTime" />
<!--<objectAnimator-->
<!--android:interpolator="@android:interpolator/decelerate_quint"-->
<!--android:valueFrom="1.0" android:valueTo="0.0"-->
<!--android:valueType="floatType"-->
<!--android:propertyName="alpha"-->
<!--android:duration="@android:integer/config_mediumAnimTime" />-->
</set>
fragment_slide_right_enter
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:interpolator="@android:interpolator/accelerate_cubic"
android:valueFrom="-1000" android:valueTo="0"
android:valueType="floatType"
android:propertyName="translationX"
android:duration="@android:integer/config_mediumAnimTime" />
<!--<objectAnimator-->
<!--android:interpolator="@android:interpolator/decelerate_quint"-->
<!--android:valueFrom="0.0" android:valueTo="1.0"-->
<!--android:valueType="floatType"-->
<!--android:propertyName="alpha"-->
<!--android:duration="@android:integer/config_mediumAnimTime" />-->
</set>
fragment_slide_right_exit
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:interpolator="@android:interpolator/accelerate_decelerate"
android:valueFrom="0" android:valueTo="1000"
android:valueType="floatType"
android:propertyName="translationX"
android:duration="@android:integer/config_mediumAnimTime" />
<!--<objectAnimator-->
<!--android:interpolator="@android:interpolator/decelerate_quint"-->
<!--android:valueFrom="1.0" android:valueTo="0.0"-->
<!--android:valueType="floatType"-->
<!--android:propertyName="alpha"-->
<!--android:duration="@android:integer/config_mediumAnimTime" />-->
</set>