Android 使用动画使物体利用重力落向地面

L1g*_*ira 5 java animation android gravity

我是 Android 开发的新手,我正在尝试在屏幕顶部创建一个图像并使其落到底部。我一直环顾四周,无法得到正确的答案或指示。我发现的许多示例已经过时,而且许多方法不再有效。

我的 drawable 文件夹中有一个图像,我想在 10 秒后创建它,让它像有重力一样落向地面。

我已经看到很多关于 ObjectAnimator、画布、速度、动画的内容。我不知道从哪里开始。

A H*_*ard 5

请务必查看ViewPropertyAnimator类及其方法。对于加速和弹跳等,有所谓的插值器,使动画更逼真/逼真。我能想到的最简单的方法是这样的:

假设您在这样的 xml 文件中有一个 RelativeLayout 作为父级和一个 ImageView 作为子级:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/your_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@mipmap/ic_launcher" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

然后在您的 Java 代码中像这样启动动画:

ImageView imageView = (ImageView) findViewById(R.id.your_image);

float bottomOfScreen = getResources().getDisplayMetrics()
    .heightPixels - (imageView.getHeight() * 4);
        //bottomOfScreen is where you want to animate to

imageView.animate()
    .translationY(bottomOfScreen)
    .setInterpolator(new AccelerateInterpolator())
    .setInterpolator(new BounceInterpolator())
    .setDuration(2000);
Run Code Online (Sandbox Code Playgroud)

这应该让你开始。