带有ViewPager的Android RecyclerView

时间:2015-10-27 08:47:35

标签: android android-viewpager android-recyclerview

我已经实现了一个带有图像动画弹出窗口的RecyclerView。我在RecyclerView中用于缩放视图(图像)的代码来自这里:https://developer.android.com/training/animation/zoom.html

图像动画结束后,我希望能够左右滑动到其他图像。有没有好办法呢?

我的缩放图像代码(缩略图完全缩放后,显示的图像分辨率更高):

public void zoomImageFromThumb(final View thumbView, final Bitmap path,final String image) {
    if (mCurrentAnimator != null) {
        mCurrentAnimator.cancel();
    }

    final TouchImageView  imageView  = (TouchImageView) activity.findViewById(R.id.expanded_image);

    imageView.setImageBitmap(path);//thumbnail image

    final Rect startBounds = new Rect();
    final Rect finalBounds = new Rect();
    final Point globalOffset = new Point();

    thumbView.getGlobalVisibleRect(startBounds);
    this.activity.findViewById(R.id.container).getGlobalVisibleRect(finalBounds, globalOffset);
    startBounds.offset(-globalOffset.x, -globalOffset.y);
    finalBounds.offset(-globalOffset.x, -globalOffset.y);

    float startScale;
    if ((float) finalBounds.width() / finalBounds.height() > (float) startBounds.width() / startBounds.height()) {
        startScale = (float) startBounds.height() / finalBounds.height();
        float startWidth = startScale * finalBounds.width();
        float deltaWidth = (startWidth - startBounds.width()) / 2;
        startBounds.left -= deltaWidth;
        startBounds.right += deltaWidth;
    } else {
        startScale = (float) startBounds.width() / finalBounds.width();
        float startHeight = startScale * finalBounds.height();
        float deltaHeight = (startHeight - startBounds.height()) / 2;
        startBounds.top -= deltaHeight;
        startBounds.bottom += deltaHeight;
    }

    thumbView.setAlpha(0f);
    imageView.setVisibility(View.VISIBLE);

    imageView.setPivotX(0f);
    imageView.setPivotY(0f);

    AnimatorSet set = new AnimatorSet();
    set.play(ObjectAnimator.ofFloat(imageView, View.X, startBounds.left, finalBounds.left)).with(ObjectAnimator.ofFloat(imageView, View.Y, startBounds.top,
            finalBounds.top))
            .with(ObjectAnimator.ofFloat(imageView, View.SCALE_X, startScale, 1f))
            .with(ObjectAnimator.ofFloat(imageView, View.SCALE_Y, startScale, 1f));

    set.setDuration(mShortAnimationDuration);
    set.setInterpolator(new DecelerateInterpolator());
    set.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            mCurrentAnimator = null;
            imageView.setImageURI(Uri.parse(image));//Better resolution of an image is displayed
        }
        @Override
        public void onAnimationCancel(Animator animation) {
            mCurrentAnimator = null;
        }
    });

如果我只是去ViewPager并在动画完成后显示更好的图像分辨率,那就不顺利了。

1 个答案:

答案 0 :(得分:0)

解决方案是缩放ViewPager而不是缩放图像。它完美地运作。