Got a weird one. I've managed a bring it down to a very simple example. Really can't figure it out.
I've got a LinearLayout inside a LinearLayout. I want to resize the child using an animation (which I have working a treat, sometimes). Here is my Layout 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>
Now, As you can see, there is a button, and here is the code for that button.
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;
}
If you were to run it now, it wouldn't slide down. At all. However, if you were to move the Button into the LinearLayout which I have named Overview and put it after the Child LinearLayout, it works a treat! Like this...
<?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>
It now slides down, exactly as expected. Clearly something is going it with the parent layout... As getMeasuredHeight() returns the correct value, it's just the animation doesn't actually run!
This is the animation class, am I missing something silly? Probably am!
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;
}
}
Any help would be fantastic!
Let me give a guess here.
In your first layout the inner layout and the button share the outer 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 -->
</LinearLayout>
<Button android:text="Slide it Down!" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="btnDoAnimDown_OnClick" />
</LinearLayout>
Which makes the outer layout a ViewGroup!
But in the initialize method of your Animation you do a cast on the parent of the given view.
You cast it to View, which it isnt in your 1st layout.
In your second it is, and thats why its working.
So i would like you to give it a try casting to ViewGroup instead:
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, ((ViewGroup)view.getParent()).getWidth(), parentHeight);
}
I don't know why your code don't want to do the animation. But I fixed with a little change. What I did is apply the animation to the parent layout and not to the layout that you are transforming. Here is the code of the xml, I only added an Id to the parent layout:
<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>
And here is the code of the button clic:
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;
}
Not sure if it will help, but try to add android:layout_weight="1"
to LinearLayout
with id="@+id/animation_subject"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With