java - 动画:缩放按钮以扩展到屏幕顶部

标签 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: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 动画消失了,它所在的空间仍然是空的,如下所示:

enter image description here

关于如何做到这一点有什么想法吗?谢谢!

最佳答案

要完成您正在尝试做的事情,请在滑出动画结束时将 TextView 的可见性设置为 GONE。像这样的东西:

tv.setVisibility(View.GONE);

使用 GONE 而不是 INVISIBLE 将使 Android 执行布局操作,就好像 View 不存在一样,幸运的是,这是执行您正在寻找的操作的最容易可逆的方式 - 换句话说,您可以将其设置回去稍后变为 VISIBLE,按钮将恢复为原始大小。

然后您只需要找出一种方法来为该方法调用计时。一种方法是将它放在一个 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);
                }
            });
}

关于java - 动画:缩放按钮以扩展到屏幕顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28095122/

相关文章:

java - 使用 JSON 解析获取 OpenWeathermapApi 数据

swift - 尝试快速移动 x 和 y 字段中的图像

jQuery SlideIn 动画到 Angular 中的 CSS 动画

java - 如何从 Ant 运行具有相对路径的 java 类?

java - 每晚构建——我们是否先删除 "target"中的文件?

javascript - Visual Studio Code 不会在 NativeScript 应用程序的断点处停止

android - 全息主题和 API 8

java - 编辑 Java 类所有属性值的注释

java - Guava :从包含变量的对象列表中获取变量列表

ios - 动画重复时更改 View 的颜色