如何在Android中流畅地扩展视图?

时间:2012-11-06 13:13:20

标签: android view layoutparams

当我将320 * 50视图扩展为全屏时,我遇到了一个问题。

如果我直接扩展它,它可以工作但行动非常突然,我认为这不是一个好的用户体验。所以我首先隐藏视图,然后展开它,两秒后再次显示视图。

TextView.setVisibility(VIEW.INVISIBLE);
ViewGroup.LayoutParams lp = TextView.getLayoutParams();
lp.width = ViewGroup.LayoutParams.FILL_PARENT;
lp.height = ViewGroup.LayoutParams.FILL_PARENT;

//after two seconds
handler.postDelay(new Show(),2000);

class Show implements Runnable{
   @Override
public void run(){
       TextView.setVisibility(VIEW.VISIBLE);
   }
}

所以我给应用程序留下了两秒钟来展开视图。然后两秒后视图将再次显示。我希望视图在显示时扩展为全屏。但实际上,事实并非如此。视图在显示后执行展开操作,而不是在隐藏的两秒内执行。

2 个答案:

答案 0 :(得分:2)

  1. android:animateLayoutChanges="true"添加到您的ViewGroup
  2. 使用setVisibility()来控制目标 View的可见性。
  3. 如果您目标 View下还有其他View,请将android:animateLayoutChanges="true"添加到您的外部 {{1 }}和ViewGroup之前的以下代码:

    setVisibility()

答案 1 :(得分:0)

我知道现在回答这个问题真的很晚,但是我只想告诉我我为需要帮助的人设置布局动画的方式。

Android有一个名为ScaleAnimation的特殊动画类,我们可以在其中平滑地扩展或折叠视图。

通过对角扩展显示视图:

ScaleAnimation expand = new ScaleAnimation(
   0, 1.0f,
   0, 1.0f,
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0);
expand.setDuration(250);

view.startAnimation(expand)

使用的构造函数为:

ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

因此您可以相应地更改值。

例如,以下示例将水平显示视图动画

ScaleAnimation expand = new ScaleAnimation(
   0, 1.1f,
   1f, 1f,
   Animation.RELATIVE_TO_PARENT, 0,
   Animation.RELATIVE_TO_PARENT, 0);
expand.setDuration(250);

您可以根据需要更改fromXtoXfromYtoY

例如,如果显示了视图,而您只需要展开它,则将fromXfromY放在1.0f上,将toXtoY放在需要。

现在,使用相同的类,您可以通过将视图稍微扩大一点,然后将其缩小到原始大小,来创建更酷的效果来显示视图。为此,将使用AnimationSet。这样会产生一种泡沫效果。

下面的示例用于创建气泡效果以显示视图:

AnimationSet expandAndShrink = new AnimationSet(true);
ScaleAnimation expand = new ScaleAnimation(
   0, 1.1f,
   0, 1.1f,
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0);
expand.setDuration(250);

ScaleAnimation shrink = new ScaleAnimation(
   1.1f, 1f,
   1.1f, 1f,
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0);
shrink.setStartOffset(250);
shrink.setDuration(120);

expandAndShrink.addAnimation(expand);
expandAndShrink.addAnimation(shrink);
expandAndShrink.setFillAfter(true);
expandAndShrink.setInterpolator(new AccelerateInterpolator(1.0f));

view.startAnimation(expandAndShrink);