展开查看动画 - 问题

时间:2016-02-23 12:05:37

标签: android animation

我是我的应用程序中的新手我将我的视图扩展到以下代码动画集:

Animation expansion = createExpansion(mytextview);
                    expansion.setDuration(400);
                    mytextview.startAnimation(expansion);

以下一组功能:

public static Animation createExpansion(View view) {

            return new SizedHeightScaleAnimation(view, 0, 1,
                    Animation.RELATIVE_TO_SELF, 0f,
                    Animation.RELATIVE_TO_SELF, 0f);
        }

这是我的SizedHeightScaleAnimation.class文件:

public class SizedHeightScaleAnimation extends ScaleAnimation{


    private float fromHeight;
    private float toHeight;
    private View viewToScale;

    public SizedHeightScaleAnimation(View viewToScale, float fromY, float toY) {
        super(1, 1, fromY, toY);
        init(viewToScale, fromY, toY);
    }

    public SizedHeightScaleAnimation(View viewToScale, float fromY, float toY, float pivotX, float pivotY) {
        super(1, 1, fromY, toY, pivotX, pivotY);
        init(viewToScale, fromY, toY);
    }

    public SizedHeightScaleAnimation(View viewToScale, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {
        super(1, 1, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue);
        init(viewToScale, fromY, toY);
    }

    private void init(View viewToScale, float fromY, float toY) {

        this.viewToScale = viewToScale;
        viewToScale.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        fromHeight = viewToScale.getMeasuredHeight() * fromY;
        toHeight = viewToScale.getMeasuredHeight() * toY;
    }

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

        final int newHeight = (int) (fromHeight * (1 - interpolatedTime) + toHeight * interpolatedTime );


        viewToScale.getLayoutParams().height = newHeight;
        viewToScale.requestLayout();
    }

视图得到扩展,但该描述中的最后一行是不可见的,它会削减该描述的某些部分。任何人请帮助出错/计算中的任何错误,提前谢谢

2 个答案:

答案 0 :(得分:0)

new SizedHeightScaleAnimation(view, 0, 1,
        Animation.RELATIVE_TO_SELF, 0f,
        Animation.RELATIVE_TO_SELF, 0f);

相当于:

new ScaleAnimation(1, 1, 0, 1,
        Animation.RELATIVE_TO_SELF, 0f,
        Animation.RELATIVE_TO_SELF, 0f);

您只是将视图从高度(0)缩放到其原始高度。因此,与视图上设置的高度相比,textview的描述太长。这就是它被削减的原因。缩放没有问题。

答案 1 :(得分:0)

我尝试了另一种方法来解决我的问题,它只对我有用,因为我无法通过设置它的动画持续时间来平滑过渡。下面是解决方案描述切割问题:

private void expand(View summary) {
            //set Visible
            summary.setVisibility(View.VISIBLE);


                    final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

                   summary.measure(widthSpec, 0);
                    //summary.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);


                    mAnimator = slideAnimator(0, 0, summary);

            mAnimator.start();
            //mAnimator.setDuration(700);

        }


        private ValueAnimator slideAnimator(int start, int end, final View summary) {

            ValueAnimator animator = ValueAnimator.ofInt(start, end);
            //animator.setDuration(700);

            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    //Update Height
                    int value = (Integer) valueAnimator.getAnimatedValue();

                    ViewGroup.LayoutParams layoutParams = summary.getLayoutParams();
                layoutParams.height = value;
                   //layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    summary.setLayoutParams(layoutParams);

                }
            });
            return animator;
        }