我有一个像这样的简单布局:
<?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:background="@android:color/white"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="My TextView" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Button" />
</LinearLayout>
单击此按钮,我将TextView设置为屏幕顶部的动画。我这样做是为了这个代码:
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView tv;
Animation mSlideOutTop;
Button button;
boolean in = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
button = (Button) findViewById(R.id.button);
mSlideOutTop = AnimationUtils.loadAnimation(this, R.anim.slide_out_top);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
in = true;
tv.startAnimation(mSlideOutTop);
tv.setVisibility(View.INVISIBLE);
}
});
}
}
我的动画是slide_out_top.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="600"
android:fromYDelta="0%"
android:toYDelta="-100%" />
</set>
所以我的问题是:当TextView动画出屏幕时,我需要按钮扩展到屏幕顶部。现在,TextView动画了,它所在的空间仍然是空的,如下所示:
关于如何做到这一点的任何想法?谢谢!
答案 0 :(得分:1)
要完成您要执行的操作,请在幻灯片动画结束时将TextView的可见性设置为GONE。类似的东西:
tv.setVisibility(View.GONE);
使用GONE而不是INVISIBLE会让Android执行布局操作,就好像视图不存在一样,谢天谢地是最容易逆转的方式来做你正在寻找的东西 - 换句话说,你可以把它设置回来稍后可见,按钮将恢复原始大小。
然后你只需要找出一种方法来调用该方法。一种方法是将它放在Runnable中并调用Handler.postDelayed(theRunnable, 600);
,虽然我更喜欢使用ViewPropertyAnimators,因为你可以为它们分配一个Runnable来执行它们的结果(调用ViewPropertyAnimator.withEndAction(theRunnable);
来执行此操作)并且它将会形成它适合你。
通过这种方式,您的动画代码将如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
button = (Button) findViewById(R.id.button);
// mSlideOutTop not used
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animateRemoval(v);
in = true;
// Do NOT set visibility to ANYTHING in here
}
});
}
private void animateRemoval(final View toRemove) {
// The method .animate() creates a ViewPropertyAnimator.
toRemove.animate()
.setDuration(600) // Previously defined in slide_out_top.xml
.translationY(1000) // Tweak this number to fit the direction/amplitude, I'm talking a wild guess
.withEndAction(new Runnable() { // Stuff in here runs AFTER the animation.
@Override
public void run() {
// It is critical that this method be executed AFTER the animation, because it
// will cause a layout to occur, and executing layouts during animation is bad news bears.
toRemove.setVisibility(View.GONE);
}
});
}