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

问题描述 投票:4回答:1

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

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

android view layoutparams
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);
© www.soinside.com 2019 - 2024. All rights reserved.