7

奇妙なものを手に入れました。私はそれを非常に単純な例に落とし込むことに成功しました。本当にそれを理解することはできません。

LinearLayout内にLinearLayoutがあります。アニメーションを使用して子のサイズを変更したい(時々、おやつを使っています)。これが私のレイアウトXMLです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
        <!-- Lots more controls will appear above -->
        <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:id="@+id/animation_subject">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/hello"
                android:gravity="center" />
        </LinearLayout> 
        <!-- Lots more controls will appear below -->
    </LinearLayout>

    <Button android:text="Slide it Down!" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="btnDoAnimDown_OnClick" />

</LinearLayout>

ご覧のとおり、ボタンがあります。これがそのボタンのコードです。

public void btnDoAnimDown_OnClick(View v)
{
    View pnlSlider = findViewById(R.id.animation_subject);
    pnlSlider.measure(1000, 1000);
    DropDownAnim anim = new DropDownAnim(pnlSlider, pnlSlider.getMeasuredHeight(), true);
    anim.setDuration(2000);
    pnlSlider.startAnimation(anim);

    return;     
}

今実行すると、スライドダウンしません。まったく。ただし、ボタンをOverviewという名前のLinearLayoutに移動し、子LinearLayoutの後に配置すると、うまく機能します。このような...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
        <!-- Lots more controls will appear above -->
        <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:id="@+id/animation_subject">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/hello"
                android:gravity="center" />
        </LinearLayout> 
        <!-- Lots more controls will appear below -->
        <Button android:text="Slide it Down!" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="btnDoAnimDown_OnClick" />
    </LinearLayout>

</LinearLayout>

期待どおりにスライドダウンします。明らかに、親レイアウトで何かが起こっています... getMeasuredHeight()は正しい値を返すので、アニメーションが実際に実行されないだけです!

これはアニメーションのクラスです、私は何かばかげたものが欠けていますか?たぶんそうです!

import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class DropDownAnim extends Animation {
    int targetHeight;
    View view;
    boolean down;

    public DropDownAnim(View view, int targetHeight, boolean down) {
        this.view = view;
        this.targetHeight = targetHeight;
        this.down = down;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        int newHeight;
        if (down) {
            newHeight = (int) (targetHeight * interpolatedTime);
        } else {
            newHeight = (int) (targetHeight * (1 - interpolatedTime));
        }
        Log.d("new height", String.valueOf(newHeight));
        view.getLayoutParams().height = newHeight;
        view.requestLayout();
    }

    @Override
    public void initialize(int width, int height, int parentWidth,
            int parentHeight) {
        super.initialize(width, height, ((View)view.getParent()).getWidth(), parentHeight);
    }

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

どんな助けでも素晴らしいでしょう!

4

3 に答える 3

3

ここで推測させてください。

最初のレイアウトでは、内側のレイアウトとボタンが外側の線形レイアウトを共有しています。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
        <!-- Lots more controls will appear above -->
        <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:id="@+id/animation_subject">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/hello"
                android:gravity="center" />
        </LinearLayout> 
        <!-- Lots more controls will appear below -->
    </LinearLayout>

    <Button android:text="Slide it Down!" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="btnDoAnimDown_OnClick" />

</LinearLayout>

これにより、外側のレイアウトがViewGroupになります。

ただし、アニメーションのinitializeメソッドでは、指定されたビューの親に対してキャストを実行します。

それをビューにキャストしますが、これは最初のレイアウトにはありません。

あなたの第二にそれはそうです、そしてそれがそれが働く理由です。

代わりに、ViewGroupにキャストしてみてください。

@Override
    public void initialize(int width, int height, int parentWidth,
            int parentHeight) {
        super.initialize(width, height, ((ViewGroup)view.getParent()).getWidth(), parentHeight);
    }
于 2012-05-14T09:41:04.227 に答える
2

それが役立つかどうかはわかりませんが、で追加android:layout_weight="1"LinearLayoutてみてくださいid="@+id/animation_subject"

于 2012-05-15T14:08:28.830 に答える
2

コードがアニメーションを実行したくない理由がわかりません。しかし、少し変更を加えて修正しました。私がしたことは、変換しているレイアウトではなく、親レイアウトにアニメーションを適用することです。これがxmlのコードです。親レイアウトにIDを追加しただけです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout android:id="@+id/animation_subject_parent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
        <!-- Lots more controls will appear above -->
        <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:id="@+id/animation_subject">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/hello"
                android:gravity="center" />
        </LinearLayout> 
        <!-- Lots more controls will appear below -->
    </LinearLayout>
        <Button android:text="Slide it Down!" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="btnDoAnimDown_OnClick" />

</LinearLayout>

そして、これがボタンクリックのコードです:

public void btnDoAnimDown_OnClick(View v)
{
    View pnlSlider = findViewById(R.id.animation_subject);
    View parent = findViewById(R.id.animation_subject_parent);

    pnlSlider.measure(1000, 1000);
    DropDownAnim anim = new DropDownAnim(pnlSlider, pnlSlider.getMeasuredHeight(), true);
    anim.setDuration(2000);

    parent.startAnimation(anim);

    return;     
}
于 2012-05-21T01:45:18.597 に答える