利用RotateAnimation實現齒輪的無限勻速轉動:
效果圖:
(不會做動態圖,齒輪其實一直在轉動)
基本代碼如下:
1. Activity的布局文件:
[html]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/gear_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/gear" />
</RelativeLayout>
2.anim的配置文件
[html]
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:interpolator="@android:anim/linear_interpolator" />
3.Activity實現:
[java]
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initGear();
}
private void initGear() {
RotateAnimation gearAnim = (RotateAnimation) AnimationUtils.loadAnimation(this, R.anim.running_gear);
ImageView gearIV = (ImageView) findViewById(R.id.gear_iv);
gearIV.startAnimation(gearAnim);
}
}