首页 > 解决方案 > Android studio 两边不停的动图

问题描述

我有一个图像,它是一只蜜蜂。我想添加一个动画,使蜜蜂看起来像是在指定区域内随机上下飞行。蜜蜂只能paddingTop="200dp"飞到paddingTop="370dp". 当它到达屏幕左侧时,它应该飞回屏幕右侧。它应该从两侧不停地移动。但是,我只能让它在指定的时间段内飞行或水平移动。

<ImageView
    android:id="@+id/bee"
    android:paddingTop="200dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/beeFaceRight" />

Main.java 中的代码

 ObjectAnimator animation;
 ImageView bee;

 protected void onCreate(Bundle savedInstanceState) { 
    bee = (ImageView) findViewById(R.id.bee);

    animation = ObjectAnimator.ofFloat(bee, "translationX", 100f);
    animation.setDuration(2000);
    animation.start();
 }

我试过这个,但它不起作用。

 ObjectAnimator objectX;
 ObjectAnimator objectY;
 AnimatorSet animatorXY;

 objectX = ObjectAnimator.offFloat(bee, "translationX", 0);
 objectY = ObjectAnimator.offFloat(bee, "translationY", 200);
 animatorXY.playTogether(objectX, objectY);
 animatorXY.setDuration(500);
 animatorXY.start();

标签: android

解决方案


推荐阅读