安卓:与添加视图扩展动画(无闪烁) [英] Android: add view with expand animation (without blinking)

查看:181
本文介绍了安卓:与添加视图扩展动画(无闪烁)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个扩大的动画添加视图视图组,所以添加的视图开始非常小,需要越来越多的空间,直到达到它的全尺寸(可能移动的过程中其他的观点)。

在尝试不同的方法,我想出了下面的解决方案。投票,如果它可以帮助你,或请张贴一个更好的选择。我相信必须有一个更简单的方法来做到这一点。

有关更多信息,请参阅如这个并<一href="http://stackoverflow.com/questions/4946295/android-expand-collapse-animation/5122460#5122460">this 之一。

下面是我加的观点,并启动动画:

  //绝招:设置高度为1,因此认为不采取它的全尺寸开头。
//(0不工作,我不知道为什么)
container.addView(视图,LayoutParams.MATCH_PARENT,1);
动画扩张= createExpansion(视图);
expansion.setDuration(200);
view.startAnimation(扩张);
 

createExpansion()方法:

 公共静态动画createExpansion(查看视图){

    返回新SizedHeightScaleAnimation(查看,0,1,
            Animation.RELATIVE_TO_SELF,0F,
            Animation.RELATIVE_TO_SELF,0F);
}
 

最后的 SizedHeightScaleAnimation 动画类:

  / **
 *如{@link ScaleAnimation}身高比例也调整大小的观点。因此,
 *所以它需要的空间适量而扩展。
 * /
公共类SizedHeightScaleAnimation扩展ScaleAnimation {

    私人浮动fromHeight;
    私人浮动toHeight;
    私人查看viewToScale;

    公共SizedHeightScaleAnimation(查看viewToScale,浮弗罗米,漂浮玩具){
        超(1,1,弗罗米,玩具);
        的init(viewToScale,弗罗米,玩具);
    }

    公共SizedHeightScaleAnimation(查看viewToScale,浮弗罗米,漂浮玩具,浮pivotX,浮pivotY){
        超级(1,1,弗罗米,玩具,pivotX,pivotY);
        的init(viewToScale,弗罗米,玩具);
    }

    公共SizedHeightScaleAnimation(查看viewToScale,浮弗罗米,漂浮玩具,INT pivotXType,浮pivotXValue,INT pivotYType,浮动pivotYValue){
        超级(1,1,弗罗米,玩具,pivotXType,pivotXValue,pivotYType,pivotYValue);
        的init(viewToScale,弗罗米,玩具);
    }

    私人无效的init(查看viewToScale,浮弗罗米,漂浮玩具){

        this.viewToScale = viewToScale;
        viewToScale.measure(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        fromHeight = viewToScale.getMeasuredHeight()*弗罗米;
        toHeight = viewToScale.getMeasuredHeight()*玩具;
    }

    @覆盖
    保护无效applyTransformation(浮动interpolatedTime,变换T){
        super.applyTransformation(interpolatedTime,T);

        最终诠释newHeight =(INT)(fromHeight *(1  -  interpolatedTime)+ toHeight * interpolatedTime);

        。viewToScale.getLayoutParams()高度= newHeight;
        viewToScale.requestLayout();
    }
}
 

顺便说一句,您可以创建崩溃效果是这样的:

 公共静态动画createCollapse(查看视图){

    返回新SizedHeightScaleAnimation(视图,1,0,
            Animation.RELATIVE_TO_SELF,0F,
            Animation.RELATIVE_TO_SELF,0F);
}
 

当你使用它,你可能会想从其父删除视图时的动画完成:

 动画崩溃= createCollapse(视图);
collapse.setDuration(200);

collapse.setAnimationListener(新AnimationListener(){
    @覆盖
    公共无效onAnimationEnd(动画动画){
        最后的ViewGroup家长=(ViewGroup中)view.getParent();
        parent.removeView(视图);
    }

    ...等覆盖...
});

view.startAnimation(崩溃);
 

解决方案

这可能是有点简单:

 公共无效扩展(最终查看查看)
{
    。view.getLayoutParams()高度= 0;

    动画=新的动画()
    {
        @覆盖
        保护无效applyTransformation(浮动interpolatedTime,变换T)
        {
            。view.getLayoutParams()身高=(INT)(mTargetHeight * interpolatedTime);
            view.requestLayout();
        }

        @覆盖
        公共布尔willChangeBounds(){
            返回true;
        }
    };

    a.setDuration(1000);
    view.startAnimation(一);
}
 

I want to add a view to a view group using an expanding animation, so the added view starts very small and takes more and more space until it reaches its full size (probably moving other views in the process).

After trying different approaches I came up with the solution below. Vote if it helps you or please post a better alternative. I am sure there must be an easier way to do this.

For more information see posts like this one and this one.

Here's how I add the view and start the animation:

// Trick: set height to 1 so the view doesn't take its full size at the beginning.
// (0 doesn't work, I don't know why)
container.addView(view, LayoutParams.MATCH_PARENT, 1);
Animation expansion = createExpansion(view);
expansion.setDuration(200);
view.startAnimation(expansion);

The createExpansion() method:

public static Animation createExpansion(View view) {

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

And finally the SizedHeightScaleAnimation animation class:

/**
 * Like {@link ScaleAnimation} for height scaling that also resizes the view accordingly,
 * so it takes the right amount of space while scaling.
 */
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();
    }
}

By the way, you can create the collapse effect this way:

public static Animation createCollapse(View view) {

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

When you use it you'll probably want to remove the view from its parent when the animation is done:

Animation collapse = createCollapse(view);
collapse.setDuration(200);

collapse.setAnimationListener(new AnimationListener() {
    @Override
    public void onAnimationEnd(Animation animation) {
        final ViewGroup parent = (ViewGroup) view.getParent();
        parent.removeView(view);
    }

    ... other overrides ...
});

view.startAnimation(collapse);

解决方案

This might be a little simpler:

public void expand(final View view)
{
    view.getLayoutParams().height = 0;

    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t)
        {
            view.getLayoutParams().height = (int)(mTargetHeight * interpolatedTime);
            view.requestLayout();
        }

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

    a.setDuration(1000);
    view.startAnimation(a);
}

这篇关于安卓:与添加视图扩展动画(无闪烁)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆