Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

android - Change the weight of layout with an animation

In my main layout file, I have a RelativeLayout, with a weight of 1 (basically to display a map) above a LinearLayout with a weight of 2, declared this way :

<LinearLayout
    android:id="@+id/GlobalLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/UpLayout"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1" >
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/DownLayout"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="2"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

DownLayout contains a list of items, when I click on an item, I would like to change the weight of DownLayout for 4, so the upper layout (the map) takes only 1/5 of the screen instead of 1/3.

I have managed to do it by changing the LayoutParams :

    LinearLayout linearLayout = (LinearLayout) mActivity.findViewById(R.id.DownLayout);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    params.weight = 4.0f;
    linearLayout.setLayoutParams(params);

It works but I'm not satisfied, the change is too immediate, there is no transition while I would like it to be smooth. Is there a way to use animation for that ?

I found some examples with ObjectAnimator to change the weightSum, but it does not do want I want (if I change only this property, I have some free space below my down layout) :

        float ws = mLinearLayout.getWeightSum();
        ObjectAnimator anim = ObjectAnimator.ofFloat(mLinearLayout, "weightSum", ws, 5.0f);
        anim.setDuration(3000);
        anim.addUpdateListener(this);
        anim.start();

Is there a way to use ObjectAnimator (or something else) to do that ?

Thanks !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I recently came across a similar problem and solved it using a standard Animation (I have to target API 10 so couldn't use ObjectAnimator). I used a combination of the answer here with slight alterations to take into account weight instead of height.

My custom animation class looks as follows...

private class ExpandAnimation extends Animation {

    private final float mStartWeight;
    private final float mDeltaWeight;

    public ExpandAnimation(float startWeight, float endWeight) {
        mStartWeight = startWeight;
        mDeltaWeight = endWeight - startWeight;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mContent.getLayoutParams();
        lp.weight = (mStartWeight + (mDeltaWeight * interpolatedTime));
        mContent.setLayoutParams(lp);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}

And its called by this method...

public void toggle() {
    Animation a;
    if (mExpanded) {
        a = new ExpandAnimation(mExpandedWeight, mCollapsedWeight);
        mListener.onCollapse(mContent);
    } else {
        a = new ExpandAnimation(mCollapsedWeight, mExpandedWeight);
        mListener.onExpand(mContent);
    }

    a.setDuration(mAnimationDuration);
    mContent.startAnimation(a);
    mExpanded = !mExpanded;
}

Hopefully this will help you out, if you need more details or have questions about something let me know.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...