这是代码。它写得不好,但它完成了工作。不要问我椅子的初始定位是怎么做的。我只是使用了RelativeLayout 并猜测了边距。为了让这个动画在不同的手机上看起来漂亮并正确对齐,你还有很多工作要做。
这是MainActivity.java
package com.example.ferriswheel;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
RelativeLayout ferrisWheelLayout;
ObjectAnimator spin;
ImageView chairView1, chairView2, chairView3, chairView4;
ObjectAnimator counterSpin1, counterSpin2, counterSpin3, counterSpin4;
boolean currentlySpinning;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onToggleClicked(View view) {
if (currentlySpinning) {
stopAnimation(view);
currentlySpinning = false;
} else {
startAnimation(view);
currentlySpinning = true;
}
}
public void stopAnimation(View view) {
spin.cancel();
counterSpin1.cancel();
counterSpin2.cancel();
counterSpin3.cancel();
counterSpin4.cancel();
}
public void startAnimation(View view) {
chairView1 = (ImageView) findViewById(R.id.chair1);
chairView2 = (ImageView) findViewById(R.id.chair2);
chairView3 = (ImageView) findViewById(R.id.chair3);
chairView4 = (ImageView) findViewById(R.id.chair4);
ferrisWheelLayout = (RelativeLayout) findViewById(R.id.parentWheelLayout);
spin = ObjectAnimator.ofFloat(ferrisWheelLayout, "rotation", 0f, 360f);
spin.setRepeatMode(-1);
spin.setRepeatCount(Animation.INFINITE);
spin.setDuration(8000);
spin.setInterpolator(new LinearInterpolator());
spin.start();
counterSpin1 = ObjectAnimator
.ofFloat(chairView1, "rotation", 0f, -360f);
counterSpin1.setRepeatMode(-1);
counterSpin1.setRepeatCount(Animation.INFINITE);
counterSpin1.setDuration(8000);
counterSpin1.setInterpolator(new LinearInterpolator());
counterSpin1.start();
counterSpin2 = ObjectAnimator
.ofFloat(chairView2, "rotation", 0f, -360f);
counterSpin2.setRepeatMode(-1);
counterSpin2.setRepeatCount(Animation.INFINITE);
counterSpin2.setDuration(8000);
counterSpin2.setInterpolator(new LinearInterpolator());
counterSpin2.start();
counterSpin3 = ObjectAnimator
.ofFloat(chairView3, "rotation", 0f, -360f);
counterSpin3.setRepeatMode(-1);
counterSpin3.setRepeatCount(Animation.INFINITE);
counterSpin3.setDuration(8000);
counterSpin3.setInterpolator(new LinearInterpolator());
counterSpin3.start();
counterSpin4 = ObjectAnimator
.ofFloat(chairView4, "rotation", 0f, -360f);
counterSpin4.setRepeatMode(-1);
counterSpin4.setRepeatCount(Animation.INFINITE);
counterSpin4.setDuration(8000);
counterSpin4.setInterpolator(new LinearInterpolator());
counterSpin4.start();
}
}
这是它的布局activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentWheelLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:onClick="onToggleClicked" >
<ImageView
android:id="@+id/wheelSpokes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/chair1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="100dp"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:layout_marginTop="100dp"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/chair2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="100dp"
android:layout_marginLeft="200dp"
android:layout_marginRight="100dp"
android:layout_marginTop="100dp"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/chair3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="100dp"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:layout_marginTop="300dp"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/chair4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginBottom="100dp"
android:layout_marginLeft="200dp"
android:layout_marginRight="100dp"
android:layout_marginTop="300dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
代码背后的一些理论:
想象一个摩天轮单向旋转 90 度。在那段时间,摩天轮上的一把椅子通常会做什么?那把椅子随着摩天轮沿着摩天轮的轴线移动,同时,那把椅子也绕着自己的轴线旋转了负90度。
换句话说,我们可以让椅子开始绕自己的轴旋转,同时,如果我们可以将摩天轮和椅子组合起来,将其封闭在一个更大的视图中,那么我们可以从较大的轴以相同的速率在相反方向上。
希望这对你有意义。