重新进入的Android共享元素转换整个屏幕闪烁

时间:2016-07-03 19:48:53

标签: android android-recyclerview android-5.0-lollipop android-5.1.1-lollipop activity-transition

我正在尝试在回收者视图项(活动A)和&之间创建“简单”图像共享元素转换。视图寻呼机(活动B)。 我已经设法使一切顺利,但似乎无论我如何设置我的退出/重新进入/进入/返回转换,从B转换回A,当共享元素重新定位时,闪烁。 通过闪烁我的意思是整个屏幕。

我正在使用AppCompat主题&在A(重新进入)和B(使用Glide设置图像时)中使用PreDrawListeners组合的ActivityCompat方法。

Heres Activity相关代码:

private Bundle mReenterState;

@TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override protected void initActivityTransitions() {
    super.initActivityTransitions();
    getWindow().setSharedElementsUseOverlay(false);
    setExitSharedElementCallback(mCallback);
}

private final SharedElementCallback mCallback = new SharedElementCallback() {
    @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
        if (mReenterState != null) {
            int startingSelection = mReenterState.getInt(DefinesGlobal.KEY_STARTING_INDEX);
            int currentSelection = mReenterState.getInt(DefinesGlobal.KEY_CURRENT_INDEX);
            if (startingSelection != currentSelection) {
                String newTransitionName = mHotel.getDetail().getHotelImagesGallery().get(currentSelection);
                View newSharedElement = mContentView.getGalleryView().getRecyclerViewPager()
                        .getLayoutManager().findViewByPosition(currentSelection);
                if (newSharedElement != null) {
                    names.clear();
                    names.add(newTransitionName);
                    sharedElements.clear();
                    sharedElements.put(newTransitionName, newSharedElement);
                }
            }
            mReenterState = null;
        } else {
            // If mReenterState is null, then the activity is exiting.
            View navigationBar = findViewById(android.R.id.navigationBarBackground);
            View statusBar = findViewById(android.R.id.statusBarBackground);
            if (navigationBar != null) {
                names.add(navigationBar.getTransitionName());
                sharedElements.put(navigationBar.getTransitionName(), navigationBar);
            }
            if (statusBar != null) {
                names.add(statusBar.getTransitionName());
                sharedElements.put(statusBar.getTransitionName(), statusBar);
            }
        }
    }
};

@Override public void onActivityReenter(int resultCode, Intent data) {
    super.onActivityReenter(resultCode, data);
    mReenterState = new Bundle(data.getExtras());
    int startingSelection = mReenterState.getInt(DefinesGlobal.KEY_STARTING_INDEX);
    int currentSelection = mReenterState.getInt(DefinesGlobal.KEY_CURRENT_INDEX);
    if (startingSelection != currentSelection) {
        mContentView.getGalleryView().getRecyclerViewPager().scrollToPosition(currentSelection);

    }
    ActivityCompat.postponeEnterTransition(this);
    mContentView.getGalleryView().getRecyclerViewPager().getViewTreeObserver()
            .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            mContentView.getGalleryView().getRecyclerViewPager().getViewTreeObserver().removeOnPreDrawListener(this);
            mContentView.getGalleryView().getRecyclerViewPager().requestLayout();
            ActivityCompat.startPostponedEnterTransition(ViewModelHotelDetailsActivity.this);

            return true;
        }
    });
}

Heres Activity B相关代码。

private boolean mIsReturning;

@Override public void finishAfterTransition() {
    mIsReturning = true;
    Intent data = new Intent();
    data.putExtra(DefinesGlobal.KEY_CURRENT_INDEX, mCurrentSelection);
    data.putExtra(DefinesGlobal.KEY_STARTING_INDEX, mStartingSelection);
    setResult(RESULT_OK, data);
    super.finishAfterTransition();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override protected void initActivityTransitions() {
    super.initActivityTransitions();
    ActivityCompat.postponeEnterTransition(this);
    setEnterSharedElementCallback(mCallback);
}

private final SharedElementCallback mCallback = new SharedElementCallback() {
    @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
        if (mIsReturning) {
            ViewModelFullSizeGalleryFragment fragment =
                    ((FullSizeGalleryPagerAdapter) mPager.getAdapter()).getCurrent();
            ImageView sharedElement = fragment.getImageView();
            if (sharedElement == null) {
                names.clear();
                sharedElements.clear();
            } else if (mStartingSelection != mCurrentSelection) {
                names.clear();
                names.add(sharedElement.getTransitionName());
                sharedElements.clear();
                sharedElements.put(sharedElement.getTransitionName(), sharedElement);
            }
        }
    }
};

活动B视图寻呼机片段:

ublic类ViewModelFullSizeGalleryFragment扩展ViewModelBaseFragment {

@Override protected int getLayoutId() {
    return R.layout.view_model_fragment_full_size_gallery;
}

@Bind(R.id.full_size_image_view) ImageView mImageView;

public static ViewModelFullSizeGalleryFragment newInstance(String url, int position, int
        target) {
    ViewModelFullSizeGalleryFragment fragment = new ViewModelFullSizeGalleryFragment();
    fragment.setUrl(url);
    fragment.setIndexAndTarget(position, target);
    return fragment;
}

private String mUrl;
private int mPosition;
private int mTarget;

public void setUrl(String url) {
    mUrl = url;
}

public void setIndexAndTarget(int position, int target) {
    mPosition = position;
    mTarget = target;
}

@Override protected void initViewModel(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mImageView.setTransitionName(mUrl);
    }
    Glide.with(getContext())
            .load(mUrl)
            .transform(new HotelDetailsImageTransformation(getContext(),
                    LMTApplication.mWidth))
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .skipMemoryCache(true)
            .error(R.drawable.ic_nopic)
            .into(new SimpleTarget<GlideDrawable>() {
                @Override public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                    if (mImageView != null && resource != null) {
                        mImageView.setImageDrawable(resource);
                        mImageView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                            @Override public boolean onPreDraw() {
                                mImageView.getViewTreeObserver().removeOnPreDrawListener(this);
                                if (mTarget == mPosition)
                                    ((ISharedElementCallback) getContext()).onViewReadyForTransition();
                                return true;
                            }
                        });

                    }
                }
            });
}

@Override public void onBackPressed() {
    //!! Not used.
}

public interface ISharedElementCallback {
    void onViewReadyForTransition();
}

public ImageView getImageView() {
    return mImageView;
}

没有弄清楚实际上闪烁的是什么......任何帮助都会很棒。感谢。

另外。如果有任何帮助,那么活动A回收者视图后面的地图片段是否有任何帮助。

1 个答案:

答案 0 :(得分:0)

android:windowSharedElementsUseOverlay(在您的活动的XML主题文件中)设置为 false 可以解决您的问题吗?