Android将自定义属性添加到View.animate()。*

时间:2012-12-21 18:28:37

标签: android view sdk

是否可以在Android 4.x中向View.animate()。*添加自定义属性?

Android 4.x提供View.animate(),然后允许动画几个属性。 像:

View.animate().alpha(0.5f).setDuration.start();

我想为这个对象添加一个自定义属性,如:

CustomView.animate().xyz(0.1f).setDuration.start();

1 个答案:

答案 0 :(得分:3)

您可以使用自定义动画执行此操作:

public class CustomAnim extends Animation {

    private CustomView mLayout;
    private float finalVal;
    private float startVal;

    public CustomAnim(CustomView layout, float finalVal) {
        this.mLayout = layout;
        this.finalVal = finalVal;
        this.startVal = layout.xyz();
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        mLayout.setXYZ(interpolatedTime * (finalVal - startVal) + startVal);
    }

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

或者,如果你的属性有getter和setter方法,例如setXYZ getXYZ,你可以使用property animation