一种带动画自定义控件的封装

来源:互联网 发布:c语言高级编程 编辑:程序博客网 时间:2024/05/10 11:29

通常我们有自定义控件的需求,但一般情况下需要有动画的效果,我们可以通过代理的方式将动画效果封装在整个控件里,做到高度内聚。


public class NineLinearLayout extends LinearLayout {
    private final AnimatorProxy mProxy;


    public NineLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mProxy = AnimatorProxy.wrap(this);
    }


    @Override
    public void setVisibility(int visibility) {
        if (mProxy != null) {
            if (visibility == GONE) {
                clearAnimation();
            } else if (visibility == VISIBLE) {
                setAnimation(mProxy);
            }
        }
        super.setVisibility(visibility);
    }


    public float getAlpha() {
        return mProxy.getAlpha();
    }


    public void setAlpha(float alpha) {
         mProxy.setAlpha(alpha);
    }


    public float getTranslationX() {
    return mProxy.getTranslationX();
    }


    public void setTranslationX(float translationX) {
  mProxy.setTranslationX(translationX);
    }
}

0 0