Android Animation共有四大类型,分别是
Alpha 透明度动画
Scale 大小伸缩动画
Translate 位移动画
Rotate 旋转动画
这四类动画按模式又可分为:
tweened animation(渐变动画) —— alpha 与 scale
frame by frame(画面转换动画)
官方给予的Rotate常用属性如上所示。
android:drawable 需要进行旋转动画的图片
android:fromDegrees
android:toDegrees 旋转的结束点(旋转最终角度)
andoird:pivotX 旋转点的X值(距离左侧的偏移量)
android:pivotY旋转点的Y值(距离顶部的偏移量)
android: visible这个好理解,就是图片初始的显示状态
详细属性说明如下:
android:fromDegrees 起始的角度度数。
android:toDegrees 结束的角度度数,负数表示逆时针,正数表示顺时针。如10圈则比android:fromDegrees大3600即可。
android:pivotX 旋转中心的X坐标。浮点数或是百分比。浮点数表示相对于Object的左边缘,如5; 百分比表示相对于Object的左边缘,如5%; 另一种百分比表示相对于父容器的左边缘,如5%p; 一般设置为50%表示在Object中心。
android:pivotY 旋转中心的Y坐标。浮点数或是百分比。浮点数表示相对于Object的上边缘,如5; 百分比表示相对于Object的上边缘,如5%; 另一种百分比表示相对于父容器的上边缘,如5%p; 一般设置为50%表示在Object中心
android:duration 表示从android:fromDegrees转动到android:toDegrees所花费的时间,单位为毫秒。可以用来计算速度。
android:interpolator表示变化率,但不是运行速度。一个插补属性,可以将动画效果设置为加速,减速,反复,反弹等。默认为开始和结束慢中间快,
android:startOffset
在调用start函数之后等待开始运行的时间,单位为毫秒,若为10,表示10ms后开始运行。
android:repeatCount 重复的次数,默认为0,必须是int,可以为-1表示不停止。
android:repeatMode
重复的模式默认为restart即重头开始重新运行,可以为reverse即从结束开始向前重新运行。在android:repeatCount大于0或为infinite时生效。
android:detachWallpaper表示是否在壁纸上运行。
android:zAdjustment 表示被animated的内容在运行时在z轴上的位置,默认为normal。normal保持内容当前的z轴顺序,top运行时在最顶层显示,bottom运行时在最底层显示。
在res目录下创建anim文件夹,并新建动画rotate_anim.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:duration="900"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%"/>
</set>
在activity_main.xml主布局文件中添加imageView布局控件
<ImageView
android:id="@+id/ivRotatePicture1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:src="@drawable/pic_wind" />
最后看下main_activity.java对Animation的实现:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.ivRotatePicture);
imageView.setOnClickListener(this);//这里想点击图片 改变旋转速度
animation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
LinearInterpolator lin = new LinearInterpolator();//设置旋转方式为匀速旋转
animation.setInterpolator(lin);//设置旋转方式为匀速旋转
animation.setDuration(1200);//设置旋转一周时间
imageView.setAnimation(animation);//将动画和image控件相关联
imageView.startAnimation(animation);//最后启动动画
}
付上运行效果图
最后附上自己调试代码源码,欢迎提意见!