package com.owen.tvrecyclerview.example.bridge;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Rect;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import com.owen.tvrecyclerview.widget.TvRecyclerView;
public class RecyclerViewBridge extends EffectNoDrawBridge {
private AnimatorSet mCurrentAnimatorSet;
private int mDx = 0;
private int mDy = 0;
public void setFocusView(View newView, View oldView, float scale, int dx, int dy) {
this.mDx = dx;
this.mDy = dy;
//
setFocusView(newView, scale);
setUnFocusView(oldView);
}
/**
* 重写边框移动函数.
*/
@Override
public void flyWhiteBorder(final View focusView, View moveView, float scaleX, float scaleY) {
Rect paddingRect = getDrawUpRect();
int newWidth = 0;
int newHeight = 0;
int oldWidth = 0;
int oldHeight = 0;
int newX = 0;
int newY = 0;
if (focusView != null) {
newWidth = (int) (focusView.getMeasuredWidth() * scaleX);
newHeight = (int) (focusView.getMeasuredHeight() * scaleY);
oldWidth = moveView.getMeasuredWidth();
oldHeight = moveView.getMeasuredHeight();
Rect fromRect = findLocationWithView(moveView);
Rect toRect = findLocationWithView(focusView);
// add by zhousuqiang
if(null != focusView.getParent() && focusView.getParent() instanceof TvRecyclerView) {
final TvRecyclerView rv = (TvRecyclerView) focusView.getParent();
final int offset = rv.getSelectedItemScrollOffset();
if(offset != -1) {
toRect.offset(rv.getLayoutManager().canScrollHorizontally() ? -offset : 0,
rv.getLayoutManager().canScrollVertically() ? -offset : 0);
}
}
//
if (mDy != 0) {
toRect.set(toRect.left, toRect.top - (mDy), toRect.right, toRect.bottom - (mDy));
mDy = 0;
}
//
int x = toRect.left - fromRect.left - (paddingRect.left);
int y = toRect.top - fromRect.top - (paddingRect.top);
newX = x - Math.abs(focusView.getMeasuredWidth() - newWidth) / 2;
newY = y - Math.abs(focusView.getMeasuredHeight() - newHeight) / 2;
//
newWidth += (paddingRect.right + paddingRect.left);
newHeight += (paddingRect.bottom + paddingRect.top);
}
// 取消之前的动画.
if (mCurrentAnimatorSet != null) {
mCurrentAnimatorSet.cancel();
mCurrentAnimatorSet = null;
}
ObjectAnimator transAnimatorX = ObjectAnimator.ofFloat(moveView, "translationX", newX);
ObjectAnimator transAnimatorY = ObjectAnimator.ofFloat(moveView, "translationY", newY);
// BUG,因为缩放会造成图片失真(拉伸).
// hailong.qiu 2016.02.26 修复 :)
ObjectAnimator scaleXAnimator = ObjectAnimator.ofInt(new ScaleView(moveView), "width", oldWidth,
(int) newWidth);
ObjectAnimator scaleYAnimator = ObjectAnimator.ofInt(new ScaleView(moveView), "height", oldHeight,
(int) newHeight);
//
AnimatorSet mAnimatorSet = new AnimatorSet();
mAnimatorSet.playTogether(transAnimatorX, transAnimatorY, scaleXAnimator, scaleYAnimator);
mAnimatorSet.setInterpolator(new DecelerateInterpolator(1));
mAnimatorSet.setDuration(getTranDurAnimTime());
mAnimatorSet.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
if (isVisibleWidget()) {
getMainUpView().setVisibility(View.GONE);
}
if (getNewAnimatorListener() != null)
getNewAnimatorListener().onAnimationStart(RecyclerViewBridge.this, focusView, animation);
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
getMainUpView().setVisibility(isVisibleWidget() ? View.GONE : View.VISIBLE);
if (getNewAnimatorListener() != null)
getNewAnimatorListener().onAnimationEnd(RecyclerViewBridge.this, focusView, animation);
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
mAnimatorSet.start();
mCurrentAnimatorSet = mAnimatorSet;
}
}