Android:增长/收缩查看一段时间

时间:2011-06-15 16:59:54

标签: android android-layout android-animation

我有这样的视图布局:

<?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=1

t = 2时:

t=2

T = 3:

t=3

但是,这并没有真正调整我的观看次数,特别是dynamicSpaceremainingSpace。它只是动画视图dynamicSpace移入。但视图“容器”已经从一开始就占据了空间。

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

2 个答案:

答案 0 :(得分:0)

Scale the View

既然你说你正在做这件事 t ,听起来好像是LinearInterpolator

答案 1 :(得分:0)

编辑: 我尝试用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);

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