有一个奇怪的.我把它归结为一个非常简单的例子.真的想不出来.
我在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;
}
如果你现在运行它,它不会滑下来.完全没有.但是,如果您要将Button移动到我已命名为Overview的LinearLayout并将其放在Child 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;
}
}
任何帮助都会很棒!
解决方法
我不知道为什么你的代码不想做动画.但我修改了一点点.我所做的是将动画应用于父布局,而不是应用于正在转换的布局.
这是xml的代码,我只在父布局中添加了一个Id:
这是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>
以下是按钮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,true);
anim.setDuration(2000);
parent.startAnimation(anim);
return;
}