Java 动画:缩放按钮以延伸到屏幕顶部
我有这样一个简单的布局:Java 动画:缩放按钮以延伸到屏幕顶部,java,android,animation,Java,Android,Animation,我有这样一个简单的布局: <?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:colo
<?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>
@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);
}
});
}
其中我的动画是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动画消失,它所在的空间保持为空,如下所示:
<?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>
@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);
}
});
}
有什么办法吗?谢谢 要完成您试图完成的操作,请在幻灯片放映动画结束时将TextView的可见性设置为GONE。比如:
tv.setVisibility(View.GONE);
使用GONE而不是INVISIBLE将让Android执行布局操作,就好像视图不存在一样,谢天谢地,这是实现您所寻找的最容易逆转的方式——换句话说,您可以在稍后将其设置为VISIBLE,按钮将恢复到其原始大小
然后,您只需要找出一种方法来计时该方法调用。一种方法是将它放在一个Runnable中,并调用Handler.postDelayed(theRunnable,600)代码>,虽然我个人更喜欢使用ViewPropertyImators,因为您可以为它们指定一个Runnable以在结束时执行(调用ViewPropertyImator.withEndAction(theRunnable);
来执行此操作),它会为您解决问题
这样做,动画代码将如下所示:
<?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>
@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);
}
});
}
谢谢你的帖子。这解决了我一半的问题,但当我以这种方式实现它时,当您设置tv.setVisibility(View.GONE)时,按钮“跳”到顶部。我希望它在TextView动画化时逐渐扩展到顶部,因此看起来ListView正在将TextView“推”出屏幕(不确定您是否理解我的意思)我想我明白你的意思了。动画本身可能很难自己编写代码,但是如果您在包含TextView和按钮的视图的XML定义中设置了Android:animateLayoutChanges=“true”
,您就可以让Android免费为您编写动画。试一试,看看会发生什么。嗯,它确实管用,但非常粗糙和滞后。按钮扩展延迟,文本视图文本出现问题,这听起来很奇怪。如果你做得正确,按钮应该不会受到任何影响,直到文本视图不再可见,所以我不知道为什么它会出现故障。您是否绝对确定在TextView动画完成之前不会发生setVisibility()
调用?实际上,按钮很好,只是TextView出现了小故障。我将在动画开始后立即设置可见性。我将添加一个AnimationListener并在完成后设置可见性,看看这是否有帮助