Dialogfragment在图像项目单击时动态增长动画

时间:2016-12-20 12:12:09

标签: java android animation dialogfragment

我使用网格布局管理器创建了recyclerview。当任何人点击图像缩略图时,图像应该在Dialogfragment中扩展为全屏,其中包含点击动画的视图寻呼机。

我尝试了不同的解决方案,但没有奏效。动画直接应用于样式,但如何定义动态运行时动画。

我曾尝试以下方法,但没有完全正常工作。

有关如何实现此事的任何想法?

MediaFragment.java

@Override
public void onItemClick(View v, int position) {
    switch (v.getId()) {
        case R.id.llItemContainer:
            Bundle bundle = new Bundle();
            bundle.putInt(Constant.KEY_CURRENT_ITEM, position);
            dialogMediaProfileFragment = new DialogMediaProfileFragment();
            dialogMediaProfileFragment.setArguments(bundle);
            dialogMediaProfileFragment.show(getFragmentManager(), TAG);
            zoomImageFromThumb(v).start();
            break;
    }
}

private Animator zoomImageFromThumb(final View thumbView) {

    if (mCurrentAnimator != null) {
        mCurrentAnimator.cancel();
    }

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

    thumbView.getGlobalVisibleRect(startBounds);
    rvMediaPost.getGlobalVisibleRect(finalBounds, globalOffset);
    startBounds.offset(-globalOffset.x, -globalOffset.y);
    finalBounds.offset(-globalOffset.x, -globalOffset.y);

    if ((float) finalBounds.width() / finalBounds.height() > (float) startBounds
            .width() / startBounds.height()) {
        // Extend start bounds horizontally
        startScale = (float) startBounds.height() / finalBounds.height();
        float startWidth = startScale * finalBounds.width();
        float deltaWidth = (startWidth - startBounds.width()) / 2;
        startBounds.left -= deltaWidth;
        startBounds.right += deltaWidth;
    } else {
        // Extend start bounds vertically
        startScale = (float) startBounds.width() / finalBounds.width();
        float startHeight = startScale * finalBounds.height();
        float deltaHeight = (startHeight - startBounds.height()) / 2;
        startBounds.top -= deltaHeight;
        startBounds.bottom += deltaHeight;
    }
    setAlpha(thumbView, 1f);
    //This shows the nullpointer exception

    /*setPivotX(dialogMediaProfileFragment.getView(), 0f);
    setPivotY(dialogMediaProfileFragment.getView(), 0f);*/

    AnimatorSet set = new AnimatorSet();
    set.play(
            ObjectAnimator.ofFloat(dialogMediaProfileFragment, "translationX",
                    startBounds.left, finalBounds.left))
            .with(ObjectAnimator.ofFloat(dialogMediaProfileFragment, "translationY",
                    startBounds.top, finalBounds.top))
            .with(ObjectAnimator.ofFloat(dialogMediaProfileFragment, "scaleX",
                    startScale, 1f))
            .with(ObjectAnimator.ofFloat(dialogMediaProfileFragment, "scaleY",
                    startScale, 1f));
    set.setDuration(mShortAnimationDuration);
    set.setInterpolator(new DecelerateInterpolator());
    set.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            mCurrentAnimator = null;
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            mCurrentAnimator = null;
        }
    });
    return mCurrentAnimator = set;

}

private void setAlpha(View view, float alpha) {
    view.setAlpha(alpha);
}

private void setPivotX(View view, float x) {
    view.setPivotX(x);
}

private void setPivotY(View view, float y) {
    view.setPivotY(y);
}

1 个答案:

答案 0 :(得分:0)

@Jaymin Panchal,

  

您必须实施自定义Android Activity Transition animation

另外,您可以参考 this code snippet 了解有关您的要求的更多信息。