开发者

Android: grow/shrink View over time

I have a view layout like this:

<?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/fix开发者_运维技巧edSpace" 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>

What I want to achieve is basically a grow/shrink behavior of dynamicSpace over the time t. With animations I can produce the following:

t=1:

Android: grow/shrink View over time

t=2:

Android: grow/shrink View over time

t=3:

Android: grow/shrink View over time

However, that doesn't really resize my views, in particular dynamicSpace and remainingSpace. It just animates the view dynamicSpace moving in. But the view "container" already has the space occupied right from the beginning.

Correct would be that the lime colored dynamicSpace starts with 0px and the pink colored remainingSpace takes over, so there is no gray space in between.


Scale the View

Since you say you are doing it over time t, it sounds like a LinearInterpolator is best.


EDIT: I tried replacing the below with an AsyncTask thread and it is far smoother. I think the key is I keep the thread running in the background and just use it when I want to resize something, thus reducing overhead


Create a custom AnimationListener and put the code for resizing the view in the onAnimationRepeat method.

Then do a dummy animation and set repeat on the animation to infinite. Once the view has reached the final size, set repeat count on the animation to zero (again in 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);

I made this work on my own app . However, views anchored to the view I'm resizing (and thus moving on screen when I resize my view) seem to glitch out. Probably related to repeated resizing rather than anything else, but just a warning. Maybe someone else knows why?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜