ObjectAnimator动画的LinearLayout宽度 [英] ObjectAnimator animate LinearLayout width

查看:212
本文介绍了ObjectAnimator动画的LinearLayout宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我签出 <一href="http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html">http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html

他说,你可以在给定时间内的动画对象的属性。我试图围绕移动物体时,它看起来很好。我遇到一个问题,当我去改变的LinearLayout的宽度。我得到这样的:

He says you can animate the property of an object in a given time. And i tried moving around objects and it looks fine. I encountered a problem when i went changing the width of a LinearLayout. I got this:

10-26 14:51:27.190: E/PropertyValuesHolder(12681): Couldn't find setter/getter for       property width with value type float

然后我试图扩展的LinearLayout,以myWidth

Then i tried extending LinearLayout, with "myWidth"

public void setMyWidth(int myWidth) {
    LinearLayout.LayoutParams params = (LayoutParams) getLayoutParams();
    params.weight = myWidth;
    setLayoutParams(params);
    requestLayout();
    this.myWidth = myWidth;
}

没有运气。然后我试图改变LayoutParams.width,原来的宽度和高度在Java历史上唯一的公共财产,ObjectAnimator需要getter和setter。没有运气。 我不好意思说我试图延长的LayoutParams太...没有运气OFC。

No luck. Then i tried changing LayoutParams.width, turns out width and height are the only public properties in java history, and ObjectAnimator needs getter and setter. No luck. I'm embarassed to say i tried extending LayoutParams too... with no luck ofc.

任何人succeded做这样的事?我用老android.view.animation我得到了我想要的,但我很好奇的未来。

Anybody succeded doing such a thing? I used old android.view.animation and i got what i wanted, but i'm curious for the future.

推荐答案

在情况下,没有一个简单的属性的getter / setter你应该使用使用<一个href="http://developer.android.com/guide/topics/graphics/prop-animation.html#value-animator">ValueAnimator并手动进行动画。

In situations where there isn't a simple property getter/setter you should use Use ValueAnimator and perform the animation manually.

假设的ViewGroup 是你在动画的观点,你的code应该是这个样子:

Assuming viewGroup is the view you're animating, Your code should look something like this:

    ValueAnimator anim = ValueAnimator.ofInt(viewGroup.getMeasuredWidth(), END_WIDTH);
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            int val = (Integer) valueAnimator.getAnimatedValue();
            ViewGroup.LayoutParams layoutParams = viewGroup.getLayoutParams();
            layoutParams.width = val;
            viewGroup.setLayoutParams(layoutParams);
        }
    });
    anim.setDuration(DURATION);
    anim.start(); 

这篇关于ObjectAnimator动画的LinearLayout宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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