androidx.core.app.SharedElementCallback Java Examples

The following examples show how to use androidx.core.app.SharedElementCallback. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GridFragment.java    From animation-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Prepares the shared element transition to the pager fragment, as well as the other transitions
 * that affect the flow.
 */
private void prepareTransitions() {
  setExitTransition(TransitionInflater.from(getContext())
      .inflateTransition(R.transition.grid_exit_transition));

  // A similar mapping is set at the ImagePagerFragment with a setEnterSharedElementCallback.
  setExitSharedElementCallback(
      new SharedElementCallback() {
        @Override
        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
          // Locate the ViewHolder for the clicked position.
          RecyclerView.ViewHolder selectedViewHolder = recyclerView
              .findViewHolderForAdapterPosition(MainActivity.currentPosition);
          if (selectedViewHolder == null) {
            return;
          }

          // Map the first shared element name to the child ImageView.
          sharedElements
              .put(names.get(0), selectedViewHolder.itemView.findViewById(R.id.card_image));
        }
      });
}
 
Example #2
Source File: SharedElementTransition.java    From Kore with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the transition for the exiting fragment
 * @param fragment
 */
@TargetApi(21)
public void setupExitTransition(Context context, Fragment fragment) {
    Transition fade = TransitionInflater
            .from(context)
            .inflateTransition(android.R.transition.fade);
    fragment.setExitTransition(fade);
    fragment.setReenterTransition(fade);

    fragment.setExitSharedElementCallback(new SharedElementCallback() {
        @Override
        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
            // Clearing must be done in the reentering fragment
            // as this is called last. Otherwise, the app will crash during transition setup. Not sure, but might
            // be a v4 support package bug.
            if (clearSharedElements) {
                names.clear();
                sharedElements.clear();
                clearSharedElements = false;
            }
        }
    });
}
 
Example #3
Source File: SharedElementLaunchedActivity.java    From PhotoDraweeView with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void initWindowTransitions() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        AutoTransition transition = new AutoTransition();
        getWindow().setSharedElementEnterTransition(transition);
        getWindow().setSharedElementExitTransition(transition);
        ActivityCompat.setEnterSharedElementCallback(this, new SharedElementCallback() {
            @Override public void onSharedElementEnd(List<String> sharedElementNames,
                    List<View> sharedElements, List<View> sharedElementSnapshots) {
                for (final View view : sharedElements) {
                    if (view instanceof PhotoDraweeView) {
                        ((PhotoDraweeView) view).setScale(1f, true);
                    }
                }
            }
        });
    }
}
 
Example #4
Source File: ImagePagerFragment.java    From animation-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Prepares the shared element transition from and back to the grid fragment.
 */
private void prepareSharedElementTransition() {
  Transition transition =
      TransitionInflater.from(getContext())
          .inflateTransition(R.transition.image_shared_element_transition);
  setSharedElementEnterTransition(transition);

  // A similar mapping is set at the GridFragment with a setExitSharedElementCallback.
  setEnterSharedElementCallback(
      new SharedElementCallback() {
        @Override
        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
          // Locate the image view at the primary fragment (the ImageFragment that is currently
          // visible). To locate the fragment, call instantiateItem with the selection position.
          // At this stage, the method will simply return the fragment at the position and will
          // not create a new one.
          Fragment currentFragment = (Fragment) viewPager.getAdapter()
              .instantiateItem(viewPager, MainActivity.currentPosition);
          View view = currentFragment.getView();
          if (view == null) {
            return;
          }

          // Map the first shared element name to the child ImageView.
          sharedElements.put(names.get(0), view.findViewById(R.id.image));
        }
      });
}
 
Example #5
Source File: BigImagePagerActivity.java    From CloudReader with Apache License 2.0 5 votes vote down vote up
private void setSharedElementCallback(final int exitIndex, final View sharedView) {
    setEnterSharedElementCallback(new SharedElementCallback() {
        @Override
        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
            String transitName = mImageUrls.get(exitIndex);
            names.clear();
            sharedElements.clear();
            names.add(transitName);
            sharedElements.put(transitName, sharedView);
        }
    });
}
 
Example #6
Source File: SharedElementTransition.java    From Kore with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up the transition for the entering fragment
 * @param fragmentTransaction
 * @param fragment entering fragment
 * @param sharedElement must have the transition name set
 */
@TargetApi(21)
public void setupEnterTransition(Context context,
                                 FragmentTransaction fragmentTransaction,
                                 final Fragment fragment,
                                 View sharedElement) {
    if (!(fragment instanceof SharedElement)) {
        LogUtils.LOGD(TAG, "Enter transition fragment must implement SharedElement interface");
        return;
    }

    androidx.core.app.SharedElementCallback seCallback = new androidx.core.app.SharedElementCallback() {
        @Override
        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
            // On returning, onMapSharedElements for the exiting fragment is called before the onMapSharedElements
            // for the reentering fragment. We use this to determine if we are returning and if
            // we should clear the shared element lists. Note that, clearing must be done in the reentering fragment
            // as this is called last. Otherwise, the app will crash during transition setup. Not sure, but might
            // be a v4 support package bug.
            if (fragment.isVisible() && (!((SharedElement) fragment).isSharedElementVisible())) {
                // shared element not visible
                clearSharedElements = true;
            }
        }
    };
    fragment.setEnterSharedElementCallback(seCallback);

    fragment.setEnterTransition(TransitionInflater
                                        .from(context)
                                        .inflateTransition(R.transition.media_details));
    fragment.setReturnTransition(null);

    Transition changeImageTransition = TransitionInflater.from(
            context).inflateTransition(R.transition.change_image);
    fragment.setSharedElementReturnTransition(changeImageTransition);
    fragment.setSharedElementEnterTransition(changeImageTransition);

    fragmentTransaction.addSharedElement(sharedElement, sharedElement.getTransitionName());
}
 
Example #7
Source File: PhotoListFragment.java    From NewFastFrame with Apache License 2.0 4 votes vote down vote up
@Override
protected void initData() {
    DaggerPhotoListComponent.builder().photoListModule(new PhotoListModule(this))
            .newsComponent(NewsApplication.getNewsComponent())
            .build().inject(this);
    display.setLayoutManager(new WrappedGridLayoutManager(getContext(), 2));
    display.addItemDecoration(new GridSpaceDecoration(2, DensityUtil.toDp(10), true));
    refresh.setOnRefreshListener(this);
    display.setLoadMoreFooterView(new LoadMoreFooterView(getContext()));
    display.setOnLoadMoreListener(this);
    photoListAdapter.setOnItemClickListener(new OnSimpleItemChildClickListener() {
        @Override
        public void onItemChildClick(int position, View view, int id) {
            List<PictureBean.PictureEntity> imageList = photoListAdapter.getData();
            if (imageList != null && imageList.size() > 0) {
                ArrayList<String> result = new ArrayList<>();
                for (PictureBean.PictureEntity item :
                        imageList) {
                    result.add(item.getUrl());
                }

                ImagePreViewActivity.start(getActivity(), result, position, view,NewsUtil.PHOTO_LIST_FLAG);
                //                    Map<String, Object> map = new HashMap<>();
                //                    map.put(Constant.POSITION, position);
                //                    map.put(Constant.VIEW, view);
                //                    Router.getInstance().deal(new RouterRequest.Builder()
                //                            .provideName("chat").actionName("preview")
                //                            .context(view.getContext())
                //                            .paramMap(map).object(result).build());
            }
        }
    });
    display.setAdapter(photoListAdapter);
    getActivity().setExitSharedElementCallback(new SharedElementCallback() {
        @Override
        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
            View itemView = display.getLayoutManager().findViewByPosition(index);
            if (itemView != null) {
                sharedElements.clear();
                sharedElements.put(photoListAdapter.getData(index).getUrl(), itemView.findViewById(R.id.iv_item_fragment_photo_list_picture));
                index=-1;
            }
        }
    });
    presenter.registerEvent(PhotoPreEvent.class, new Consumer<PhotoPreEvent>() {
        @Override
        public void accept(PhotoPreEvent photoPreEvent) throws Exception {
            if (photoPreEvent.getFlag() == NewsUtil.PHOTO_LIST_FLAG) {
                index = photoPreEvent.getIndex();
            }
        }
    });
}
 
Example #8
Source File: BigImagePagerActivity.java    From CloudReader with Apache License 2.0 4 votes vote down vote up
public static void startThis(final AppCompatActivity activity, List<View> imageViews, List<String> imageUrls, int enterIndex) {
    Intent intent = new Intent(activity, BigImagePagerActivity.class);
    intent.putStringArrayListExtra(KEY_IMAGE_URLS, (ArrayList<String>) imageUrls);
    intent.putExtra(KEY_ENTER_INDEX, enterIndex);

    ActivityOptionsCompat optionsCompat
            = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, imageViews.get(enterIndex), imageUrls.get(enterIndex));

    try {
        ActivityCompat.startActivity(activity, intent, optionsCompat.toBundle());
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        activity.startActivity(intent);
    }

    ActivityCompat.setExitSharedElementCallback(activity, new SharedElementCallback() {
        @Override
        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
            super.onMapSharedElements(names, sharedElements);
            /* 这个方法会调用两次,一次进入前,一次回来前。 */
            if (sExitIndex == null) {
                return;
            }

            int exitIndex = sExitIndex;
            sExitIndex = null;

            if (exitIndex != enterIndex
                    && imageViews.size() > exitIndex
                    && imageUrls.size() > exitIndex) {
                names.clear();
                sharedElements.clear();
                View view = imageViews.get(exitIndex);
                String transitName = imageUrls.get(exitIndex);
                if (view == null) {
                    activity.setExitSharedElementCallback((SharedElementCallback) null);
                    return;
                }
                names.add(transitName);
                sharedElements.put(transitName, view);
            }
            activity.setExitSharedElementCallback((SharedElementCallback) null);
        }
    });
}