下面列出了怎么用android.app.SharedElementCallback的API类实例代码及写法,或者点击链接到github查看源代码。
private void setupTransitions() {
// grab the position that the search icon transitions in *from*
// & use it to configure the return transition
setEnterSharedElementCallback(new SharedElementCallback() {
@Override
public void onSharedElementStart(
List<String> sharedElementNames,
List<View> sharedElements,
List<View> sharedElementSnapshots) {
if (sharedElements != null && !sharedElements.isEmpty()) {
View searchIcon = sharedElements.get(0);
if (searchIcon.getId() != R.id.searchback) return;
int centerX = (searchIcon.getLeft() + searchIcon.getRight()) / 2;
CircularReveal hideResults = (CircularReveal) TransitionUtils.findTransition(
(TransitionSet) getWindow().getReturnTransition(),
CircularReveal.class, R.id.results_container);
if (hideResults != null) {
hideResults.setCenter(new Point(centerX, 0));
}
}
}
});
}
@Override
void handleElementOnClick(final View view) {
final int itemPosition = getRecyclerView().getChildAdapterPosition(view);
VideoOnDemand item = getElements().get(itemPosition);
if (activity instanceof VODActivity) {
((VODActivity) activity).startNewVOD(item);
} else {
Intent intent = VODActivity.createVODIntent(item, getContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
intent.putExtra(getContext().getString(R.string.stream_preview_url), item.getMediumPreview());
intent.putExtra(getContext().getString(R.string.stream_preview_alpha), hasVodBeenWatched(item.getVideoId()) ? VOD_WATCHED_IMAGE_ALPHA : 1.0f);
final View sharedView = view.findViewById(R.id.image_stream_preview);
sharedView.setTransitionName(getContext().getString(R.string.stream_preview_transition));
final ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(activity, sharedView, getContext().getString(R.string.stream_preview_transition));
activity.setExitSharedElementCallback(new SharedElementCallback() {
@SuppressLint("NewApi")
@Override
public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots);
sharedView.animate().alpha(VOD_WATCHED_IMAGE_ALPHA).setDuration(300).start();
activity.setExitSharedElementCallback(null);
}
});
activity.startActivity(intent, options.toBundle());
} else {
getContext().startActivity(intent);
activity.overridePendingTransition(R.anim.slide_in_bottom_anim, R.anim.fade_out_semi_anim);
}
}
}
@Override
protected void initData() {
bg.setBackgroundColor(Color.TRANSPARENT);
dataList = getIntent().getStringArrayListExtra(Constant.DATA);
position = getIntent().getIntExtra(Constant.POSITION, 0);
flag = getIntent().getIntExtra(Constant.FLAG, 0);
mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
list = new ArrayList<>();
for (String item :
dataList) {
list.add(ImagePreViewFragment.newInstance(item));
}
mViewPagerAdapter.setTitleAndFragments(null, list);
display.setAdapter(mViewPagerAdapter);
display.setCurrentItem(position);
//这个可以看做个管道 每次进入和退出的时候都会进行调用 进入的时候获取到前面传来的共享元素的信息
//退出的时候 把这些信息传递给前面的activity
//同时向sharedElements里面put view,跟对view添加transitionname作用一样
supportPostponeEnterTransition();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setEnterSharedElementCallback(new SharedElementCallback() {
@Override
public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
sharedElements.clear();
sharedElements.put(dataList.get(display.getCurrentItem()), ((ImagePreViewFragment) list.get(display.getCurrentItem())).getSharedElement());
}
});
}
}
private void initSharedElement() {
supportPostponeEnterTransition();
ViewCompat.setTransitionName(headerView, "header");
headerAvatar.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
headerAvatar.getViewTreeObserver().removeOnPreDrawListener(this);
supportStartPostponedEnterTransition();
return true;
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setExitSharedElementCallback(new SharedElementCallback() {
@Override
public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
View view = null;
if (headerDisplay != null) {
view = headerDisplay.getLayoutManager().findViewByPosition(index);
}
if (view != null) {
sharedElements.clear();
sharedElements.put(((ImageShareInfoHolder.ImageShareAdapter) headerDisplay.getAdapter())
.getData(index), view);
index = -1;
}
}
});
}
presenter.registerEvent(PhotoPreEvent.class, photoPreEvent -> {
if (photoPreEvent.getFlag() == ConstantUtil.COMMENT_LIST_FLAG) {
index = photoPreEvent.getIndex();
}
});
}
@Override
void handleElementOnClick(final View view) {
final int itemPosition = getRecyclerView().getChildAdapterPosition(view);
VideoOnDemand item = getElements().get(itemPosition);
if (activity instanceof VODActivity) {
((VODActivity) activity).startNewVOD(item);
} else {
Intent intent = VODActivity.createVODIntent(item, getContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
intent.putExtra(getContext().getString(R.string.stream_preview_url), item.getMediumPreview());
intent.putExtra(getContext().getString(R.string.stream_preview_alpha), hasVodBeenWatched(item.getVideoId()) ? VOD_WATCHED_IMAGE_ALPHA : 1.0f);
final View sharedView = view.findViewById(R.id.image_stream_preview);
sharedView.setTransitionName(getContext().getString(R.string.stream_preview_transition));
final ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(activity, sharedView, getContext().getString(R.string.stream_preview_transition));
activity.setExitSharedElementCallback(new SharedElementCallback() {
@SuppressLint("NewApi")
@Override
public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots);
sharedView.animate().alpha(VOD_WATCHED_IMAGE_ALPHA).setDuration(300).start();
activity.setExitSharedElementCallback(null);
}
});
activity.startActivity(intent, options.toBundle());
} else {
getContext().startActivity(intent);
activity.overridePendingTransition(R.anim.slide_in_bottom_anim, R.anim.fade_out_semi_anim);
}
}
}
public static SharedElementCallback createSharedElementReenterCallback(
@NonNull Context context) {
final String shotTransitionName = context.getString(R.string.transition_shot);
final String shotBackgroundTransitionName =
context.getString(R.string.transition_shot_background);
return new SharedElementCallback() {
/**
* We're performing a slightly unusual shared element transition i.e. from one view
* (image in the grid) to two views (the image & also the background of the details
* view, to produce the expand effect). After changing orientation, the transition
* system seems unable to map both shared elements (only seems to map the shot, not
* the background) so in this situation we manually map the background to the
* same view.
*/
@Override
public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
if (sharedElements.size() != names.size()) {
// couldn't map all shared elements
final View sharedShot = sharedElements.get(shotTransitionName);
if (sharedShot != null) {
// has shot so add shot background, mapped to same view
sharedElements.put(shotBackgroundTransitionName, sharedShot);
}
}
}
};
}
static void clearSharedElementsOnReturn(@NonNull final BaseActivity activity) {
activity.setEnterSharedElementCallback(new SharedElementCallback() {
@Override
public void onMapSharedElements(final List<String> names,
final Map<String, View> sharedElements) {
super.onMapSharedElements(names, sharedElements);
if (activity.isFinishingAfterTransition()) {
names.clear();
sharedElements.clear();
}
}
});
}
private static SharedElementCallback createCallback(SharedElementCallback21 callback) {
SharedElementCallback newListener = null;
if (callback != null) {
newListener = new SharedElementCallbackImpl(callback);
}
return newListener;
}
@Override
void handleElementOnClick(final View view) {
int itemPosition = getRecyclerView().getChildAdapterPosition(view);
final ChannelInfo item = getElements().get(itemPosition);
final StreamerInfoViewHolder vh = (StreamerInfoViewHolder) getRecyclerView().getChildViewHolder(view);
final PreviewTarget previewTarget = getTargets().get(vh.getTargetsKey());
// Create intent for opening StreamerInfo activity. Send the StreamerInfo object with
// the intent, and flag it to make sure it creates a new task on the history stack
final Intent intent = new Intent(getContext(), ChannelActivity.class);
intent.putExtra(getContext().getResources().getString(R.string.channel_info_intent_object), item);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View sharedView = view.findViewById(R.id.profileLogoImageView);
sharedView.setTransitionName(getContext().getString(R.string.streamerInfo_transition));
final ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(activity, sharedView, getContext().getString(R.string.streamerInfo_transition));
activity.setExitSharedElementCallback(new SharedElementCallback() {
@SuppressLint("NewApi")
@Override
public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots);
if (!sharedElements.isEmpty() && sharedElements.get(0) != null && previewTarget != null) {
View element = sharedElements.get(0);
Animation anim = new RoundImageAnimation(element.getWidth() / 2, 0, (ImageView) element, previewTarget.getPreview());
anim.setDuration(200);
anim.setInterpolator(new DecelerateInterpolator());
view.startAnimation(anim);
}
activity.setExitSharedElementCallback(null);
}
});
activity.startActivity(intent, options.toBundle());
} else {
getContext().startActivity(intent);
if (activity != null) {
activity.overridePendingTransition(R.anim.slide_in_right_anim, R.anim.fade_out_semi_anim);
}
}
}
private static SharedElementCallback createCallback(SharedElementCallback21 callback) {
if (callback != null) {
return new SharedElementCallbackImpl(callback);
}
return null;
}
@Override
void handleElementOnClick(final View view) {
int itemPosition = getRecyclerView().getChildAdapterPosition(view);
final ChannelInfo item = getElements().get(itemPosition);
final StreamerInfoViewHolder vh = (StreamerInfoViewHolder) getRecyclerView().getChildViewHolder(view);
final PreviewTarget previewTarget = getTargets().get(vh.getTargetsKey());
// Create intent for opening StreamerInfo activity. Send the StreamerInfo object with
// the intent, and flag it to make sure it creates a new task on the history stack
final Intent intent = new Intent(getContext(), ChannelActivity.class);
intent.putExtra(getContext().getResources().getString(R.string.channel_info_intent_object), item);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View sharedView = view.findViewById(R.id.profileLogoImageView);
sharedView.setTransitionName(getContext().getString(R.string.streamerInfo_transition));
final ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(activity, sharedView, getContext().getString(R.string.streamerInfo_transition));
activity.setExitSharedElementCallback(new SharedElementCallback() {
@SuppressLint("NewApi")
@Override
public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots);
if (!sharedElements.isEmpty() && sharedElements.get(0) != null && previewTarget != null) {
View element = sharedElements.get(0);
Animation anim = new RoundImageAnimation(element.getWidth()/2, 0, (ImageView) element, previewTarget.getPreview());
anim.setDuration(200);
anim.setInterpolator(new DecelerateInterpolator());
view.startAnimation(anim);
}
activity.setExitSharedElementCallback(null);
}
});
activity.startActivity(intent, options.toBundle());
} else {
getContext().startActivity(intent);
if(activity != null) {
activity.overridePendingTransition(R.anim.slide_in_right_anim, R.anim.fade_out_semi_anim);
}
}
}
@NonNull
private DesignerNewsStoryHolder createDesignerNewsStoryHolder(ViewGroup parent) {
final DesignerNewsStoryHolder holder = new DesignerNewsStoryHolder(layoutInflater.inflate(
R.layout.designer_news_story_item, parent, false), pocketIsInstalled);
holder.itemView.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
final Story story = (Story) getItem(holder.getAdapterPosition());
CustomTabActivityHelper.openCustomTab(host,
DesignerNewsStory.getCustomTabIntent(host, story, null).build(),
Uri.parse(story.url));
}
}
);
holder.comments.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View commentsView) {
final Intent intent = new Intent();
intent.setClass(host, DesignerNewsStory.class);
intent.putExtra(DesignerNewsStory.EXTRA_STORY,
(Story) getItem(holder.getAdapterPosition()));
ReflowText.addExtras(intent, new ReflowText.ReflowableTextView(holder.title));
setGridItemContentTransitions(holder.itemView);
// on return, fade the pocket & comments buttons in
host.setExitSharedElementCallback(new SharedElementCallback() {
@Override
public void onSharedElementStart(List<String> sharedElementNames, List<View>
sharedElements, List<View> sharedElementSnapshots) {
host.setExitSharedElementCallback(null);
notifyItemChanged(holder.getAdapterPosition(),
HomeGridItemAnimator.STORY_COMMENTS_RETURN);
}
});
final ActivityOptions options =
ActivityOptions.makeSceneTransitionAnimation(host,
Pair.create((View) holder.title,
host.getString(R.string.transition_story_title)),
Pair.create(holder.itemView,
host.getString(R.string.transition_story_title_background)),
Pair.create(holder.itemView,
host.getString(R.string.transition_story_background)));
host.startActivity(intent, options.toBundle());
}
});
if (pocketIsInstalled) {
holder.pocket.setImageAlpha(178); // grumble... no xml setter, grumble...
holder.pocket.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
PocketUtils.addToPocket(host,
((Story) getItem(holder.getAdapterPosition())).url);
// notify changed with a payload asking RV to run the anim
notifyItemChanged(holder.getAdapterPosition(),
HomeGridItemAnimator.ADD_TO_POCKET);
}
});
}
return holder;
}
/**
* Returns the callback for the shared element transition.
*
* @return The callback for the shared element transition.
*/
public @Nullable SharedElementCallback getSharedElementCallback() {
return mSharedElementCallback;
}
/**
* Sets the callback for the shared element transition.
*
* @param sharedElementCallback The callback for the shared element transition.
*/
public void setSharedElementCallback(@Nullable SharedElementCallback sharedElementCallback) {
this.mSharedElementCallback = sharedElementCallback;
}