我有一张图片,这是一只蜜蜂。我想添加一个动画,让蜜蜂看起来像是在指定区域内随机飞来飞去。蜜蜂只能在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();