【问题标题】:Can I hide an Android control with an animation?我可以用动画隐藏 Android 控件吗?
【发布时间】:2011-12-09 10:23:17
【问题描述】:

我有一个包含带有三个 RadioButtons 的 RadioGroup 的 Android 视图。 When one of the RadioButtons is selected, the user also has to enter text into an EditText control.如果选择了其他两个 RadioButton 中的任何一个,则不需要此额外信息。

我目前正在使用 RadioGroup 的 OnCheckedChangedListener 来确定何时检查新的 RadioButton,并通过将 EditText 的可见性设置为 View.GONE 来隐藏 EditText。然而,这有点不和谐,我想知道是否有一种方法可以让过渡动画化。这可能吗?如果可以,入门的关键是什么?

【问题讨论】:

    标签: android animation android-edittext


    【解决方案1】:

    由于您使用的是 API Level 7,因此 LayoutTransition 的答案不再适用。

    参考文章:How can I set an entire view's alpha value in api level 7 (Android 2.1)

    【讨论】:

    • 该类仅在 API 级别 11 (Android 3.0) 中引入。我目前的目标是 Android 2.1(API 级别 7)之前的设备
    【解决方案2】:

    我提出了以下可行的解决方案,它基于我在 http://tech.chitgoks.com/2011/10/29/android-animation-to-expand-collapse-view-its-children/ 找到的代码

    在我的活动中:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_account);
        companyGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                if (checkedId == R.id.companyRadio)
                    EDNUtils.expandCollapse(companyNameText, true, 500);
                else
                    EDNUtils.expandCollapse(companyNameText, false, 500);
            }
        });
    }
    

    EDNUtils 的实现:

    public static Animation expandCollapse(final View v, final boolean expand) 
    {       
        return expandCollapse(v, expand, 1000);
    }
    
    public static Animation expandCollapse(final View v, final boolean expand, final int duration) 
    {
        int currentHeight = v.getLayoutParams().height;
        v.measure(MeasureSpec.makeMeasureSpec(((View)v.getParent()).getMeasuredWidth(), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        final int initialHeight = v.getMeasuredHeight();
    
        if ((expand && currentHeight == initialHeight) || (!expand && currentHeight == 0))
            return null;
    
        if (expand) 
            v.getLayoutParams().height = 0;
        else 
            v.getLayoutParams().height = initialHeight;
        v.setVisibility(View.VISIBLE);
    
        Animation a = new Animation() 
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) 
            {
                int newHeight = 0;
                if (expand) 
                    newHeight = (int) (initialHeight * interpolatedTime);
                else 
                    newHeight = (int) (initialHeight * (1 - interpolatedTime));
                v.getLayoutParams().height = newHeight;            
                v.requestLayout();
    
                if (interpolatedTime == 1 && !expand)
                    v.setVisibility(View.GONE);
            }
    
            @Override
            public boolean willChangeBounds()  
            {
                return true;
            }
        };
        a.setDuration(duration);
        v.startAnimation(a);
        return a;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多