关于Transition兼容问题 Transition原作者的兼容库
一、列表界面:
Intent intent = new Intent(mContext, LiveDetailAct.class);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) mContext, sdCover, getString(R.string.image_transition_name));
startActivityForResult(intent, 0, options.toBundle());
sdCover: 列表界面所要共享图片的View
R.string.image_transition_name:自定义的一个string字端在value/string下面,下面有用处。
二、详情界面
注意: android:transitionName 保持一致
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sd_cover"
android:layout_width="match_parent"
android:layout_height="210dp"
android:transitionName="@string/image_transition_name"
fresco:actualImageScaleType="centerCrop" />
三、完成
效果图:
四、具体开发中:Bug:
我用的Fresco有一个问题:解决图片闪动问题
在跳转的第二个界面onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().setSharedElementEnterTransition(DraweeTransition.createTransitionSet(ScalingUtils.ScaleType.CENTER_CROP, ScalingUtils.ScaleType.CENTER_CROP));
getWindow().setSharedElementReturnTransition(DraweeTransition.createTransitionSet(ScalingUtils.ScaleType.FIT_CENTER, ScalingUtils.ScaleType.CENTER_CROP));
}
拓展1:
transition共享view元素动画结束后,如何实现关闭启动界面。这里需要通过一个监听器。
if (ApiLevelHelper.isAtLeast(Build.VERSION_CODES.LOLLIPOP)) {
//getSharedElementExitTransition 在转移到启动的活动窗口之前,返回启动窗口中用于共享元素的转换
activity.getWindow().getSharedElementExitTransition().addListener(
new TransitionListenerAdapter() {
@Override
public void onTransitionEnd(Transition transition) {
activity.finish();
}
});
//代码中启动页设置共享的标签
final Pair[] pairs = TransitionHelper.createSafeTransitionParticipants(activity, true,
new Pair<>(v, activity.getString(R.string.transition_avatar)));
@SuppressWarnings("unchecked")
ActivityOptionsCompat activityOptions = ActivityOptionsCompat
.makeSceneTransitionAnimation(activity, pairs);
Intent starter = new Intent(context, CategorySelectionActivity.class);
ActivityCompat.startActivity(activity, starter, options.toBundle());
}
拓展2:
共享View启动页面,一些动画处理开始时机。
ActivityCompat.setEnterSharedElementCallback(this,
new TextSharedElementCallback(categoryNameTextSize, paddingStart) {
@Override
public void onSharedElementStart(List<String> sharedElementNames,
List<View> sharedElements,
List<View> sharedElementSnapshots) {
super.onSharedElementStart(sharedElementNames,
sharedElements,
sharedElementSnapshots);
//初始化
mToolbarBack.setScaleX(0f);
mToolbarBack.setScaleY(0f);
}
@Override
public void onSharedElementEnd(List<String> sharedElementNames,
List<View> sharedElements,
List<View> sharedElementSnapshots) {
super.onSharedElementEnd(sharedElementNames,
sharedElements,
sharedElementSnapshots);
// Make sure to perform this animation after the transition has ended.
ViewCompat.animate(mToolbarBack)
.setStartDelay(startDelay)
.scaleX(1f)
.scaleY(1f)
.alpha(1f);
}
});