属性动画设置延迟后,如果在未开始前取消,会执行onAnimationStart方法,特此记录

本文介绍了一个使用Animator实现的带有延迟效果的移动动画示例。该示例通过ObjectAnimator类实现TextView沿Y轴移动的效果,并提供了开始和取消动画的方法。同时,文章展示了如何设置动画的监听器来跟踪动画的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class AnimatorDelayTestActivity extends AppCompatActivity {



    private View mTextView;
    private ObjectAnimator translationY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animator_delay);
        mTextView = findViewById(R.id.animator_test);
    }

    public void start(View view) {
        translationY = moveView(mTextView, "translationY", 0, mTextView.getHeight(), 5000);
        translationY.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                Log.i("onAnimationUpdate", "start方法");
            }

        });
        translationY.start();
    }

    public void cancel(View view) {
        translationY.cancel();
    }

    private ObjectAnimator moveView(View view, final String attr, float start, float end, int delay) {
        ObjectAnimator move = ObjectAnimator.ofFloat(view, attr, start, end);
        move.setDuration(1000);
        move.setStartDelay(delay);
        return move;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lizheng_ds3.myapplication.MainActivity">

    <TextView
        android:id="@+id/animator_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="动画测试"
        android:textSize="20dp" />

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/animator_test"
        android:layout_marginTop="50dp"
        android:onClick="start"
        android:text="开始" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/start"
        android:layout_marginTop="50dp"
        android:onClick="cancel"
        android:text="取消" />
</RelativeLayout>

@Override
public void cancel() {
    // Only cancel if the animation is actually running or has been started and is about
    // to run
    AnimationHandler handler = getOrCreateAnimationHandler();
    if (mPlayingState != STOPPED
            || handler.mPendingAnimations.contains(this)
            || handler.mDelayedAnims.contains(this)) {
        // Only notify listeners if the animator has actually started
        if ((mStarted || mRunning) && mListeners != null) {
            if (!mRunning) {
                // If it's not yet running, then start listeners weren't called. Call them now.
                notifyStartListeners();
            }
            ArrayList<AnimatorListener> tmpListeners =
                    (ArrayList<AnimatorListener>) mListeners.clone();
            for (AnimatorListener listener : tmpListeners) {
                listener.onAnimationCancel(this);
            }
        }
        endAnimation(handler);
    }
}
private void notifyStartListeners() {
    if (mListeners != null && !mStartListenersCalled) {
        ArrayList<AnimatorListener> tmpListeners =
                (ArrayList<AnimatorListener>) mListeners.clone();
        int numListeners = tmpListeners.size();
        for (int i = 0; i < numListeners; ++i) {
            tmpListeners.get(i).onAnimationStart(this);
        }
    }
    mStartListenersCalled = true;
}

不知道为什么这么设计,但是源码里面就是这么写的,用的时候注意一下


在Win10中配置VS Code调试Linux环境下的C语言代码,需要进行以下步骤: 1. 首先,在Win10中安装WSL(Windows Subsystem for Linux),以便能够在Windows系统上运行Linux操作系统。打开“控制面板”,选择“程序”->“打开或关闭Windows功能”,勾选“适用于Linux的Windows子系统”,点击确定并重启系统。 2. 在Microsoft Store中下载并安装所需版本的Linux发行版(如Ubuntu)。 3. 安装VS Code,打开VS Code后,按下`Ctrl + Shift + X`打开扩展市场,搜索并安装"C/C++"、"Remote - WSL"和"Code Runner"等插件。 4. 打开已安装好的WSL,创建一个新的C语言项目或打开已有的项目文件夹。 5. 在VS Code中,打开新建或已有的C语言文件,如main.c。 6. 点击VS Code左侧的“调试”图标,在顶部的下拉菜单中选择“添加配置”->“C++ (GDB/LLDB)",此步骤将会自动生成一个`.vscode`文件夹,并在其中创建一个`launch.json`文件。 7. 打开`launch.json`文件,将默认的配置修改为如下形式: ```json { "version": "0.2.0", "configurations": [ { "name": "C++ Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/main", "args": [], "stopAtEntry": true, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb" } ] } ``` 8. 配置好后,可以在main.c文件中设置断点,然后点击VS Code左下角的“启动调试”按钮,就能在Linux环境下调试该C语言代码了。可以通过F10单步调试,F5继续运行,查看变量的值等。 通过以上步骤,我们可以在Win10中配置VS Code调试Linux环境下的C语言代码,方便进行代码测试和调试工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值