【问题标题】:Android: grow/shrink View over timeAndroid:随时间增长/缩小视图
【发布时间】:2011-06-15 16:59:53
【问题描述】:

我有这样的视图布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@color/light_gray"
    android:padding="5dip">

    <View android:id="@+id/fixedSpace" android:layout_width="fill_parent"
        android:layout_height="50dip" android:background="@color/aqua"
        android:layout_alignParentBottom="true" android:clickable="true"
        android:onClick="onClickStartAnimation" />

    <View android:id="@+id/dynamicSpace" android:layout_width="fill_parent"
        android:layout_height="200dip" android:background="@color/lime"
        android:layout_above="@id/fixedSpace" />


    <View android:id="@+id/remainingSpace" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:background="@color/pink"
        android:layout_alignParentTop="true" android:layout_above="@id/dynamicSpace" />


</RelativeLayout>

我想要实现的基本上是dynamicSpace 在一段时间内的增长/收缩行为t。使用动画,我可以制作以下内容:

t=1:

t=2:

t=3:

但是,这并没有真正调整我的视图大小,尤其是 dynamicSpaceremainingSpace。它只是动画视图dynamicSpace 移动。但是视图“容器”从一开始就已经占据了空间。

正确的是,石灰色的dynamicSpace 以 0px 开头,粉红色的remainingSpace 接管,因此两者之间没有灰色空间。

【问题讨论】:

    标签: android android-layout android-animation


    【解决方案1】:

    Scale the View

    既然你说你会随着时间的推移这样做t,听起来LinearInterpolator 是最好的。

    【讨论】:

    • 我确实已经使用过动画了。但是,这并不能解决实际放大或缩小实际视图的问题。它只是动画它。很快会在我的问题中提供更多信息。
    • 为什么你不能在动画后重新调整视图的大小(因为你知道你动画了多少)?
    • 这不起作用,因为这是调整大小的 1 步。但我实际上正在寻找的是随着时间的推移调整大小。动画并没有真正调整任何东西的大小(它们只是看起来像)。
    【解决方案2】:

    编辑: 我尝试用AsyncTask 线程替换下面的内容,它更流畅。我认为关键是我让线程在后台运行,并在我想调整大小时使用它,从而减少开销


    创建一个自定义 AnimationListener 并将调整视图大小的代码放在 onAnimationRepeat 方法中。

    然后做一个虚拟动画并将动画的重复设置为无限。一旦视图达到最终大小,将动画的重复计数设置为零(再次在 onAnimationRepeat 中):

    class ResizeAnimationListener implements AnimationListener{
    
    int finalHeight; // max Height
    int resizeAmount; // amount to resize each time
    View view; // view to resize
    
    public ResizeAnimationListener(int finalHeight; View view, int resizeAmount) {
        super();
        finalHeight; = finalHeight; 
        this.resizeAmount = resizeAmount;
        this.view = view;
    
    }
    
    @Override
    public void onAnimationEnd(Animation animation) {
    
    }
    
    @Override
    public void onAnimationRepeat(Animation animation) {
    
        int newHeight;
        int currentHeight;
    
        current = view.getMeasuredHeight();
    
        newHeight= currentHeight+ resizeAmount;
        if(newHeight> finalHeight){
            // check if reached final height
    
            // set new height to the final height
            newHeight = finalHeight;
            // set repeat count to zero so we don't have any more repeats
            anim.setRepeatCount(0);
    
        }
    
        // set new height
        LayoutParams params = view.getLayoutParams();                                           
        params.height = newHeight;
        v.setLayoutParams(params);                                      
    
    }
    
    @Override
    public void onAnimationStart(Animation animation) {
    
    }
    
    };
    
    class DummyAnimation extends Animation{}
    
    
    float frameRate = 1000/30;                              
    DummyAnimation anim = new DummyAnimation();
    anim.setDuration((long)frameRate);                          
    anim.setRepeatCount(Animation.INFINITE);
    ResizeAnimationListener animListener = new ResizeAnimationListener(((View)view.getParent()).getHeight(), view, 25);
    anim.setAnimationListener(animListener);
    
    view.startAnimation(anim);
    

    我在自己的应用程序上完成了这项工作。但是,锚定到我正在调整大小的视图的视图(因此当我调整视图大小时在屏幕上移动)似乎出现了故障。可能与重复调整大小有关,而不是其他任何事情,但只是一个警告。也许其他人知道为什么?

    【讨论】:

      猜你喜欢
      • 2014-07-07
      • 2010-12-05
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      • 2015-06-01
      • 1970-01-01
      • 2012-07-31
      相关资源
      最近更新 更多