Android5.0 Activity的跳轉動畫
一、首先介紹5.0以下的跳轉動畫,在res下面建立“anim”的包,建立兩個xml檔案,分别命名為screen_zoom_in.xml和screen_zoom_out.xml,這是由大變小的動畫。
screen_zoom_in.xml
android:duration="300"
>
android:fromXScale="1.4"
android:fromYScale="1.4"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="40%p"
android:pivotY="40%p"
/>
android:fromAlpha="0"
android:toAlpha="1"
/>
screen_zoom_out.xml
android:duration="300"
>
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.4"
android:toYScale="1.4"
android:pivotX="40%p"
android:pivotY="40%p"
/>
android:fromAlpha="0"
android:toAlpha="1"
/>
跳轉代碼為:
Intent intent1 = new Intent(this, CommonActivity.class);
startActivity(intent1);
overridePendingTransition(R.anim.screen_zoom_in,R.anim.screen_zoom_out);
如下圖所示:
二、Android5.0以上的圓形動畫,采用編碼的形式。
secondView.setVisibility(View.VISIBLE);
// Activity設定自定義 Shared Element切換動畫
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int centerX = (v.getLeft() + v.getRight()) / 2;
int centerY = (v.getTop() + v.getBottom()) / 2;
float finalRadius = (float) Math.hypot((double) centerX, (double) centerY);
Animator mCircularReveal = ViewAnimationUtils.createCircularReveal(
secondView, centerX, centerY, 0, finalRadius);
mCircularReveal.setDuration(400).start();
}
###如下圖所示:
三、Android5.0以上的爆炸效果,首先,要在res下的建立transition檔案夾,注意名字一定是transition,不能放在anim包下,建立一個explode.xml檔案。
android:duration="300"
>
跳轉代碼。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Transition explode = TransitionInflater.from(this).inflateTransition(R.transition.explode);
//退出時使用
getWindow().setExitTransition(explode);
//第一次進入時使用
getWindow().setEnterTransition(explode);
//再次進入時使用
getWindow().setReenterTransition(explode);
}
Intent intent2 = new Intent(this, CommonActivity.class);
startActivity(intent2, ActivityOptionsCompat.makeSceneTransitionAnimation(this).toBundle());
如下圖所示:
四、Android5.0以上的淡化效果,首先,要在res下的建立transition檔案夾,注意名字一定是transition,不能放在anim包下,建立一個fade.xml檔案。
android:duration="300"
>
跳轉代碼
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Transition fade = TransitionInflater.from(this).inflateTransition(R.transition.fade);
//退出時使用
getWindow().setExitTransition(fade);
//第一次進入時使用
getWindow().setEnterTransition(fade);
//再次進入時使用
getWindow().setReenterTransition(fade);
}
Intent intent = new Intent(this, CommonActivity.class);
startActivity(intent, ActivityOptionsCompat.makeSceneTransitionAnimation(this).toBundle());
效果如圖:
五、Android5.0的元素共享效果。
5.1 共享一個資源效果,在第一個activity的布局檔案中,元素的 android:transitionName="sharedBlue",要跟下一個跳轉的activity布局中共享元素的名字一樣。
android:id="@+id/share_blue"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:background="#6699ff"
android:transitionName="sharedBlue"
android:layout_gravity="right"
/>
android:id="@+id/share_yellow"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:background="#ffff00"
android:transitionName="sharedYellow"
/>
####下一個activity布局中元素
android:id="@+id/share_blue"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="#6699ff"
android:transitionName="sharedBlue"
android:layout_alignParentBottom="true"
/>
android:id="@+id/share_yellow"
android:layout_alignParentRight="true"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="#ffff00"
android:transitionName="sharedYellow"
/>
跳轉代碼
Intent intent1 = new Intent(ShareActivity.this, BlueActivity.class);
startActivity(intent1, ActivityOptionsCompat.makeSceneTransitionAnimation(ShareActivity.this, share_blue, "sharedBlue").toBundle());
動畫效果
5.2 Android5.0共享兩個元素,布局參考5.1,跳轉代碼:
Pair first = new Pair<>(share_blue, ViewCompat.getTransitionName(share_blue));
Pair second = new Pair<>(share_yellow, ViewCompat.getTransitionName(share_yellow));
ActivityOptionsCompat transitionActivityOptions =
ActivityOptionsCompat.makeSceneTransitionAnimation(ShareActivity.this, first, second);
Intent intent2 = new Intent(ShareActivity.this, BlueActivity.class);
ActivityCompat.startActivity(ShareActivity.this,
intent2, transitionActivityOptions.toBundle());
動畫效果:
5.3 Android5.0自定義共享元素的動畫效果,首先寫一個自定義動畫的Java檔案,CustomChangeBounds.java,
public class CustomChangeBounds extends ChangeBounds {
@Override
public Animator createAnimator(final ViewGroup sceneRoot,
TransitionValues startValues,
final TransitionValues endValues) {
Animator changeBounds = super.createAnimator(sceneRoot, startValues, endValues);
if (startValues == null || endValues == null || changeBounds == null)
return null;
changeBounds.setDuration(300);
changeBounds.setInterpolator(AnimationUtils.loadInterpolator(sceneRoot.getContext(),
android.R.interpolator.fast_out_slow_in));
return changeBounds;
}
}
在第二個activity中,
//布局
android:id="@+id/share_blue"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="10dp"
android:background="#6699ff"
android:transitionName="transition_morph_view"
android:layout_gravity="right"
android:layout_centerInParent="true"
/>
//代碼
share_blue = findViewById(R.id.share_blue);
// Activity設定自定義 Shared Element切換動畫
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//定義ArcMotion
ArcMotion arcMotion = new ArcMotion();
arcMotion.setMinimumHorizontalAngle(50f);
arcMotion.setMinimumVerticalAngle(50f);
//插值器,控制速度
Interpolator interpolator = AnimationUtils.loadInterpolator(this, android.R.interpolator.fast_out_slow_in);
//執行個體化自定義的ChangeBounds
CustomChangeBounds changeBounds = new CustomChangeBounds();
changeBounds.setPathMotion(arcMotion);
changeBounds.setInterpolator(interpolator);
changeBounds.addTarget(share_blue);
//将切換動畫應用到目前的Activity的進入和傳回
getWindow().setSharedElementEnterTransition(changeBounds);
getWindow().setSharedElementReturnTransition(changeBounds);
}
第一個activity的跳轉代碼
Intent intent = new Intent(this, CustomShareActivity.class);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation
(ShareActivity.this, share_blue, "transition_morph_view");
startActivity(intent, options.toBundle());
動畫效果