放大android中的动画

时间:2019-05-07 10:36:00

标签: android android-animation

我想在尝试场景的android中实现以下动画,但根据确认的文档,场景无法与文本配合使用:

“如果尝试使用动画来调整TextView的大小,则在对象完全调整大小之前,文本会弹出到新位置。为避免此问题,请不要为调整包含文本的视图的大小设置动画。”

请任何解决方案,放大的布局文本也可以包含图像。 animation video

1 个答案:

答案 0 :(得分:0)

这东西是如何工作的,但是动画有点抖动,我想首先要达到布局高度的最终值,然后再达到layoutWidth,必须解决这个问题。这是我的放大/缩小动画:

public class EnlargeAnimation extends Animation {

private final int diffHeight;
private final int diffWidth;
private final int initialHeight;
private final int initialWidth;
private final View targetView;

public EnlargeAnimation(View targetView, float targetHeight, float targetWidth) {
    this.targetView = targetView;
    this.initialHeight = targetView.getMeasuredHeight();
    this.initialWidth = targetView.getMeasuredWidth();
    this.diffHeight = (int) (targetHeight-initialHeight);
    this.diffWidth = (int) (targetWidth-initialWidth);
}

@Override
public boolean willChangeBounds() {
    return true;
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {

    float newHeight = initialHeight + diffHeight * interpolatedTime;
    float newWidth = initialWidth + diffWidth * interpolatedTime;
    targetView.getLayoutParams().height = (int) newHeight;
    targetView.getLayoutParams().width = (int) newWidth;
    targetView.requestLayout();
}

}

这是所谓的放大动画:

我正在使用viewpager,因此我必须使padding为负数以扩大卡的大小:

  ValueAnimator paddingAnimator = ValueAnimator.ofInt(20, -10).setDuration(400);
                paddingAnimator.setInterpolator(new LinearInterpolator());
                paddingAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int padding = (int) animation.getAnimatedValue();
                        view.setPadding(padding,
                                (int) DeviceUtils.convertDpToPx(50, v.getContext()), padding,
                                (int) DeviceUtils.convertDpToPx(50, v.getContext()));
                        view.requestLayout();
                    }
                });
                viewPagerItemSizeListener.onEnlarged();
                EnlargeAnimation
                        enlargeAnimation =
                        new EnlargeAnimation(cardView, screenHeight, screenWidth);
                enlargeAnimation.setDuration(400);
                enlargeAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        paddingAnimator.start();
                        seeExampleText.setVisibility(View.INVISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        stage.setVisibility(View.GONE);
                        cardEnlargedWidth = cardView.getLayoutParams().width;
                        cardEnlargedHeight = cardView.getLayoutParams().height;
                        crossContianer.setVisibility(View.VISIBLE);
                        detailTextContianer.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                view.startAnimation(enlargeAnimation);

我猜没有人在读它,但是如果对变量有些困惑,请告诉我我将编辑答案。