相关疑难解决方法(0)

使用动画更改布局的权重

在我的主布局文件中,我有一个RelativeLayout,权重为1(基本上显示一个地图)在LinearLayout上方,权重为2,这样声明:

<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>
Run Code Online (Sandbox Code Playgroud)

DownLayout包含一个项目列表,当我点击一个项目时,我想将DownLayout的权重更改为4,因此上部布局(地图)仅占屏幕的1/5而不是1/3.

我设法通过更改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);
Run Code Online (Sandbox Code Playgroud)

它有效,但我不满意,变化太直接,没有过渡,而我希望它是顺利的.有没有办法使用动画?

我发现了一些使用ObjectAnimator来改变weightSum的例子,但它并不想要我想要的(如果我只改变这个属性,我在我的向下布局下面有一些空闲空间):

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

有没有办法使用ObjectAnimator(或其他东西)来做到这一点?

谢谢 !

android android-animation android-layout

11
推荐指数
1
解决办法
7353
查看次数

使用ObjectAnimator为weightSum属性设置动画

介绍:

我有一个LinearLayout,它包含两个子LinearLayout,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dual_pane"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:weightSum="1.0">

    <!-- Screen 1 -->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="#ff0000"
        android:layout_weight="1">
    </LinearLayout>

    <!-- Screen 2 -->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="#ff6600"
        android:layout_weight="1">
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

最初,我希望"屏幕1"可以使用所有屏幕宽度.因此,我的R.id.dual_pane的weightSum属性为1.0.这很好用!如果weightSum = 1.0,屏幕1占据整个屏幕!

加载一些资源后,我将R.id.dual_pane weighSum更改为2.0,这会导致屏幕1和屏幕2的屏幕宽度减少50%.这也很完美.当weightSum = 2.0时,两个屏幕占据宽度的50%.

问题:

我想为weightSum属性设置动画,所以我的Screen2将会滑入.我的目标是HoneyComb,所以minSDK版本是11,我想,使用新的ObjectAnimator框架,我可以很容易地动画这个属性,以获得一个很好的平滑效果.我验证了LinearLayout确实有getWeightSum()和setWeightSum()方法(我认为这是使用ObjectAnimator所必需的).

自己的努力:

这是我使用ObjectAnimator显示和隐藏Screen2的代码:

private void showScreen2()
{
    //Not-animated will work...
    //mDualPane.setWeightSum(2.0f);

    // Now try to animate the weightSum
    float ws = mDualPane.getWeightSum();
    ObjectAnimator anim = ObjectAnimator.ofFloat(mDualPane, "weightSum", ws, 2.0f);
    anim.setDuration(5000);
    anim.start();
}

private void hideScreen2()
{
    //Not-animated will work...
    //mDualPane.setWeightSum(1.0f);

    // Now …
Run Code Online (Sandbox Code Playgroud)

android android-animation android-linearlayout

7
推荐指数
1
解决办法
5353
查看次数