【问题标题】:How to expand view smoothly in Android?如何在Android中顺利展开视图?
【发布时间】:2012-10-26 10:52:46
【问题描述】:

我将 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);
   }
}

所以我留了两秒钟让应用程序展开视图。然后两秒钟后视图将再次显示。我预计视图在显示时会扩展到全屏。但实际上,并没有。视图在显示后而不是在它隐藏的两秒钟内执行展开操作。

【问题讨论】:

  • 是的,这是一个很好的解决方法。但是有没有更直接的解决方案?
  • @wayne_bai :我认为没有比使用动画更好的方法了。它们实施起来并不复杂。
  • 发布 2 秒等待部分,我怀疑你没有在主线程上松开手
  • @njzk2:我只使用 handler.postDelay(runnable,time)。我已经更新了代码。

标签: android view layoutparams


【解决方案1】:

我知道现在回答这个问题已经很晚了,但我会告诉你我为有需要的人选择动画布局更改的方式。

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);

您可以根据需要更改fromX,toX,fromY & toY

例如,如果显示视图并且您必须将其展开,则根据需要将fromXfromY设置为1.0ftoXtoY

现在,使用同一个类,您可以通过稍微扩展视图然后将其缩小到原始大小来创建更酷的显示视图效果。为此,将使用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);

【讨论】:

    【解决方案2】:
    1. android:animateLayoutChanges="true" 添加到您的ViewGroup
    2. 使用setVisibility() 控制目标 View 的可见性。
    3. 如果你下面还有其他Viewtarget View,添加android:animateLayoutChanges="true"到你的outer ViewGroupsetVisibility()之前的代码:

      LayoutTransition layoutTransition = rootLinearLayout.getLayoutTransition();
      layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
      

    【讨论】:

      猜你喜欢
      • 2015-10-10
      • 2018-08-10
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多